两个ListView并排

Sha*_*ful 8 android listview

我有两个ListView.我需要将它们水平并排放置.但问题是 - 只能看到一个列表.(注意:第二个列表位于布局的右侧,最多可以包含1个字符.第一个列表将展开以填充屏幕的其余部分.)

请帮帮我.

布局是这样的.

------------
|       |  |
|       |  |
|       |  |
|       |  |
|       |  |
|       |  |
|       |  |
|       |  |
|       |  |
------------
Run Code Online (Sandbox Code Playgroud)

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="vertical" >

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="true" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Shaiful

QED*_*QED 6

我无法相信三年内没有人能提出这个问题.也许其他人会有这个问题.

问题是ListView在水平空间方面很贪婪.你可以告诉它旱烟使用layout_width='0dp'和哪个layout_weight你觉得是最好的数字.下面是一个放置leftListView¾和rightListView¼ 的例子.我试过了,它工作正常.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="@+id/leftListView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight=".75" />

    <ListView
        android:id="@+id/rightListView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight=".25" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用相同的布局技巧,但使用FrameLayouts,然后为每个ListView创建片段.它比较重,但你可能会发现片段四处都有价值.


Sam*_*yay 5

我认为使用这段代码它会正常工作.你必须使用layout_weight = 0.5,事情将完美无缺.

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginBottom="60dp"
android:layout_marginTop="60dp"
android:orientation="horizontal"
android:id="@+id/linearLayout2">
    <ListView
        android:id="@+id/sportsList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/sports_array"
        android:layout_weight="0.5"/>

    <ListView
        android:id="@+id/sportsList_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/sports_array"
        android:layout_weight="0.5"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

因此,基本上在线性布局中使用两个列表视图,并将每个布局的权重赋予0.5.我觉得这有帮助.

  • @QED他的答案没有提到你必须将宽度(或高度取决于你的方向)设置为0px.这个问题是一个很大的遗漏. (2认同)

小智 0

尝试使用相对布局或表格布局作为根布局,然后将 2 个列表视图定位为根的子视图。