【发布时间】:2017-06-26 01:36:44
【问题描述】:
我正在尝试为我的列表项构建类似的布局,如上图所示。但是,我被卡住了,因为我不完全知道如何嵌套视图,以便销售按钮与两个文本视图具有相同的高度。
如果我使用RelativeLayout,那么我就不能再使用layout_weight 属性,它可以将视图水平地均匀地放置在屏幕上。
但是,如果我使用 LinearLayout,则无法使用 alignTop 等相对属性。我正在尝试以某种方式嵌套视图,以便实现这一目标,但是到目前为止我失败了......(对不起我糟糕的英语技能)
这是我目前所拥有的,但是我仍然无法得到想要的结果:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="80dp"
android:padding="16dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp">
<TextView
android:id="@+id/productname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Google Pixel" />
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/productname"
tools:text="699$" />
<TextView
android:id="@+id/quantity_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
tools:text="Quantity" />
<TextView
android:id="@+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/quantity_textview"
android:layout_centerHorizontal="true"
tools:text="31" />
</RelativeLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_alignParentRight="true"
tools:text="SELL"
android:layout_alignParentTop="true" />
</RelativeLayout>
【问题讨论】:
-
为什么不能使用
android:layout_height="match_parent"作为按钮?这将使按钮的高度等于父高度。 -
我不太明白你的意思......M
-
鉴于您有一个自定义行,它是一种布局(实际上不管是哪一个)。使您的按钮与行一样高可以解决您的问题。这是因为这两个 TextView 显然一个对齐到顶部边框,另一个对齐到行的底部边框。或者,至少,我会这样做。这真的很简单
-
我已经编辑了我的问题...也许我想得太复杂了..如果我使视图的边框可见而不是它们具有相同的高度,但是按钮顶部/底部边框不匹配textviews 的高度..它不是一条直线
-
请注意嵌套布局不利于性能。一个 RelativeLayout 会导致更快的运行。
标签: android user-interface android-linearlayout android-relativelayout