首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在片段中获取按钮的宽度/高度

如何在片段中获取按钮的宽度/高度
EN

Stack Overflow用户
提问于 2016-05-27 07:30:47
回答 3查看 2.5K关注 0票数 4

如何在片段中获取任意视图(Button、TextView、RelativeLayout)的高度/宽度,我尝试这样的方法

代码语言:javascript
运行
复制
 public static class FirstDemoFragment extends Fragment {

    int width,height;
    private Button button;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_layout, container, false);
        button = (Button) view.findViewById(R.id.fragment_demo_button);

        width =  button.getWidth();
        height = button.getHeight();


        System.out.println("==== View Width : " + width);
        System.out.println("==== View height : " + height);

        width = button.getMeasuredWidth();
        height = button.getMeasuredHeight();


        System.out.println("==== View Width : " + width);
        System.out.println("==== View height : " + height);

        return view;
    }

我的layout.xml文件是

代码语言:javascript
运行
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/does_really_work_with_fragments"
    android:id="@+id/fragment_demo_button"
    android:layout_centerInParent="true" />

我对此做了一点研究,我用这种方法在活动中找到了按钮的宽度/高度。

代码语言:javascript
运行
复制
@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
    relMain = (RelativeLayout) findViewById(R.id.layoutMain);

    width = relMain.getMeasuredWidth();
    height = relMain.getMeasuredHeight();

    width = relMain.getWidth();
    height = relMain.getHeight();

    //System.out.println("=== Width : " + width);
    //System.out.println("=== height : " + height);


}

但是onWindowFocusChanged()在android片段中是不可用的?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-05-27 07:42:06

在片段中,在片段的onCreateView方法中尝试下面的代码:

代码语言:javascript
运行
复制
button = (Button) view.findViewById(R.id.fragment_demo_button);   
     ViewTreeObserver vto = button.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                button.getViewTreeObserver().removeOnPreDrawListener(this);
                Log.d("", "Height: " + button.getMeasuredHeight()+ " Width: " + button.getMeasuredWidth());
                return true;
            }
        });
票数 4
EN

Stack Overflow用户

发布于 2016-05-27 07:35:37

在片段中使用以下代码片段的onCreateView方法

代码语言:javascript
运行
复制
ViewTreeObserver mviewtreeobs = button.getViewTreeObserver();
mviewtreeobs .addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
    width = button.getWidth();
    height = button.getHeight(); 
}
});
票数 2
EN

Stack Overflow用户

发布于 2016-05-27 07:39:12

您可以将布局侦听器添加到按钮中,如下所示:

代码语言:javascript
运行
复制
button.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int width = button.getWidth();
            if(width > 0){
              int height = button.getHeight();
              // do something with your width and height
              ....
              // remove layout listener
              button.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
            }
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37477420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档