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

5
我正在查看http://bakhtiyor.com/2009/10/iphonish-tabs/上发布的示例,实际上他们在这里将单选按钮放置在Tabview的位置。
但问题是,我无法将图像设置为居中。它始终对齐到左侧。
这是截图。我希望我的视图应该像这样,但我想要图像位于中心。 enter image description here 这是我的布局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>

请给我建议

使用默认选项卡小部件后更新图像 enter image description here

是否可以在默认选项卡视图中自定义像上面的单选按钮?


你是想知道如何将自定义图像置于单选按钮中心,还是想问如何使TabView居中?我不太明白这个问题。 - TryTryAgain
不,我只是想让我的图片居中显示。 - Naruto
嗨@LLL,你最后能实现它吗? - abc cba
是的,我已将其标记为正确答案。 - Naruto
2个回答

3

并不是实际使图像居中,但是你可以让它看起来像是居中... 你可以修改图像的左侧留出空白,并将其立即裁剪掉。

编辑

我不知道你是否看过这些内容,但你让我好奇,于是我去找了几个链接,可能对你最终想要在底部创建选项卡有所帮助:

Niko's Blog

SO问题答案

另一个SO问题答案

祝你好运!

编辑2

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

具体代码在这里


Barak,谢谢您的回复。是的,我可以这样做,但除此之外还有其他选项吗? - Naruto
据我所知,这很容易吗?不是的。你可能需要深入研究Android源代码并像Longerian建议的那样创建自己的ImageButton,但每次我尝试这样做时都以失败告终。实际上,想一想,为什么要用单选按钮呢?为什么不用普通按钮呢? - Barak
实际上,我想为用户提供底部的选项卡界面,就像我在上面的图片中展示的那样,但默认的选项卡界面很难自定义。我在某个地方读到单选按钮可以使其变得简单。所以我正在尝试使用它们。请看一下这个链接:http://blog.mokriya.com/post/15342694933/custom-tabs-in-android。你知道有什么替代方案吗? - Naruto
我可以想象使用常规(图像)按钮来实现这一点。这可能会消除单选按钮对齐问题。我四处寻找并找到了一些链接到其他底部选项卡实现的链接,您可能已经看过或者还没有看过。我将它们添加到我的答案中。 - Barak
Barak,感谢您的帮助。我对您的回答感到非常高兴。我已经发布了使用默认选项卡环境创建的选项卡外观。但是最后一个问题是,是否可以将默认选项卡外观自定义为上面发布的RADIO按钮图像(即第一张图片)? - Naruto
谢谢Barak,非常感谢。我想这是最后的选择了...我会看看的,如果需要帮助会再问你...非常感谢你的帮助。 - Naruto

0

你可以定义一个继承自CompoundButton的自定义ImageButton。查看RadioButtonCompoundButton的源代码以了解如何实现。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接