Domanda

I need to set two buttons side by side... I have used linearlayout for it... I have given each button to 0.5 layout_weight as i want each one to take equal space..I also make button to take equal height. The problem i am facing that left button will slide down if i give right button to match its height.

enter image description here

<LinearLayout
    android:id="@+id/buttonLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/botomMarginTextView"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/id1"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_marginLeft="20dp"
        android:layout_weight="0.5"
        android:background="@drawable/button_back"
        android:text="@string/name1" />

    <Button
        android:id="@+id/id2"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="20dp"
        android:layout_weight="0.5"
        android:background="@drawable/button_back"
        android:text="@string/name2" />
</LinearLayout>

Edit :

Please give left button longer text and right button smaller text..otherwise it is producing right layout

I am using @android:style/Theme.Black.NoTitleBar.Fullscreen . If i am going with normal theme then it is working fine

Now how to make this button keep side by side and also to make them of equal height?

È stato utile?

Soluzione

To android:layout_height attribute of LinearLayout, give value of 60dp

android:layout_height="60dp"

and to android:layout_height attribute of both Button, give value of match_parent

android:layout_height="match_parent"

So, updated XML snippet will be

<LinearLayout
    android:id="@+id/buttonLayout"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:layout_below="@id/botomMarginTextView"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/id1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp"
        android:layout_weight="0.5"
        android:background="@drawable/button_back"
        android:text="@string/name1" />

    <Button
        android:id="@+id/id2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="20dp"
        android:layout_weight="0.5"
        android:background="@drawable/button_back"
        android:text="@string/name2" />
</LinearLayout>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top