android - 在 Android 布局中显示 3 个等高的可滚动 ListView

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

我试图在一个屏幕上的 Android 布局中显示三个不同的垂直部分,每个占据屏幕的三分之一,一个在顶部,一个在中间,一个在底部。每个部分都有一个TextView 和一个ListViewListViews 可以滚动以便您可以看到所有项目,但整体页面不会移动.我尝试将每个 TextViewListView 放在 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_heightlayout_width 的值。如果您需要添加 TextView,则必须将此代码中的每个 ListView 设为 RelativeLayout LinearLayoutFrameLayout ,使用相同的宽度/高度/重量,并将 ListViewTextView 排列在里面品尝。要最有效地执行此操作,请使用 Framelayout 并在 ListView 上使用边距以使其与 TextView 偏移。您可以使用 TextView 中的 layout_gravityTextView 相对于 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/

相关文章:

android - 从 setOnItemClickListener 中的 ListView 项获取字符串

java - 在 Android 中动画旋转图像

android - volley.VolleyError.NetworkError 在 android 中到底是什么意思?

android - 使用应用名称获取包名称

java - 如何将带有 Imageview 和 TextView 的 RelativeLayout 转换为 PNG 图像?

安卓 : How to change name of the Enter key to "Search" with respect to particular EditText. ..?

java - Android ListView 点击时抛出 NullPointer 异常

android - 在暂停时停止线程

android - 使用样式设置 layout_width、layout_height

安卓 Ice Cream Sandwich : why ListView items remain selected by default?