如何在单选按钮的中心绘制图像

Nar*_*uto 5 android

我正在查看http://bakhtiyor.com/2009/10/iphonish-tabs/上发布的示例,实际上他们已将Radio按钮替换为Tabview.

但问题是,我无法将图像设置为中心.它始终与左对齐.

这是截图.我希望我的视图看起来像这样,但我希望图像在中心. 在此输入图像描述

这就是我的布局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"/>

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


      <RadioButton android:id="@+id/allcontacts" 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:orientation="vertical"
      android:background="@drawable/button_radio"/>


      <RadioButton
          android:id="@+id/categories"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:background="@drawable/button_radio"
          android:orientation="vertical"
          android:layout_gravity="center|right"/>

      <RadioButton android:id="@+id/favourites" 
   android:layout_width="fill_parent"
          android:layout_height="wrap_content"

    android:gravity="center"
    android:orientation="vertical"
      android:layout_weight="1" 
      android:background="@drawable/button_radio"/>

    </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)

请指教

使用"默认"选项卡小部件后更新的图像 在此输入图像描述

是否可以在默认的tabview中自定义上面的单选按钮.

Bar*_*rak 3

实际上并没有使图像居中,但您可以使其看起来像这样...您可以修改图像以在左侧有空白区域并立即在图像右侧剪切。

编辑

我不知道你是否看过其中任何一个,但你让我很好奇,所以我去寻找并发现了几个链接,这些链接可能对你实现底部选项卡的最终目标有帮助:

尼科的博客

所以问题解答

另一个SO问题答案

祝你好运!

编辑2

好的,找到了。您必须从 RadioButton 扩展一个新类并重写 onDraw(),然后创建一个 attrs.xml 文件,您需要将其放入 res/values 中,以便代码可以获取平台定义的属性。

具体代码看这里