Center two buttons horizontally

后端 未结 18 1137
我在风中等你
我在风中等你 2020-12-12 13:32

I try to arrange two buttons (with images on them which work fine) next to each other and to center them horizontally. That\'s what I have so far:



        
相关标签:
18条回答
  • 2020-12-12 14:14

    If you re searching for a fast fully RelativeLayout solution, the following works well. The dummy View element is invisible and centered horizontal. Both buttons are aligned either left or right.

    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.be.android.stopwatch"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center_horizontal">
    
    <View android:id="@+id/dummy"  android:visibility="visible" android:layout_height="0dip" android:layout_width="1dip" android:background="#FFFFFF" android:layout_centerHorizontal="true"/>
    
    <ImageButton android:id="@+id/button1" android:layout_height="25dip" android:layout_alignTop="@+id/dummy" android:layout_toLeftOf="@+id/dummy" android:layout_width="50dip"/>
    
    <ImageButton android:id="@+id/button2" android:layout_height="25dip" android:layout_alignTop="@+id/dummy"  android:layout_toRightOf="@+id/dummy"   android:layout_width="50dip"/>
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-12 14:15

    This is my solution:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">
    
        <TextView
            android:text="@+id/SomeText"
            android:id="@+id/TextView01"
            android:layout_width="wrap_content" android:layout_height="wrap_content" />
    
        <LinearLayout
            android:orientation="horizontal"
            android:background="@android:drawable/bottom_bar"
            android:paddingLeft="4.0dip"
            android:paddingTop="5.0dip"
            android:paddingRight="4.0dip"
            android:paddingBottom="1.0dip"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:layout_below="@+id/TextView01">
    
            <Button
                android:id="@+id/allow"
                android:layout_width="0.0dip" android:layout_height="fill_parent"
                android:text="Allow"
                android:layout_weight="1.0" />
    
            <Button
                android:id="@+id/deny"
                android:layout_width="0.0dip" android:layout_height="fill_parent"
                android:text="Deny"
                android:layout_weight="1.0" />
    
        </LinearLayout>
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-12 14:17

    Attributes named android:layout_foo are LayoutParams - arguments to the View's parent. Try setting android:gravity="center" on the LinearLayout instead of android:layout_gravity. One affects how the LinearLayout will lay out its children within itself, the other affects how the LinearLayout's parent should treat it. You want the former.

    0 讨论(0)
  • 2020-12-12 14:17

    this work for me. simple and easy.

    <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        >
                            <RelativeLayout
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content">
                                    <Button
                                        android:id="@+id/btn_one"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:text="Button One"
                                        />
                                    <Button
                                        android:id="@+id/btn_two"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:text="Button Two"
                                        android:layout_toRightOf="@id/btn_one"
                                        />
                            </RelativeLayout>
                    </LinearLayout>
            </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-12 14:21

    I feel your pain, I had to adjust things a bit and got this one to work.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
     <Button 
         android:id="@+id/Button01"
         android:layout_alignParentLeft="true"
         android:layout_width="75dp"
         android:layout_height="40dp"
         android:layout_marginTop="15dp"
         android:layout_marginLeft="60dp"
         android:text="No" />
    
     <Button 
         android:id="@+id/Button02"
         android:layout_alignParentRight="true"
         android:layout_width="75dp"
         android:layout_height="40dp"
         android:layout_marginTop="15dp"
         android:layout_marginRight="60dp"
         android:text="Yes" />
    
     <TextView
        android:id="@+id/centerPoint"
        android:layout_toRightOf="@id/Button01"
        android:layout_toLeftOf="@id/Button02"
        android:text=""
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        />
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-12 14:22

    I've wrapped two horizontal LinearLayout's as follows:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
    

    your UI widgets here

    </LinearLayout> </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题