【问题标题】:No vertical scrollers displayed in a ListViewListView 中不显示垂直滚动条
【发布时间】:2015-06-22 13:27:01
【问题描述】:

以下是正在使用的布局。单选按钮组是动态添加的。组中的单选按钮很少,单选按钮更多,并且一旦方向改变,一切都可以正常工作,并且期望看到垂直滚动条并且它不显示。

还有其他属性需要设置到 ListView 吗?

感谢指点。

如果它值得,它是这个问题的扩展RadioGroup and TextView in a RelativeLayout - RadioGroup is hiding TextView

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

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height=“wrap_content” >
</ListView>

<TextView
    android:id="@+id/sample"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="20dp"
    android:background="@color/black"
    android:gravity="center"
    android:text="@string/sample"
    android:textColor="@color/white" />

<RadioGroup
    android:id="@+id/radio_group"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:layout_below="@+id/sample”
    android:orientation="vertical" >
</RadioGroup>

</RelativeLayout>

【问题讨论】:

  • 您的列表视图中没有任何内容(至少不是我们在您的布局中看到的radiogroup),并且它的高度为 0。RadioGroup 本身不可滚动,如果您愿意,请将其放入 ScrollView让它变得简单。

标签: android android-listview


【解决方案1】:

您可以尝试像这样用&lt;ScrollView&gt; 包围&lt;RadioGroup&gt;,它应该向下/向上滚动,因为默认情况下 RadioGroup 本身不会滚动:

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

    <ListView
         android:id="@android:id/list"
         android:layout_width="match_parent"
         android:layout_height=“wrap_content” >
    </ListView>

    <TextView
         android:id="@+id/sample"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_gravity="center"
         android:layout_marginBottom="20dp"
         android:layout_marginTop="20dp"
         android:background="@color/black"
         android:gravity="center"
         android:text="@string/sample"
         android:textColor="@color/white" />

    <ScrollView 
         android:id="@+id/scrollview_radioGroup"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:fillViewport="true"
         android:layout_below="@id/sample">

        <RadioGroup
            android:id="@+id/radio_group"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:layout_below="@+id/sample”
            android:orientation="vertical" >
        </RadioGroup>
    </ScrollView>

</RelativeLayout>

【讨论】:

  • 另外,永远不要在 ListView 的高度上使用 wrap_content,除非你知道自己在做什么(我不相信你是这样的)。
  • 那么您建议将 ListView 的高度设置为什么?
  • 好吧,这段代码中的 ListView 似乎没有使用,我的评论更多是针对 OP。但是您的滚动视图是相同的,它不应该包含在内容上(因为它永远不会滚动)。它应该是 match_parent,或者遵守您对布局的任何约束(例如:在文本视图下方)
  • 三思而后行——listView 和 ScrollView 都将被动态填充,因此它们不是空的和未使用的。如果我们将 layout_height 设置为 match_parent,我们将不会再看到布局中的其他视图,因为特定视图将填满整个 Layout。你自己试过吗?
  • 关于高度,你要求滚动视图的最终高度被包裹在它的内容上,所以它没有理由让内容滚动。如果您想在布局中查看其他内容,则取决于您是否将约束置于以下/高于约束或任何适合您的需求。关于 listview 没用的事实:他的问题是,当他的组中有单选按钮时他的 listview 不滚动,而这些单选按钮不在列表中,这让我认为他对 listview 的工作原理有错误的想法,他在这里放了一个他的整个布局滚动(不起作用)
【解决方案2】:

根据您在另一篇文章中所写的内容和您的“滚动”问题,让我建议您一个布局:

“这就是我想要完成的。一个 textView 和下面的一个 radioButton 组,其中以编程方式添加了 radioButtons。我只能得到带有 radioButtons 的 radioButton 组,而不是 textView。radioButton 组占用了所有layout 和 textView 中的空间是不可见的。”

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

<TextView
     android:id="@+id/sample"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_gravity="center"
     android:layout_marginBottom="20dp"
     android:layout_marginTop="20dp"
     android:background="@color/black"
     android:gravity="center"
     android:text="@string/sample"
     android:textColor="@color/white" />

    <ScrollView 
     android:id="@+id/scrollview_radioGroup"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_below="@id/sample">

        <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical" >
        </RadioGroup>
    </ScrollView>

</RelativeLayout>

如果您的布局中只有这些元素,您甚至应该使用 LinearLayout 而不是您的亲戚。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    相关资源
    最近更新 更多