How to layout view right aligned and bottom of an LinearLayout

前端 未结 3 2364
离开以前
离开以前 2021-02-20 16:13

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

相关标签:
3条回答
  • 2021-02-20 16:29

    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
    0 讨论(0)
  • 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>
    
    0 讨论(0)
  • 2021-02-20 16:41

    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.

    0 讨论(0)
提交回复
热议问题