简体   繁体   中英

How to align 2 views to the left and one to the right in linear layout?

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

 <CheckBox
     android:id="@+id/checkBox2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Learning a lot" />

<TextView
    android:id="@+id/ctv2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="  ?" />

                <SeekBar
    android:id="@+id/seekBar2"
    android:layout_width="100dp"    
    android:layout_height="wrap_content" 
    android:gravity="right"/>

</LinearLayout>

Thats the code in xml. I want checkbox and textview next to each other left aligned and seekbar right aligned.I found topics similar to this but I wasn't able to found solution. hmmmmmmm ... Please help me ...

Try this:

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:orientation="horizontal" >
        <CheckBox
            android:id="@+id/checkBox2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Learning a lot" />

        <TextView
            android:id="@+id/ctv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="some text" />
    </LinearLayout>
    <SeekBar
        android:id="@+id/seekBar2"
        android:layout_width="100dp"    
        android:layout_height="wrap_content" 
        android:layout_gravity="right"/>
</LinearLayout>

This will align the first LinearLayout (containing CheckBox and TextView ) to Left, and SeekBar to right.

Hope this helps.

将android:layout_weight =“ 1”添加到TextView,它将告诉线性布局为textview提供任何额外的空间,这会将seekbar移动到右侧。

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