简体   繁体   中英

layout element control in Android

I want to layout 3 element Button , TextView , Button on the bar like this

[Button1] --- TextView --- [Button2]

2 button are always anchor fixed in left and right screen(padding 4dp) ,and TextView is center (change width depend on screen size),They should be apply for all screen size(not scaled in larger screen). How can I do it??

It would look something like this.-

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />

</RelativeLayout>

Notice that you still need to set other properties such as texts, ...

You can use LinearLayout with android:weightSum

Try Like this, By using android:weightSum , you can divide how much space for Button and textView.. And it will automatically adjust in all the density devices.

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

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_margin="4dp"
        android:layout_weight="3"
        android:text="LeftButton" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="4dp"
        android:layout_weight="4"
        android:gravity="center"
        android:text="Center TextView text" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_margin="4dp"
        android:layout_weight="3"
        android:text="Right Button" />

</LinearLayout>

android:weightSum对我来说工作正常,我解决了所有麻烦:)

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