我有两个 TextViews 在“行”中对齐,使用以下布局:
<RelativeLayout
android:id="@+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp">
<TextView
android:id="@+id/big_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="21sp"/>
<TextView
android:id="@+id/small_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
但是,由于左边的比右边的大,结果是这样的(图片只是为了说明):
相反,这就是我要找的:
尝试将 gravity="bottom"添加到小 TextView,但没有成功。 还尝试将小 TextView 包裹在另一个布局中,将其布局高度设置为 match_parent(因此它将采用大 TextView 的高度),然后将小 TextView 重力设置为底部,但它也没有用。
编辑: plot twist:如何将它对齐到大 TextView 的中心?
最佳答案
将此行添加到较小的 textview
android:layout_alignBaseline="@+id/big_text"
或
android:layout_alignBottom="@+id/big_text"
像这样:
<RelativeLayout
android:id="@+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp">
<TextView
android:id="@+id/big_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="21sp"/>
<TextView
android:id="@+id/small_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_alignBaseline="@+id/big_text"
android:layout_alignParentRight="true"/>
</RelativeLayout>
关于java - 在 Android 中对齐两个 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32590748/