如何对齐屏幕中央的单选按钮

Nar*_*uto 14 tabs android

我在我的应用程序中使用单选按钮作为选项卡.

我为它加载了图像,但是它们朝向左侧对齐.如何使它们在中心对齐.

在此输入图像描述

这就是我的XML文件的外观

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@android:id/tabhost" 
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">

    <FrameLayout android:id="@android:id/tabcontent"
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"
      android:layout_weight="1" 
      android:padding="20dip" 
      android:background="#fff"/>

    <RadioGroup android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:orientation="horizontal"
      android:checkedButton="@+id/allcontacts"
      android:id="@+id/contactgroup">


      <RadioButton android:id="@+id/allcontacts" 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center" 
      android:layout_weight="1"/>

      <RadioButton
          android:id="@+id/categories"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:layout_weight="1"/>

      <RadioButton android:id="@+id/favourites" 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center" 
      android:layout_weight="1" />

    </RadioGroup>

    <TabWidget android:id="@android:id/tabs"
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"
      android:layout_weight="0" android:visibility="gone" />
  </LinearLayout>
</TabHost>
Run Code Online (Sandbox Code Playgroud)

Vin*_*ing 26

我以前遇到过类似的问题,我想出来了,所以我试着为你解释一下.

运行时截图

上面是我的应用程序截图,你可以看到,我也做了同样的事情,我有一个菜单对齐屏幕底部,它们是一个RadioGroup和里面的三个RadionButton,我希望每个菜单图标对齐RadionButton的中心,阴影图像(当按钮检查事件触发时,9-patch)一起显示.
起初,我使用android:button属性指的是一个drawable选择器,选择器在check和uncheck之间有两个可绘制状态,但我无法对齐菜单图标中心并使RadionButton填充阴影图像,它们看起来像:

在此输入图像描述

我尝试包括android:layout_gravity = centerandroid:gravity = center与RadionButton,但它也没有生效.之后,我的同事告诉我android:button属性是指前景而不是背景,然后我使用android:background而不是android:按钮它工作,下面是我的代码:

<RadioGroup android:layout_width="match_parent" android:layout_height="60dp"
        android:orientation="horizontal" android:background="@drawable/menu_bg">

    <RadioButton android:layout_width="0dp" android:layout_height="match_parent"
                 android:layout_weight="1" android:button="@null" android:checked="true"
                 android:background="@drawable/menu_item_selector" />

    <RadioButton android:layout_width="0dp" android:layout_height="match_parent"
                 android:layout_weight="1" android:button="@null"
                 android:background="@drawable/menu_item_selector" />

    <RadioButton android:layout_width="0dp" android:layout_height="match_parent"
                 android:layout_weight="1" android:button="@null"
                 android:background="@drawable/menu_item_selector" />

</RadioGroup>
Run Code Online (Sandbox Code Playgroud)

menu_item_selector是因为它有重要的事情重力设置:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/menu_item_layer_list" android:state_checked="true" />
    <item android:drawable="@drawable/menu_item_layer_list" android:state_pressed="true" />
    <item>
        <bitmap android:src="@drawable/menu_item_off" android:gravity="center" />
    </item>
</selector>
Run Code Online (Sandbox Code Playgroud)

因为当用户转动按钮状态被检查时,我有两个图像被绘制,阴影背景图像和图标活动状态图像,所以我使用图层列表来实现它,活动图标也有重力设置,下面是menu_item_layer_list代码:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/menu_bg_pressed" />
    <item>
        <bitmap android:src="@drawable/menu_item_on" android:gravity="center" />
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

我不知道解决方案是否完美,因为我总是把它作为一个视图来实现并绘制自己.但我认为Android SDK提供了更方便的组件让我们完成xml配置工作,我们应该学习和使用它.

  • 迄今为止最好的解决方案!尤其是&lt;item /&gt;标记中的android:gravity =“ center”对于您的选择器而言,这对我来说真是新事物!竖起大拇指! (2认同)

Bol*_*ing 17

您可以使用空视图在单选按钮之间均匀填充空间.在这种情况下,我有3个单选按钮,看起来居中,漂亮和整洁.

<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>
Run Code Online (Sandbox Code Playgroud)


Bar*_*rak 11

尝试android:gravity="center"使用每个RadioButton的xml.

我看到你是wrap_content,所以理论上它不应该做任何事情,但看起来图标是均匀划分的,也许RadioGroup均匀划分空间,所以使用android:gravity会有效果.

我猜是值得一试.

编辑

试试这个:

  <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"/>
Run Code Online (Sandbox Code Playgroud)

你将不得不玩android:marginLeft="20dp"它以使其正确居中,但这将让你做你想做的事情.我复制了你的布局并进行了测试,所以我知道它有效!:)