我试图在一个屏幕上的 Android 布局中显示三个不同的垂直部分,每个占据屏幕的三分之一,一个在顶部,一个在中间,一个在底部。每个部分都有一个TextView
和一个ListView
,ListViews
可以滚动以便您可以看到所有项目,但整体页面不会移动.我尝试将每个 TextView
和 ListView
放在 LinearLayout
中,并将每个 LinearLayout
的高度设置为三分之一屏幕的总高度,但第一个 ListView
仅显示其中的所有项目,占据了屏幕的大部分,其他部分被向下推。我也尝试过使用 layout_weights,但由于某种原因它不起作用。 (编辑:设置 layout_weight="1"
最终起作用了,我不确定我第一次做错了什么)我怎样才能让它起作用,或者有更好的方法来解决这个问题?
最佳答案
<LinearLayout
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent" android:background="#FF0000"/>
<ListView android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent" android:background="#00FF00">
<ListView android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent" android:background="#0000FF">
</LinearLayout>
这将为您提供三个等宽的列:要使其成为行,将方向更改为垂直并交换每个 ListView 的 layout_height
和 layout_width
的值。如果您需要添加 TextView
,则必须将此代码中的每个 ListView 设为 RelativeLayout
LinearLayout
或 FrameLayout
,使用相同的宽度/高度/重量,并将 ListView
和 TextView
排列在里面品尝。要最有效地执行此操作,请使用 Framelayout
并在 ListView 上使用边距以使其与 TextView
偏移。您可以使用 TextView 中的
。layout_gravity
将 TextView
相对于 ListView
放置在 FrameLayout
中
即(交换第一个“列”):
<FrameLayout android:orientation="vertical" android:layout_width="0dp"
android:layout_weight="1" android:layout_height="match_parent"
android:background="#FF0000">
<TextView android:text="Column!" android:background="#3Eff0000"
android:layout_height="40dp" android:layout_width="match_parent">
<ListView android:layout_marginTop="48dp" android:layout_height="match_parent"
android:layout_width="match_parent" android:background="#8Aff0000"/>
</FrameLayout>
关于android - 在 Android 布局中显示 3 个等高的可滚动 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29378072/