Android相对布局 - 将图像对齐到右侧

时间:2014-03-17 10:59:55

标签: android android-layout

我想有以下布局:
enter image description here

布局由3个图像(Image1,Image2,Image3)和
组成 2 TextView(Text1和Text2)
Image2和Image3是可选的

我们的想法是使用具有以下约束的RelativeLayout:
- Text1位于image1的右侧 - Text2位于image1的右侧 - Text2低于Text1
- Image2位于Text1的右侧(并向右对齐)
- Image3位于Text2的右侧(并向右对齐)

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >

<ImageView
    android:id="@+id/Image1"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="6dip" />

<TextView
    android:id="@+id/Text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignWithParentIfMissing="true"
    android:layout_toRightOf="@id/Image1"
    android:textSize="16sp" />

<TextView
    android:id="@+id/Text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/Text1"
    android:layout_toRightOf="@id/image1"
    android:singleLine="true"
    android:textSize="12sp" />

<ImageView
    android:id="@+id/Image2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@id/Text1" />

<ImageView
    android:id="@+id/Image3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/Image2"
    android:layout_toRightOf="@id/Text2" />

</RelativeLayout>

问题
至于现在,我无法将Image2和Image3对齐到右边 如何控制Image1和Image2的大小?

3 个答案:

答案 0 :(得分:5)

添加这些行

android:adjustViewBounds="true"
android:scaleType="fitEnd"

到右边的图像,Image2和Image3。

答案 1 :(得分:4)

使用

android:alignParentRight="true"

用于image2和image3

答案 2 :(得分:0)

以下是实现上述目标的完整代码。

enter image description here

相应地更改Id和背景颜色。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/rghtImgOne"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:layout_alignParentRight="true"
        android:background="#0000FF" />

    <ImageView
        android:id="@+id/rghtImgTwo"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:layout_alignParentRight="true"
        android:layout_below="@id/rghtImgOne"
        android:background="#FF0000" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="100dip"
        android:layout_height="100dip"
        android:background="#00FFFF" />

    <TextView
        android:id="@+id/txtOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:layout_toRightOf="@id/imageView"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/txtTwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtOne"
        android:layout_margin="10dip"
        android:layout_toRightOf="@id/imageView"
        android:text="@string/hello_world" />

</RelativeLayout>

快乐的编码。

相关问题