在android中以线性布局将元素放置在右侧

时间:2012-02-15 22:56:41

标签: android alignment android-linearlayout

我想在视图右侧放置一个图像。为此,我想使用像

这样的东西
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" >

...some other elements

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

    <ImageView
        android:id="@+id/imageIcon"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"            
        android:layout_gravity="right"
        android:src="@drawable/image_icon" >
    </ImageView>
</LinearLayout>

...
</RelativeLayout>

以上似乎把图像放在了中心位置。无论我们处于纵向还是横向模式,如何确保图像右对齐?

感谢名单!

3 个答案:

答案 0 :(得分:6)

以下似乎有效。两个变化 1.使用Zortkun建议的orientation = vertical 2.在layout_width中使用wrap_content。

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/settingsIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:src="@drawable/settings_icon" >
    </ImageView>
</LinearLayout>

答案 1 :(得分:0)

LinearLayout的默认方向是水平的。确保线性布局的方向设置为垂直,否则您无法水平对齐。

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

答案 2 :(得分:0)

如果在Linearlayout中仅使用一个视图,则不需要Linearlayout。

无论如何,

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

//...some other elements

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="right">

    <ImageView
        android:id="@+id/imageIcon"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"            
        android:layout_gravity="right"
        android:src="@drawable/image_icon" >
    </ImageView>
</LinearLayout>

</RelativeLayout>