【问题标题】:How to detect the height of a view?如何检测视图的高度?
【发布时间】:2016-01-08 10:30:13
【问题描述】:

我有 2 个布局:

ma​​in.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/addhere"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </RelativeLayout>

</ScrollView>

a.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_height="200dp"
        android:layout_width="match_parent"/>

</LinearLayout>

我想扩充 a.xml 布局并将其添加到 id 为 addhere 的视图组中,然后检测 a 视图的高度。 a.getHeight() 给我 0。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        View a = getLayoutInflater().inflate(R.layout.a,null);
        ViewGroup b = (ViewGroup)findViewById(R.id.addhere);
        b.addView(a);
        Log.d("states", a.getHeight() + ""); // show me 0

    }

请告诉我如何检测 a 视图的高度?

【问题讨论】:

    标签: android view height


    【解决方案1】:
    a.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            Log.d("states", "a's height: " + v.getHeight());
            a.removeOnLayoutChangeListener(this);
        }
    });
    

    【讨论】:

      【解决方案2】:

      这是因为视图树在调用onCreate() 时仍未测量和布局。
      如果 api view.getViewTreeObserver().addOnGlobalLayoutListener()
      否则使用view.addOnLayoutChangeListener()
      请记住在这两种情况下都删除侦听器。

      【讨论】:

        【解决方案3】:

        你可以试试这样的:

        final View view=a;
            view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
            {
                @TargetApi(16)
                @Override
                public void onGlobalLayout()
                {
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
                    {
                        view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
                    else
                    {
                        view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    }
        
                    final int width=view.getWidth();
                    final int height=view.getHeight();
                }
            });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-06-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-03
          相关资源
          最近更新 更多