简体   繁体   中英

How to layout view right aligned and bottom of an LinearLayout

I am trying to layout 1 textview (upText) left aligned and 1 textview (downText) and an image view (image) both on the same line and right aligned.

how can I do that? I tried that, but both 'textview' and image view at left aligned.

        <TextView
            android:id="@+id/uptext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="left"/>
        <TextView
            android:id="@+id/downtext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:gravity="right|bottom"/>
        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right|bottom"/>
    </LinearLayout>

Don't use a LinearLayout . Use a RelativeLayout , with

  • your first TextView set with android:layout_alignParentLeft="true"
  • your second TextView set with android:layout_alignParentBottom="true" and android:layout_alignParentRight="true"
  • something similar for your ImageView , which presently looks like it is going to overlap the second TextView

I realize this post is a bit old but just in case someone comes across this in their search for clarity;

The parent linear layout is where gravity needs to be specified for the child to align with the desired behavior which is why the above posts are explaining that linear layout is not possible for two separate behaviors to occur since a child cannot decide where to align itself within a linear layout .

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="200dp"
android:layout_height="50dp"
android:gravity="bottom|right">

<TextView
    android:layout_width="40dp"
    android:layout_height="20dp" 
    android:text="test"/></LinearLayout>

It should also be said that the parent linear layout must have a defined size and not be wrap-content or this will not work since wrap content implies that there will be no extra space in the layout for positioning, so at least 'match-parent' for width and height is necessary as well as having a parent with a greater size than wrap-content for the child linear layout itself.

Hope this helps.

Using RelativeLayout

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom">

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="hi"
        android:textStyle="bold|italic"
        android:gravity="right"/>
    <TextView
        android:id="@+id/text2"
        android:layout_below="@id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="All"
        android:gravity="right"/>

</RelativeLayout>

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