【问题标题】:How do I get a ListView beside a GridView如何在 GridView 旁边获取 ListView
【发布时间】:2014-07-17 02:20:39
【问题描述】:

我有一个ListView,我想将它显示在GridView 的左侧。我使用以下 xml 代码来尝试实现这一点:

<?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="vertical">

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/listView_items">
</ListView>
<GridView 
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id="@+id/gridView_board"
    android:layout_toRightOf="@id/listView_items">
</GridView>

</RelativeLayout>

有问题的是,只有ListView 在活动启动时出现。我意识到这很简单,但是我很久没有编码了,并且无法使用 Google 找到有用的信息。

谢谢!

【问题讨论】:

  • 你在 ListView 和 GridView 上都使用了 android:layout_width="match_parent" 这就是为什么 ListView 首先占据了父视图的所有宽度,即相对布局。尝试使用具有水平方向的 LinearLayout 并将 weightSum 设置为 2,然后将 GridView 和 ListView 的权重设置为 1,将 layout_width 设置为 0dp。这应该可以解决问题。
  • @KaHeL 确实做到了。谢谢!如果您想发表您的评论作为我可以接受的答案。

标签: android listview gridview layout


【解决方案1】:

您在ListViewGridView 上都使用了android:layout_width="match_parent",这就是为什么ListView 首先占据父视图的所有宽度,即RelativeLayout

RelativeLayout 更改为水平方向的LinearLayout,并将weightSum 设置为2,然后将GridViewListView 的权重设置为1,将layout_width 设置为0dp。

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

<ListView
    android:id="@+id/listView_items"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" >

</ListView>

<GridView
    android:id="@+id/gridView_board"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" >

</GridView>

</LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多