如何在不使用ScrollView的情况下使LinearLayout可滚动

3
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/bag" 
    android:layout_height="fill_parent" android:layout_width="fill_parent"
    android:orientation="vertical" >

<RelativeLayout
        android:id="@+id/relativeLayout2"
            style="@style/relbag"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView style="@style/CodeFont" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="18dp"
        android:textSize="15dp"
        android:text="General Preference"
        android:id="@+id/genpref">
        </TextView>


    <ListView
        android:id="@+id/settingsListView1"
        style="@style/listbag"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="15dip"

        android:background="@drawable/radius"
        android:listSelector="@drawable/list_selector"
        android:paddingTop="8dip"
        android:paddingBottom="8dip" 
        android:scrollbars="none"
        android:layout_below="@id/genpref"/>

        <TextView style="@style/CodeFont" 
        android:id="@+id/notpref"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="25dp"
        android:textSize="15dp"
        android:text="Notification Preference"
        android:layout_below="@id/settingsListView1">
        </TextView>


        <ListView style="@style/listbag" android:id="@+id/settingsListView2" android:layout_width="fill_parent"
         android:layout_height="fill_parent"
            android:layout_marginLeft="15dip" android:layout_marginRight="15dip" 
              android:layout_marginBottom="15dip" 
        android:background="@drawable/radius" 
                android:paddingLeft="5dip" android:paddingRight="5dip" 
            android:paddingTop="15dip" android:paddingBottom="15dip" 
            android:listSelector="@drawable/list_selector" 
            android:layout_below="@id/notpref" 
            android:scrollbars="none"
            />
            </RelativeLayout>
</LinearLayout>


我看到过其他类似的问题,不明白为什么人们想要显示一个带有所有可用列表项的 ListViewListView 的整个意义(以及它的滚动能力)在于它应该适合屏幕(或其一部分),并且可以滚动以显示无法适应其占用空间的其他项目。如果您想显示一系列“项目”,只需为项目创建一个视图,然后动态地创建多个视图插入到 ScrollView 中,忘记使用 ListView 即可。 - Squonk
请参考网址 https://dev59.com/wXA75IYBdhLWcg3wOGLS#17876855 ...可能会有用。 - CoDe
4个回答

7
我认为在一个屏幕上放置多个列表不是一个好主意,因为触摸会变得奇怪,使用屏幕也会变得复杂。您应该考虑其他方法来在单个屏幕上显示多个列表。您可以在单个屏幕上水平排列多个列表。Romain Guy(Google开发人员)在以下链接中也接受这一事实...

http://groups.google.com/group/android-developers/browse_thread/thread/77acd4e54120b777

我希望这个答案能够帮助您解决问题。

0
使用LinearLayout作为ScrollView的子元素,并将ListView放置在LinearLayout中。

2
那行不通,兄弟。我之前已经试过了。但还是谢谢你的建议。 - fish40
在这种情况下,您可以尝试这个问题:Multiple ListView issue - SamSPICA

0

我想这里有你的解决方案,

LinearLayout 中,您不需要使用 ScrollView

在您的情况下,使用子视图(即两个 listview)的 layout_weight 参数。

由于 RelativeLayout 中没有 layout_weight 参数,因此您需要将其更改为 LinearLayout

您可以按照以下方式进行设置,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/bag" 
    android:layout_height="fill_parent" android:layout_width="fill_parent"
    android:orientation="vertical" >

<LinearLayout
        android:id="@+id/relativeLayout2"
            style="@style/relbag"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    android:orientation="vertical">

        <TextView style="@style/CodeFont" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="18dp"
        android:textSize="15dp"
        android:text="General Preference"
        android:id="@+id/genpref">
        </TextView>


    <ListView
        android:id="@+id/settingsListView1"
        style="@style/listbag"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="15dip"
        android:background="@drawable/radius"
        android:listSelector="@drawable/list_selector"
        android:paddingTop="8dip"
        android:paddingBottom="8dip"/>

        <TextView style="@style/CodeFont" 
        android:id="@+id/notpref"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="25dp"
        android:textSize="15dp"
        android:text="Notification Preference">
        </TextView>


        <ListView style="@style/listbag" 
        android:id="@+id/settingsListView2" 
        android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
                android:layout_marginLeft="15dip" 
        android:layout_marginRight="15dip" 
                android:layout_marginBottom="15dip" 
            android:background="@drawable/radius" 
                android:paddingLeft="5dip" android:paddingRight="5dip" 
                android:paddingTop="15dip" android:paddingBottom="15dip" 
                android:listSelector="@drawable/list_selector"/>

     </LinearLayout>
</LinearLayout>

使用上述方法更改你的XML...我还没有测试过,可能会有一些愚蠢的参数错误,但你很容易解决。


0

我使用了这个,帮助了我

ListAdapter listAdapter = listView.getAdapter();
int rows = listAdapter.getCount();
int height = 60 * rows; // Insert the general cell height plus the dividers.

ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = height;
listView.setLayoutParams(params);
listView.requestLayout();

希望对你也有帮助


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接