How to align Radio button at the center of the screen

前端 未结 11 1319
误落风尘
误落风尘 2021-01-04 02:13

I am using Radio buttons as tab in my application.

I have loaded images for it, but they are aligning towards left side. how to make them align at center.

相关标签:
11条回答
  • 2021-01-04 02:48

    Try adding an android:layout_gravity="center_horizontal"on your RadioGroup.

    0 讨论(0)
  • 2021-01-04 02:49

    I tried all of the above and it doesn't work.

    I had found a better solution which i would like to share. Set your button to @null ; then add the drawable to android:drawableTop or any other type of position. This will enable us to set the drawables to other position of the radioButton. add some padding or drawablePaddingso that it will not stick to any side of the button/text.

    android:background="@drawable/background_paint"
    android:drawableTop="@drawable/shape"
    android:button="@null"
    
    0 讨论(0)
  • 2021-01-04 02:50

    You can use empty views to fill upp the space evenly between the radio buttons. In this case I have 3 radio buttons that vill look centered, nice and tidy.

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    
        <View
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    
        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="" />
    
        <View
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    
        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:checked="true"
            android:gravity="center"
            android:text="" />
    
        <View
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    
        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="" />
    
        <View
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </RadioGroup>
    
    0 讨论(0)
  • 2021-01-04 02:51

    Try android:gravity="center" in the xml for each RadioButton.

    I see you're wrap_content, so theoretically that shouldn't do anything, but it looks like the icons are evenly divided, maybe the RadioGroup divides the space evenly so using android:gravity will have an effect.

    Worth a shot I guess.

    EDIT

    Try this:

      <RadioButton android:id="@+id/allcontacts" 
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:marginLeft="20dp" 
      android:gravity="center"
      android:layout_weight="1"/>
    

    You will have to play with the android:marginLeft="20dp" to get it properly centered, but that'll let you do what you're looking to do. I copied your layout and tested this time, so I know it works! :)

    0 讨论(0)
  • 2021-01-04 03:00

    i used this line in my RadioGroup and it is working fine!

    android:gravity="center|left" 
    
    0 讨论(0)
  • 2021-01-04 03:01

    in my case, my radio button contents were aligned right. I let them aligned center by set android:background be some value, such as @null or some color. below were my layout

    <LinearLayout
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >
    
        <RadioGroup
            android:paddingTop="5dp"
            android:id="@+id/radioGroupAsTab"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_gravity="center_vertical|center_horizontal"
            android:gravity="center_vertical|center_horizontal"
            android:background="@color/v3_header_bg"
            android:fadingEdge="none"
            android:orientation="horizontal" >
    
            <RadioButton
                android:id="@+id/rbDiagnose"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@null"
                android:button="@null"
                android:checked="true"
                android:drawableTop="@drawable/v3_tab_diagnose"
                android:layout_gravity="center_vertical|center_horizontal"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/tabCaption_health_maintenance"
                android:textColor="@color/radio_astab_changecolor"
                android:textSize="10sp"
                android:textStyle="bold" />
    
            <RadioButton
                android:id="@+id/rbHistory"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@null"
                android:button="@null"
                android:drawableTop="@drawable/v3_tab_history"
                android:layout_gravity="center_vertical|center_horizontal"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/tabCaption_history"
                android:textColor="@color/radio_astab_changecolor"
                android:textSize="10sp"
                android:textStyle="bold" />
    
            <RadioButton
                android:id="@+id/rbChart"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@null"
                android:button="@null"
                android:drawableTop="@drawable/v3_tab_chart"
                android:layout_gravity="center_vertical|center_horizontal"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/tabCaption_graph"
                android:textColor="@color/radio_astab_changecolor"
                android:textSize="10sp"
                android:textStyle="bold" />
    
            <RadioButton
                android:id="@+id/rbCyclopedia"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@null"
                android:button="@null"
                android:drawableTop="@drawable/v3_tab_encyclopedia"
                android:layout_gravity="center_vertical|center_horizontal"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/tabCaption_encyclopedia"
                android:textColor="@color/radio_astab_changecolor"
                android:textSize="10sp"
                android:textStyle="bold" />
    
            <RadioButton
                android:id="@+id/rbSetting"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@null"
                android:button="@null"
                android:drawableTop="@drawable/v3_tab_setting"
                android:layout_gravity="center_vertical|center_horizontal"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/tabCaption_info"
                android:textColor="@color/radio_astab_changecolor"
                android:textSize="10sp"
                android:textStyle="bold" />
        </RadioGroup>
    </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题