Android: 在 LinearLayout 中右对齐 ImageView
Android: Right-Aligning ImageView in LinearLayout
imageView 在此线性布局中未右对齐(如果重要的话,此线性布局实际上是列表中的一行)。
但是如果我使用 TextView(它在下面的代码中有注释)而不是 ImageView,它会成功右对齐。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#FFFFAA"
android:orientation="horizontal" >
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textAppearance="?android:attr/textAppearanceMedium" />
<!--
<TextView
android:id="@+id/labelNext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="> "
android:textSize="20sp"
android:textStyle="bold">
</TextView>
-->
<ImageView
android:id="@+id/iconNext"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="right"
android:layout_weight="1"
android:gravity="right"
android:src="@drawable/right_arrow" >
</ImageView>
</LinearLayout>
如何让 imageView 位于 LinearLayout 行的最右边缘?
使用 RelativeLayout
而不是 LinearLayout
因为 LinearLayout
不支持它。
像这样使用:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#FFFFAA" >
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/iconNext"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="right"
android:layout_weight="1"
android:layout_alignParentRight="true"
android:src="@drawable/right_arrow" >
</ImageView>
</RelativeLayout>
或者您可以在 imageView 和 textView 之间添加空白视图,在它们之间填充空白 space,您的第二个文本视图将 "pushed" 到右侧:
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"/>
将宽度设置为 0 并将权重设置为 1 会导致此视图填充整个可用 space,其他视图不使用。所以它的大小将是
width = (screenWidth-(imageView width+textView width))
如果您只需要 TextView
且图像在右侧,请考虑使用 android:drawableRight
参数。 LinearLayout
与权重和 RelativeLayout
运行 测量两次所以只留下 TextView
将在计算上更快。
<TextView
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textAppearance="?android:attr/textAppearanceMedium"
android:drawableRight="@drawable/right_arrow" />
(另请注意ListView
中的箭头指向Android design guidelines)
imageView 在此线性布局中未右对齐(如果重要的话,此线性布局实际上是列表中的一行)。
但是如果我使用 TextView(它在下面的代码中有注释)而不是 ImageView,它会成功右对齐。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#FFFFAA"
android:orientation="horizontal" >
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textAppearance="?android:attr/textAppearanceMedium" />
<!--
<TextView
android:id="@+id/labelNext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="> "
android:textSize="20sp"
android:textStyle="bold">
</TextView>
-->
<ImageView
android:id="@+id/iconNext"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="right"
android:layout_weight="1"
android:gravity="right"
android:src="@drawable/right_arrow" >
</ImageView>
</LinearLayout>
如何让 imageView 位于 LinearLayout 行的最右边缘?
使用 RelativeLayout
而不是 LinearLayout
因为 LinearLayout
不支持它。
像这样使用:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#FFFFAA" >
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/iconNext"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="right"
android:layout_weight="1"
android:layout_alignParentRight="true"
android:src="@drawable/right_arrow" >
</ImageView>
</RelativeLayout>
或者您可以在 imageView 和 textView 之间添加空白视图,在它们之间填充空白 space,您的第二个文本视图将 "pushed" 到右侧:
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"/>
将宽度设置为 0 并将权重设置为 1 会导致此视图填充整个可用 space,其他视图不使用。所以它的大小将是
width = (screenWidth-(imageView width+textView width))
如果您只需要 TextView
且图像在右侧,请考虑使用 android:drawableRight
参数。 LinearLayout
与权重和 RelativeLayout
运行 测量两次所以只留下 TextView
将在计算上更快。
<TextView
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textAppearance="?android:attr/textAppearanceMedium"
android:drawableRight="@drawable/right_arrow" />
(另请注意ListView
中的箭头指向Android design guidelines)