简体   繁体   中英

How to align a text layout to the left and an image to the right in android

I'm trying to align my arrow to the very right of the screen but for some reason the arrow places itself to much to the left for my liking. I have looked at plenty of other examples but for some reason they aren't working.

在此处输入图片说明

    <LinearLayout
        android:id="@+id/list_item3"
        android:layout_width="match_parent"
        android:layout_height="42dp"
        android:layout_below="@+id/list_item"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:background="#DCDBDB" >

                <ImageView
                    android:layout_width="33dp"
                    android:layout_height="match_parent"
                    android:layout_marginRight="15dp"
                    android:src="@drawable/quotedevil" />

 <TextView
    android:id="@+id/TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="21sp" 
    android:text="@string/quoteDevil"/>

<ImageView
    android:layout_width="51dp"
    android:layout_height="20dp"
    android:layout_alignParentRight="true"
    android:src="@drawable/nextarrow" />

            <!-- Put line under text -->


            </LinearLayout>

The layout_alignParentRight doesn't work in a LinearLayout it's a RelativeLayout parameter. So you could switch to a RelativeLayout but the easier solution is to make your TextView fill the available space:

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

PS: Not directly related but Android Studio warns you about stuff like this (only mention it because of the Eclipse tag in the OP :))

Android Studio摇滚!

Also the android:layout_centerHorizontal and android:layout_centerInParent doesnt work for LinearLayout. Try with this:

<LinearLayout
    android:id="@+id/list_item3"
    android:layout_width="match_parent"
    android:layout_height="42dp"
    android:layout_below="@+id/list_item"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:background="#DCDBDB" >

            <ImageView
                android:layout_width="33dp"
                android:layout_height="match_parent"
                android:layout_marginRight="15dp"
                android:src="@drawable/quotedevil" />

    <TextView
            android:id="@+id/TextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="21sp" 
            android:layout_marginRight="15dp"
            android:text="@string/quoteDevil"/>

          <ImageView
            android:layout_width="51dp"
            android:layout_height="20dp"
            android:layout_gravity="right"
            android:src="@drawable/nextarrow" />

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