简体   繁体   中英

Right-align imageView of a LinearLayout

How do I rightalign an imageview in a linearlayout - that is put the imageview to the right side of the linearlayout parent? android:layout_alignparent is not allowed in a LinearLayout. And I have tried with different gravity-constants.

This is a depiction of the screen and where I want to position the imageview. Its assumed that the parent is a linearlayout and orients its child in a horizontal order.

______________________
|                    |                  
|                    |
|                   X|
|                    |
|                    |
|                    |
|                    |
|                    |
|                    |
|                    |
|                    |
|                    |    
|                    |
|                    |
|                    |
|____________________|


<LinearLayout 
        android:orientation="horizontal" 
        android:layout_width="match_parent"
        android:layout_marginTop="10dp"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <ImageView 
            android:id="@+id/dateImg"
            android:layout_width="60dp"
            android:layout_height="64dp"
            android:layout_marginLeft="8dp"
            android:layout_gravity="end"
            android:src="@drawable/unchecked"/>
</LinearLayout>

Why can't you use a relative layout instead ?

Or in case you must use a linear layout , add another view before the image view like this -

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent"
    android:layout_marginTop="10dp"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <View
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"/>
    <ImageView 
        android:id="@+id/dateImg"
        android:layout_width="60dp"
        android:layout_height="64dp"
        android:layout_marginLeft="8dp"
        android:layout_gravity="end"
        android:src="@drawable/unchecked"/>

enter code here

Change orientation to "vertical":

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_marginTop="10dp"
          android:layout_height="wrap_content" >

<ImageView
        android:layout_width="60dp"
        android:layout_height="64dp"
        android:layout_marginLeft="8dp"
        android:layout_gravity="end"
        android:background="#000000">

</ImageView>

Can you try this

 <LinearLayout 
            android:orientation="horizontal" 
            android:layout_width="match_parent"
            android:layout_marginTop="10dp"
            android:layout_height="0dp"
            android:layout_weight="1" >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2">
         <ImageView 
             android:id="@+id/dateImg"
             android:layout_width="60dp"
             android:layout_height="64dp"
             android:layout_marginLeft="8dp"
             android:alignParentRight="true"
             android:src="@drawable/unchecked"/>
    </RelativeLayout>
</LinearLayout >

Have you tried android:gravity="right"

Anyways in my opinion RelativeLayout is better for stuff like that :)

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