我有一个自定义的 ListItem 行,其顶部有一个 TextView,底部有一个 TextView。我想创建一个右对齐且垂直居中的按钮。我如何在 XML 中执行此操作?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/toptext"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:textSize="18sp" android:textColor="#FFFFFF"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="@+id/bottomtext"
android:singleLine="true"
android:ellipsize="marquee"
android:textSize="15sp" android:textColor="#FFFFFF"/>
</LinearLayout>
</LinearLayout>
最佳答案
使用android:gravity
或setGravity(int)
Specifies how to place the content of an object, both on the x- and y-axis, within the object itself.
http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:gravity
特别针对这种情况,right|center_vertical
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right|center_vertical">
<TextView
android:id="@+id/toptext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="text centered"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/bottomtext"
android:text="text to the left"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="the button is at the right"/>
</LinearLayout>
关于android - 如何使按钮在布局中右对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5891480/