How do I create a Textview on the left side and two buttons on the right side in one line?

In Android I will lay out how to create a TextView on the left and two buttons on the right side with the same line. What is the best layout for this layout, Table or Relative?

+3


source to share


3 answers


You can use LinearLayout to achieve this



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:layout_weight="1"
        android:text="a text"/>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button 1">
    </Button>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button 2">
    </Button>
</LinearLayout>

      

+1


source


Better to use Relative Layout for this kind of view. use android:layout_alignParentRight="true"

for right alignment and android:layout_alignParentLeft="true"

left alignment. eg



RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp" >

    <TextView
        android:id="@+id/type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Large Text"
        android:textColor="#76D4F7"
        android:textStyle="bold"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

      

+1


source


The simplest would be to use LinearLayout

(horizontal orientation) as pointed out in the previous answer. You can also set the property under Layout Layout 1 for each button and text box. Then set the layout width for all of these objects to 0. This will allow you to proportionally scale the amount of area occupied by each object horizontally. So, for example, if you set the weight of the layout in text mode to 2 and each button to 1, then the text image will take up 50% of the horizontal space (2 / (2 + 1 + 1)). This makes it easier to scale objects across devices and hopefully begins to decide which one is best to use in this case.

0


source







All Articles