布局高度为-2

时间:2013-12-09 08:46:17

标签: android custom-controls layout-inflater

以下是我在customList

中膨胀的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <exp.viswanath.dropper.CustomView
        android:id="@+id/custom_view"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="#e1e1e1"
        android:padding="4dp" />
    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/custom_view" />
</RelativeLayout>
</LinearLayout>

我的自定义视图布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<exp.viswanath.dropper.CustomList
    android:id="@+id/wrapper"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

我将height Layout的{​​{1}} parentlistview作为-1和-2。但我得到身份custom_view为105的元素的正确高度。我在CustomList

的构造函数中膨胀

CODE

    View rowView = LayoutInflater.from(context).inflate(R.layout.activity_dropper, null);

    parentLayout = (RelativeLayout)rowView.findViewById(R.id.parent);

    Log.d("", "DEBUG:pl:" + parentLayout.getLayoutParams().height);

    myView = (CustomView)parentLayout.findViewById(R.id.custom_view);
    _viewHeight = myView.getLayoutParams().height;

    initList();

    _height = (-1) * _viewHeight;
    Log.d("","height:" + _height);
    listView = (ListView)parentLayout.findViewById(R.id.listView);

    Log.d("", "DEBUG:lv:" + listView.getLayoutParams().height);

有人知道问题或经历过相同的问题吗?布局根本不可见

问题

MY VIEW IS NOT VISIBLE

2 个答案:

答案 0 :(得分:2)

问题是myView.getLayoutParams().height;不会返回高度,因为它还没有被测量过。在这种情况下,它会返回与视图关联的常量,例如FILL_PARENTWRAP_CONTENT等。请参阅文档here

  

有关视图有多高的信息。可以是其中之一   常量FILL_PARENT(由API级别8中的MATCH_PARENT替换)或   包装内容。或确切的大小。

要获得布局的大小,您应该使用getWidth()getMeasuredWidth()方法。但是,在测量视图之前,这些方法不会给您有意义的答案。在这里阅读how android draws views

您可以将onWindowFocusChanged()改为mentioned in this thread来获得正确的尺寸。

或者,你可以这样做:

    myView.post(new Runnable() {
        @Override
        public void run() {
            Log.d("", "DEBUG:lv:" + myView.getMeasuredHeight());
        }
    });

答案 1 :(得分:0)

试试这种方式

listView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                Log.d("", "DEBUG:lv:" + listView.getLayoutParams().height);
            }
        });