java 我的列表视图没有填充父级

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7175770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:57:22  来源:igfitidea点击:

my listview doesn't fill parent

javaandroidlistviewscrollview

提问by Tsunaze

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"

    <TextView 
        android:id="@+id/tv_test"
        android:text="Test "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
    <ListView 
        android:id="@+id/list_test"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/tv_test"

  ></ListView>
</RelativeLayout>

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"

    <TextView 
        android:id="@+id/tv_test"
        android:text="Test "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
    <ListView 
        android:id="@+id/list_test"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/tv_test"

  ></ListView>
</RelativeLayout>

And everything is in a ScrollView, i don't know why but the relative layout seems to set a wrap content. And it makes the ListView small and not taking the whole place.

一切都在 ScrollView 中,我不知道为什么,但相对布局似乎设置了换行内容。它使 ListView 变小而不是占据整个地方。

I don't know if it's useful but here's my java code :

我不知道它是否有用,但这是我的 Java 代码:

public class ShowView extends Activity{
ListView lv;
ArrayAdapter<String> adapter;
String[] myList = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Height", "Nine", "Ten"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.show);
    lv = (ListView) findViewById(R.id.list_test);
    adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, myList);
    adapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
    lv.setAdapter(adapter);

}
}

回答by droid kid

I think you ScrollView is not filling the entire screen. Use this inside scrollView

我认为您 ScrollView 没有填满整个屏幕。在滚动视图中使用它

android:fillViewport="true"

回答by Mark Allison

Replace the RelativeLayoutwith a vertical LinearLayout, and set android:layout_weight="1"and android:layout_height="wrap_content"on your ListView:

将 替换RelativeLayout为垂直LinearLayoutandroid:layout_weight="1"android:layout_height="wrap_content"在您的 ListView 上设置和:

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">

    <TextView 
        android:id="@+id/tv_test"
        android:text="Test "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ListView 
        android:id="@+id/list_test"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_below="@id/tv_test" />
</LinearLayout>

Check out this articlefor further information on layout weights.

查看本文以获取有关布局权重的更多信息。

Also, I would remove the ScrollViewaltogether. ListViewmanages scrolling itself, and you will run in to all kinds of problems if you embed it inside a container which handles scrolling. If you want to scroll @is/tv_testalong with your ListViewcontent, then consider putting it inside your ListViewas a header. If you want it to remain static when the ListViewscrolls, then keep it outside the ListView.

另外,我会ScrollView完全删除。ListView管理滚动本身,如果您将其嵌入处理滚动的容器中,您将遇到各种问题。如果你想@is/tv_test随着你的ListView内容滚动,那么考虑把它放在你ListView的标题中。如果您希望它在ListView滚动时保持静态,请将其放在ListView.