【问题标题】:my listview doesn't fill parent我的列表视图没有填满父母
【发布时间】:2011-11-02 19:07:15
【问题描述】:

<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>

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

我不知道它是否有用,但这是我的 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);

}
}

【问题讨论】:

  • 能否显示整个 xml 代码?
  • 运行良好,伙计,我检查了你的代码并执行了。
  • 也许在你的 xml 中的某个地方你有 wrap_content 而不是 fill_parent。因此,如果您发送所有 xml 代码,我将能够提供帮助
  • 它可以正常工作,但我接下来要做的是:让列表视图与 ScrollView 一起工作。寻找 IMDB Android 应用,因为他们做了一个滚动视图,你可以看到里面有列表视图,它看起来不像列表。
  • 如果你只有 textview 和 listview 那么我认为你不需要使用滚动视图只需使用 listview 并添加页眉或页脚任何视图。

标签: java android listview scrollview


【解决方案1】:

我认为您的 ScrollView 没有填满整个屏幕。在 scrollView 中使用它

android:fillViewport="true"

【讨论】:

  • 好的,谢谢它是解决方案,但我有一件小事我想做:滚动布局中的所有内容,就像它们都是同一个屏幕一样。
  • 这是一个很好的解决方法。作为一个注释,我必须在它包含的 ScrollView 和 ListView 中添加这个值。在 android studio 0.9.9 中有一个 UI 复选框选项可以直接启用它。
  • 感谢您的帮助:)
【解决方案2】:

RelativeLayout 替换为垂直的LinearLayout,并在ListView 上设置android:layout_weight="1"android:layout_height="wrap_content"

<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>

查看this article 了解有关布局权重的更多信息。

另外,我会完全删除ScrollViewListView 自己管理滚动,如果将其嵌入到处理滚动的容器中,您将遇到各种问题。如果您想滚动 @is/tv_test 以及您的 ListView 内容,请考虑将其作为标题放在您的 ListView 中。如果您希望它在 ListView 滚动时保持静止,请将其保留在 ListView 之外。

【讨论】:

  • +1 表示您不应该在滚动视图中包含列表视图。今天早上让我有些奇怪
猜你喜欢
  • 1970-01-01
  • 2017-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-17
  • 2020-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多