如何设置原始比例的自定义单选按钮android的背景图像?

时间:2014-01-28 09:27:58

标签: android background radio-button scale android-button

我需要将时间轴栏实现为附加图像

  

enter image description here

我尝试使用自定义单选按钮绘制每个节点: 布局xml 文件:

<LinearLayout 
android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:gravity="center"
    android:paddingLeft="50dp"
    android:paddingRight="50dp"
    >
    <RadioGroup 
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       android:layout_gravity="center"
       android:gravity="center"
       android:checkedButton="@+id/first">
       <RadioButton android:id="@+id/first"
          style="@style/timeLineRadioButton"/>
       <RadioButton android:id="@+id/second"
          style="@style/timeLineRadioButton"/>
       <RadioButton android:id="@+id/third"
          style="@style/timeLineRadioButton"/>
       <RadioButton android:id="@+id/fourth"
          style="@style/timeLineRadioButton"/>
    </RadioGroup>
</LinearLayout>

RES /值/ style.xml

<style name="timeLineRadioButton">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_weight">1</item>
    <item name="android:gravity">top|center</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:button">@null</item>
    <item name="android:background">@drawable/button_radio</item>
    <item name="android:scaleType">matrix</item>
</style>

res / drawable / button_radio.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_checked="true" android:state_pressed="false"
      android:drawable="@drawable/radio_button_pressed"/>
  <item android:state_checked="false" android:state_pressed="false"
      android:drawable="@drawable/radio_button"/>
  <item android:state_checked="true" android:state_pressed="true"
      android:drawable="@drawable/radio_button_pressed"/>
  <item android:state_checked="false" android:state_pressed="true"
      android:drawable="@drawable/radio_button"/>
</selector>

时间线显示确定,但背景是不同比例的比例 https://dl.dropboxusercontent.com/u/5860710/timeline.jpg

所以,请帮我修复原始比例尺的背景图像

1 个答案:

答案 0 :(得分:3)

根据你的代码,我想你想要实现类似scaleType="center"的ImageView,但不幸的是,RadioButton没有类似的东西,但我认为这个解决方案可以帮到你。< / p>

首先,在这种情况下不要使用weightSum/layout_weight,只需为RadioButtons设置android:layout_width="wrap_content"

秒,更新您的选择器(请注意,主要内容是使用bitmap属性gravity的标记,此代码我将其设置为center

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_checked="true" android:state_pressed="false">
        <bitmap android:src="@drawable/radio_button_pressed" android:gravity="center" />
  </item>
  <item android:state_checked="false" android:state_pressed="false">
        <bitmap android:src="@drawable/radio_button" android:gravity="center" />
  </item>
  <item android:state_checked="true" android:state_pressed="true">
        <bitmap android:src="@drawable/radio_button_pressed" android:gravity="center" />
  </item>
  <item android:state_checked="false" android:state_pressed="true">
        <bitmap android:src="@drawable/radio_button" android:gravity="center" />
  </item>
</selector>

结果是你的RadioButton的背景将表现得像scaleType="center"

希望这有帮助!