【问题标题】:Two ListViews in a vertical LinearLayout垂直 LinearLayout 中的两个 ListView
【发布时间】:2014-10-22 12:15:30
【问题描述】:

我需要在一个垂直的LinearLayout 中排列两个ListViews,这样第二个ListView 将位于第一个ListView 的最后一项的末尾。

如果 ListView1 为空,则视图应该是这样的。

随着 ListView1.size() 的增加,ListView2 应该被拉低到一个极限。

ListView1 达到固定高度(或重量),它应该停止扩展并开始滚动。

我认为android中没有方法为View设置maxHeight。

我已经尝试了几乎所有的 layout_height="wrap_content" 组合,两个 ListView 的固定高度和权重。在所有情况下, 第一个要求(image1)将失败:ListView1 不会在顶部 否则最后一个要求(image3) 将失败:ListView2 将被推出屏幕。

任何帮助或建议将不胜感激。提前致谢。

编辑:

我当前的代码是:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1" >

    <ListView
        android:id="@+id/myOrderListView"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="0.6" >
    </ListView>

    <ListView
        android:id="@+id/myCompletedOrderListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_weight="0.4" >
    </ListView>
</LinearLayout>

我尝试过不同的重量组合,例如:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1" >

    <ListView
        android:id="@+id/myOrderListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.6" >
    </ListView>

    <ListView
        android:id="@+id/myCompletedOrderListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_weight="0.4" >
    </ListView>
</LinearLayout>

但它会满足最后一个要求。

Edit2: @dangVarmit

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1" >

    <ListView
        android:id="@+id/myOrderListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

    <ListView
        android:id="@+id/myCompletedOrderListView"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginTop="20dp" >
    </ListView>

    <Space
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

如果你是这个意思,它仍然失败 req3。

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 反对票意味着你不知道?或者你不会帮忙?或者我不应该问?或者它是一个错误的问题?还是没有解决方案?要么 ?我花了两天多的时间,我找不到蚂蚁解决方案。
  • 尝试将您的第一个 xml 中的 android:layout_height 更改为 android:layout_height="0dp",就像您对第一个 ListView 所做的那样
  • @JuanAguilarGuisado 已经尝试过了(ListView2 的 layout_height="0dp")。但没有运气:-(
  • @Vijeesh 尝试这个哪个要求失败了?首先?我的意思是...如果它是第一个,您可以尝试将 ListView1 的可见性设置为 ListView.GONE 当它的大小为 0 时。这样做,它不会占用您的空间;)祝您好运!

标签: android android-layout listview android-listview android-linearlayout


【解决方案1】:

您可以找到以下左右列表视图的代码,您可以将其更改为上下列表视图或您想要显示的内容 main_activity.xml

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

    <LinearLayout
        android:id="@+id/main_top_bit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@color/orange">

            <LinearLayout android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:orientation="horizontal">

                <Button
                    android:id="@+id/button_1"
                    style="@style/landscape_top_button"
                    android:text="button1" />
                <Button
                    android:id="@+id/button_2"
                    style="@style/landscape_top_button"
                    android:text="button2"/>
            </LinearLayout>


        </LinearLayout>
        <View android:id="@+id/main_horizontal_bar"
            android:layout_height="3dip" android:layout_width="fill_parent"
            android:background="@color/gray" android:layout_marginTop="1dip" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lower_linear_layout"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/main_top_bit">

        <RelativeLayout
            android:layout_below="@+id/lower_linear_layout"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="150dip"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true">

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:background="@color/teal"
                android:id="@+id/org_left_linearlayout2">

                <Button
                    android:id="@+id/button_3"
                    style="@style/landscape_top_button"
                    android:text="button3"/>
                <Button
                    android:id="@+id/button_4"
                    style="@style/landscape_top_button"
                    android:text="button4" />
            </LinearLayout>

            <ListView
                android:background="@color/green"
                android:id="@+id/lstLeft"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/org_left_linearlayout2"
                android:layout_above="@+id/org_left_bottom_rl"
                android:cacheColorHint="#00000000"/>

            <RelativeLayout
                android:id="@+id/org_left_bottom_rl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:background="@color/purple" >

                <Button
                    android:text="button5"
                    android:id="@+id/button_5"
                    android:layout_alignParentLeft="true"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
                </Button>
            </RelativeLayout>
        </RelativeLayout>

        <View
            android:layout_width="3dip"
            android:layout_height="fill_parent"
            android:background="@color/gray">
        </View>

        <RelativeLayout
            android:layout_below="@+id/lower_linear_layout"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"

            android:layout_alignParentLeft="true">

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:background="@color/olive"
                android:id="@+id/org_right_linearlayout2">

                <Button
                    android:id="@+id/button_6"
                    style="@style/landscape_top_button"
                    android:text="button6" />
                <Button
                    android:id="@+id/button_7"
                    style="@style/landscape_top_button"
                    android:text="button7" />
            </LinearLayout>

            <ListView
                android:id="@+id/lstRight"
                android:background="@color/blue"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/org_right_linearlayout2"
                android:layout_above="@+id/org_right_bottom_rl"
                android:cacheColorHint="#00000000"/>

            <RelativeLayout
                android:id="@+id/org_right_bottom_rl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/red"
                android:layout_alignParentBottom="true">

                <Button
                    android:text="button8"
                    android:id="@+id/button_8"
                    android:layout_alignParentRight="true"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
                </Button>
            </RelativeLayout>
        </RelativeLayout>
    </LinearLayout>
</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {
    private final String TAG =  this.getClass().getName();
    ListView leftList;
    ListView rightList;


     static final String[] FRUITS = new String[] {"Apple", "Bananna", "Cherry", "Pear", "Strawberry"};
     static final String[] VEGETABLES = new String[] {"Asparagus", "Bamboo shoots", "Celery", "Ginger", "Spinach"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        leftList = (ListView) findViewById(R.id.lstLeft);
        rightList = (ListView) findViewById(R.id.lstRight);      

        populateLeftList();
        populateRightList();
    }

    public void populateLeftList(){
        leftList.setAdapter(new ArrayAdapter<string>(this, android.R.layout.simple_list_item_1, FRUITS));  
    }

    public void populateRightList(){
        rightList.setAdapter(new ArrayAdapter<string>(this, android.R.layout.simple_list_item_1, VEGETABLES));
    }
}

【讨论】:

  • 实现左右ListView很简单,因为一个ListView的高度不依赖于下一个ListView的高度。但就我而言,它是...... ;-(
  • 实际上,第二个 ListView 的位置取决于第一个 ListView 的高度。
【解决方案2】:
  • 对于 list1,layout_height=wrap_content,移除 layout_weight
  • 对于 list2,layout_height=100dp(任何固定值),移除 layout_weight
  • 在列表末尾添加

这应该会给你你正在寻找的结果。

【讨论】:

    【解决方案3】:

    我自己找到了一种解决方案。它可以按我的意愿完美运行。我不知道这是否是一个好方法。我正在寻找 xml 解决方案。

    getDeviceHeight() 是我以像素为单位查找设备高度的方法。 200 是 My ListViews 上方其他 ui 元素的近似高度。 myorderList 是我的第一个 ListView

    if(myorderList.getHeight() > ((getDeviceHeight()/2) - 200)) {
        LayoutParams params = myorderList.getLayoutParams();
        params.height = (int)((getDeviceHeight()/2) - 200);
        myorderList.setLayoutParams(params);
    }
    

    当第一个 ListView 发生任何类型的更改时,应执行此操作。

    编辑: 当物品在达到最大高度后被移除时,这也会失败:-(

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多