【问题标题】:How to draw image at center of Radio button如何在单选按钮的中心绘制图像
【发布时间】:2012-05-06 04:41:57
【问题描述】:

我正在查看http://bakhtiyor.com/2009/10/iphonish-tabs/ 中发布的示例,实际上他们在这里放置了单选按钮作为 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>

请给我建议

使用默认选项卡小部件后更新的图像

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

【问题讨论】:

  • 您想知道如何在单选按钮的中心放置自定义图像,还是想知道如何使 TabView 居中?这个问题我不清楚。
  • 不,我希望我的图像位于中心,仅此而已,
  • 嗨@LLL,你最后能做到吗?
  • 是的,我已将其标记为正确答案

标签: android


【解决方案1】:

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

编辑

我不知道你是否看过这些,但你让我很好奇,所以我去找了几个链接,这些链接可能有助于你实现底部标签的最终目标:

Niko's Blog

SO Question Answer

Another SO Question Answer

祝你好运!

编辑 2

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

Specific code here

【讨论】:

  • 巴拉克,感谢您的回复。是的,我可以这样做,但除了 tat 还有其他选择吗?
  • 我知道的?容易地?不,您可能会按照 Longerian 的建议潜入 android 源代码并创建自己的 ImageButton,但每次我尝试这样的事情时,它都会以眼泪告终。实际上,仔细想想,为什么是单选按钮?为什么不是常规按钮?
  • 实际上我想在底部为用户提供选项卡式界面,如上图所示,但默认选项卡式界面很难自定义。我读了一些单选按钮使它变得简单的地方。所以我正在和他们一起努力。请看一下blog.mokriya.com/post/15342694933/custom-tabs-in-android。你知道任何替代方案吗?
  • 我可以设想一种使用常规(图像)按钮的方法。这可能会摆脱 RadioButton 对齐问题。我环顾四周,发现了一些您可能见过也可能没见过的底部选项卡的其他实现的链接。我将它们添加到我的答案中。
  • 巴拉克,谢谢你的帮助。我真的很高兴你的回答。我已经发布了使用默认选项卡环境创建的选项卡的外观。但最后一个疑问,是否可以将默认选项卡外观自定义为上面发布的 RADIO 按钮图像。 (即第一张图片)。
【解决方案2】:

您可以定义自己的扩展CompoundButton 的ImageButton。请参阅 RadioButtonCompoundButton 的源代码以了解如何操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 2021-02-03
    • 2021-06-17
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    • 2011-07-04
    相关资源
    最近更新 更多