【问题标题】:how to make LinearLayout scrollable without using Scrollview如何在不使用 Scrollview 的情况下使 LinearLayout 可滚动
【发布时间】:2012-01-08 13:14:31
【问题描述】:
<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>

在我的应用程序中,我想在线性布局中创建两个列表视图,并使线性布局可滚动而不使用 ScrollView。据我阅读,滚动视图在使用列表视图时存在问题。这张图片不是我想要的,但让我们假设在 listview 的下方还有另一个 listview。所以我想显示列表视图的所有可用项目(在我的应用程序 2 列表视图的项目中)并使 LinearLayout 可滚动。 ScrollView 并不顺利,因为它必须只有一个直接子级。我找不到解决方案。所以请帮我解决,谢谢

【问题讨论】:

  • 我见过其他类似的问题,但不明白为什么人们要显示 ListView 并显示所有可用的列表项。 ListView(以及它的滚动能力)的全部意义在于它旨在适应屏幕(或部分屏幕),并且可以滚动以显示无法适应其占据的空间的其他项目。如果您想显示“项目”序列,只需为项目创建一个视图,然后动态创建多个项目,将它们动态插入ScrollView,然后忘记使用ListView
  • 推荐stackoverflow.com/a/17876855/336990 ...可能有用

标签: android


【解决方案1】:

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

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

我希望这个答案能帮助你解决你的问题。

【讨论】:

    【解决方案2】:

    使用 LinearLayout 作为 ScrollView 的子项,并将 ListView 作为子项放置到 LinearLayout。

    【讨论】:

    • 那不行,兄弟,我已经试过了。不过还是谢谢你的建议
    • 这种情况下,你可以试试这个问题:Multiple ListView issue
    【解决方案3】:

    我认为这是您的解决方案,

    您不需要在LinearLayout 中使用ScrollView

    LinearLayout 的子视图(在您的情况下为两个列表视图)中使用 layout_weight 参数

    由于layout_weight 参数在RelativeLayout 中不可用,您需要将其更改为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..我没有测试过,所以它们可能是一些你可以轻松解决的愚蠢的参数错误..

    【讨论】:

      【解决方案4】:

      我用过这个并帮助了我

      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();
      

      希望也能帮助你

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-09
        • 2011-05-02
        相关资源
        最近更新 更多