简体   繁体   中英

Align ImageView to right of it`s parent layout which is LinearLayout

I want to align a ImageView to right(End) side of it`s parent layout, Which is compulsory LinearLayout. It is also not recommended to use RelativeLayout anywhere. As shown in below Image, the highlighted image should be right side of the screen. And without using RelativeLayout. Thanks in Advance.

图片在这里

Below is My Activity_Main.xml code

    <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>

Supposing that you have in the horizontal layout three view (ImageView, TextView, ImageView), you can use the following code:

<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>

You can insert an invisible View right before your ImageView in LinearLayout that will take all available space like this:

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

You can use it like:

<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>

I have tried this code. Just change your textview txt_app_name with the following code.

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

Try this

 <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>

Try this way and change as you want to put that. change in layout_weight until you not get which you want. i hope this will help u more.

<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>

Happy Codeing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM