Android 如何更改父视图';s的高度以适合子视图

Android 如何更改父视图';s的高度以适合子视图,android,android-layout,android-custom-view,Android,Android Layout,Android Custom View,我有一个相对视图,其中包含一个listview。我在运行时创建这个相对视图,并将内容(基本上是一个字符串数组)添加到列表视图中。我已将listview的宽度和高度设置为“包裹内容”。 问题是我的列表父级相对视图没有调整大小以显示我的列表视图。它只显示列表的第一个值。 我知道我必须重写onmeasure方法。那么,如何准确地在onmeasure方法中获得listview的高度,以便将其传递给父视图 我读过很多类似的问题,但都没能解决 在我的onmeasure()方法中,getMeasuredHei

我有一个相对视图,其中包含一个listview。我在运行时创建这个相对视图,并将内容(基本上是一个字符串数组)添加到列表视图中。我已将listview的宽度和高度设置为“包裹内容”。 问题是我的列表父级相对视图没有调整大小以显示我的列表视图。它只显示列表的第一个值。 我知道我必须重写onmeasure方法。那么,如何准确地在onmeasure方法中获得listview的高度,以便将其传递给父视图 我读过很多类似的问题,但都没能解决

在我的onmeasure()方法中,getMeasuredHeight()还返回0

更新

我的主要布局活动

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_route_search"
android:layout_width="match_parent"
android:layout_height="match_parent" >

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

</ScrollView>
//int listViewh=0; //私有浮动列表视图高度

    public block(Context context) {
        super(context);

        View mView = View.inflate(getContext(), R.layout.lo_block, this);

        ListView lst = (ListView) mView.findViewById(R.id.listView1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getActivity(), android.R.layout.simple_list_item_1,
                new String[] { "ad", "sfsd", "sfsf", "asfsfs" });

        lst.setAdapter(adapter);
    }

    public block(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);

    }

    @Override
    public int getMinimumHeight() {

        return super.getMinimumHeight();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int w = MeasureSpec.getSize(widthMeasureSpec);
        int h = MeasureSpec.getSize(heightMeasureSpec);
}

}
main.xml

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

</LinearLayout>

在我的例子中,确切的问题是我试图将列表视图放在scrollview中。通常不建议这样做。但我已经设法扩展了我的listview。虽然它不能滚动,但比以前更好。下面是我在构造函数中添加的代码

ListView lst = (ListView) mView.findViewById(R.id.listView1);
        String[] arrayList = new String[] { "ad", "sfsd", "sfsf", "asfsfs",
                "ssfsff", "llkllk", "4323" };

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getActivity(), android.R.layout.simple_list_item_1,
                arrayList);
        int totalHeight = 0;
        for (int i = 0; i < adapter.getCount(); i++) {
            View listItem = adapter.getView(i, null, lst);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        LayoutParams lp = (LayoutParams) lst.getLayoutParams();
        int height = totalHeight;

        // arraylist list is in which all data is kept

        lp.height = height;
        lst.setLayoutParams(lp);

        lst.setAdapter(adapter); 
ListView lst=(ListView)mView.findviewbyd(R.id.listView1);
String[]arrayList=新字符串[]{“ad”、“sfsd”、“sfsf”、“asfsfs”,
“ssfsff”、“llkllk”、“4323”};
ArrayAdapter适配器=新的ArrayAdapter(
getActivity(),android.R.layout.simple\u list\u item\u 1,
arrayList);
int totalHeight=0;
对于(inti=0;i
忘了提到,Relativelayout的宽度和高度也是包裹内容。结果也是一样的。请注意,如果将我的listview的高度设置为一些像素,如“250dp”,那么我的相对布局显示了这一点,因此唯一的问题是在相对视图呈现自身之前为其提供计算高度。不完全是这样,它可以正常工作,但如果我将此代码放在片段中,则不会。用fragment也试试这个。主活动XML包含一个带有linearlayout的framelayout。我已经更新了完整的代码。运行一次,请让我知道我哪里做错了。我将显示一个复合视图。该文本视图类似于显示一些信息的标题,然后是列表视图。我想多次创建此视图,并将每个视图添加到一个视图下的滚动视图中。列表视图的组件是动态的,仅在运行时可用。因此,我想设置此视图的高度以适应标题(文本视图[固定高度]和列表视图(其高度是可变的)。我正在使用片段,它只是我应用程序的一部分。根据用户选择的选项,片段将更改。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

</LinearLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    getFragmentManager().beginTransaction().replace(R.id.fragment,
            new Frag()).commit();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


}
ListView lst = (ListView) mView.findViewById(R.id.listView1);
        String[] arrayList = new String[] { "ad", "sfsd", "sfsf", "asfsfs",
                "ssfsff", "llkllk", "4323" };

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getActivity(), android.R.layout.simple_list_item_1,
                arrayList);
        int totalHeight = 0;
        for (int i = 0; i < adapter.getCount(); i++) {
            View listItem = adapter.getView(i, null, lst);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        LayoutParams lp = (LayoutParams) lst.getLayoutParams();
        int height = totalHeight;

        // arraylist list is in which all data is kept

        lp.height = height;
        lst.setLayoutParams(lp);

        lst.setAdapter(adapter);