Android:线安装中的右对齐图像视图



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>

,或者您可以在图像视图和文本视图之间添加空视图,这些视图填充它们之间的空空间,第二个文本视图将"按"向右侧:

 <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight="1"/>

将宽度设置为0,重量为1,导致此视图填充整个可用空间,而其他视图不使用。所以它的尺寸将是

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设计指南)

最新更新