将ImageView对齐它的右侧布局是LinearLayout

时间:2015-07-14 12:27:16

标签: android android-layout

我想将ImageView对齐到它的父布局的右侧(End),这是强制性的LinearLayout。也不建议在任何地方使用RelativeLayout。如下图所示,突出显示的图像应位于屏幕的右侧。并且不使用RelativeLayout。在此先感谢。

Image Here

下面是我的Activity_Main.xml代码

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="4"
    tools:context="com.PinchZoom.pinchzoomexampletwo.MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/img_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/img_back_description"
            android:src="@drawable/back_icon" />

        <TextView
            android:id="@+id/txt_app_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/txt_app_name" />

        <ImageView
            android:id="@+id/img_menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/img_menu_description"
            android:src="@drawable/menu_icon" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:orientation="horizontal"
        android:weightSum="2" >

        <com.PinchZoom.pinchzoomexampletwo.TouchImageView
            android:id="@+id/img_to_be_zoomed"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/img" >
        </com.PinchZoom.pinchzoomexampletwo.TouchImageView>

        <com.PinchZoom.pinchzoomexampletwo.TouchImageView
            android:id="@+id/img_to_be_zoomed_mirror"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/img" >
        </com.PinchZoom.pinchzoomexampletwo.TouchImageView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/txt_view_top" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.25"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_gallery"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_label_gallery" />
    </LinearLayout>

</LinearLayout>

5 个答案:

答案 0 :(得分:2)

假设您在水平布局中有三个视图(ImageView,TextView,ImageView),您可以使用以下代码:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/arrow"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="prova"
            android:textSize="30sp"
            android:layout_weight="1"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/three_dots"/>
</LinearLayout>

答案 1 :(得分:0)

您可以在LinearLayout中的ImageView之前插入一个不可见的视图,它将占用所有可用空间,如下所示:

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

答案 2 :(得分:0)

您可以像以下一样使用它:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="4"
    tools:context="com.PinchZoom.pinchzoomexampletwo.MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/img_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/img_back_description"
            android:src="@drawable/back_icon" />

        <TextView
            android:id="@+id/txt_app_name"
            android:layout_width="match_content"
            android:layout_height="wrap_parent"
            android:drawableRight="@drawable/menu_icon"
            android:text="@string/txt_app_name" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:orientation="horizontal"
        android:weightSum="2" >

        <com.PinchZoom.pinchzoomexampletwo.TouchImageView
            android:id="@+id/img_to_be_zoomed"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/img" >
        </com.PinchZoom.pinchzoomexampletwo.TouchImageView>

        <com.PinchZoom.pinchzoomexampletwo.TouchImageView
            android:id="@+id/img_to_be_zoomed_mirror"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/img" >
        </com.PinchZoom.pinchzoomexampletwo.TouchImageView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/txt_view_top" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.25"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_gallery"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_label_gallery" />
    </LinearLayout>

</LinearLayout>

答案 3 :(得分:0)

试试这个

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="4"
tools:context="com.PinchZoom.pinchzoomexampletwo.MainActivity" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/img_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/img_back_description"
        android:src="@drawable/back_icon" />

    <TextView
        android:id="@+id/txt_app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/txt_app_name" />
     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right">
               <ImageView
                  android:id="@+id/img_menu"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:contentDescription="@string/img_menu_description"
                  android:src="@drawable/menu_icon" />
       </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:orientation="horizontal"
    android:weightSum="2" >

    <com.PinchZoom.pinchzoomexampletwo.TouchImageView
        android:id="@+id/img_to_be_zoomed"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@drawable/img" >
    </com.PinchZoom.pinchzoomexampletwo.TouchImageView>

    <com.PinchZoom.pinchzoomexampletwo.TouchImageView
        android:id="@+id/img_to_be_zoomed_mirror"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@drawable/img" >
    </com.PinchZoom.pinchzoomexampletwo.TouchImageView>
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="@string/txt_view_top" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.25"
    android:gravity="center"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn_gallery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_label_gallery" />
</LinearLayout>

答案 4 :(得分:-2)

尝试这种方式并根据需要进行更改。 更改layout_weight,直到找不到您想要的内容。我希望这会对你有所帮助。

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/img_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:contentDescription="@string/img_back_description"
        android:src="@drawable/back_icon" />

    <TextView
        android:id="@+id/txt_app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/txt_app_name" />

    <ImageView
        android:id="@+id/img_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:contentDescription="@string/img_menu_description"
        android:src="@drawable/menu_icon" />
</LinearLayout>

快乐编码

相关问题