【发布时间】:2016-08-19 08:43:56
【问题描述】:
我有以下问题。我的列表视图元素由两个排成一行的按钮组成:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp">
<Button
android:id="@+id/time_field"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/task_field"
android:layout_alignTop="@+id/task_field"
android:layout_centerVertical="true"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:clickable="false"
android:enabled="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:longClickable="false"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:textColor="@color/black_col" />
<Button
android:id="@+id/task_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/time_field"
android:clickable="false"
android:enabled="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center|start"
android:longClickable="false"
android:paddingLeft="10dp"
android:paddingRight="10dp"/>
所以在应用程序中发生的情况是,第二个(右)按钮被(由用户)填充的文本可以是 2 行、3 行甚至更多行,而左按钮中的文本总是单行。所以,我想要的是让第一个(左)按钮的高度始终与右一个按钮的高度相同,这取决于文本行的数量。左侧按钮中的文本行也应该在中心。
现在我只能制作等高的按钮(使用 layout_alignBottom 和 layout_alignTop),但左侧按钮中的文本始终位于顶部。
据我了解,这是由于在设置高度后完成列表视图项的渲染而发生的,但我不知道如何克服这个问题。我试图在我的列表视图适配器中以编程方式将左按钮高度设置为等于右按钮的高度,但它仅在我点击列表视图项而不是在填充或滚动列表时才有帮助!
请帮助我克服这个问题!谢谢!
【问题讨论】:
-
还可以尝试设置
layout_height=match_parent而不是wrap_content,那么它将尊重您设置的上下边界并确保time_field按钮占用与task_field按钮。
标签: android listview android-adapter