自定义单选按钮图像不填充空间

时间:2011-01-13 15:41:45

标签: android radio-button css-selectors

我有一个自定义单选按钮,背景为9补丁图像。我使用选择器来确定背景。 我还有一些文字我想放在图像的背景上,但是文字正在按钮旁边。

这是RadioGroup

<LinearLayout
    android:id="@+id/segmented"
    android:layout_width="fill_parent" 
    android:layout_height="50sp"
    android:gravity="center"
    android:layout_below="@+id/header">
    <RadioGroup android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal"
        android:id="@+id/group1"
        android:gravity="center">
        <RadioButton 
            android:checked="false"
            android:layout_width="90sp"
            android:id="@+id/rbVerzekeringen"
            android:text="Verzekeringen"
            android:textSize="10sp"
            android:button="@drawable/checkbox_theme" />
        <RadioButton 
            android:checked="false"
            android:layout_width="90sp"
            android:id="@+id/rbPersoonlijk"
            android:text="Persoonlijk"
            android:textSize="10sp"
            android:button="@drawable/checkbox_theme" />
        <RadioButton 
            android:checked="false"
            android:layout_width="90sp"
            android:id="@+id/rbNotities"
            android:text="Notities"
            android:textSize="10sp"
            android:button="@drawable/checkbox_theme" />
    </RadioGroup>
</LinearLayout>

这是选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
        android:drawable="@drawable/bt_filter_active" />
    <item android:state_checked="false" android:state_window_focused="false"
        android:drawable="@drawable/bt_filter" />
    <item android:state_checked="true" android:state_pressed="true"
        android:drawable="@drawable/bt_filter_active" />
    <item android:state_checked="false" android:state_pressed="true"
        android:drawable="@drawable/bt_filter" />
    <item android:state_checked="true" android:state_focused="true"
        android:drawable="@drawable/bt_filter_active" />
    <item android:state_checked="false" android:state_focused="true"
        android:drawable="@drawable/bt_filter" />
    <item android:state_checked="false" android:drawable="@drawable/bt_filter" />
    <item android:state_checked="true" android:drawable="@drawable/bt_filter_active" />
</selector>

这就是它的样子:

alt text

你可以弄清楚我想要3个带有文字的大按钮。

我该怎么做?

修改

我将选择器设置为背景而不是按钮,并将按钮设置为空。

代码现在看起来像这样:

<LinearLayout
    android:id="@+id/segmented"
    android:layout_width="fill_parent" 
    android:layout_height="50sp"
    android:gravity="center"
    android:layout_below="@+id/header">
    <RadioGroup android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal"
        android:id="@+id/group1"
        android:gravity="center">
        <RadioButton 
            android:checked="false"
            android:layout_width="100sp"
            android:layout_height="40sp"
            android:id="@+id/rbVerzekeringen"
            android:text="Verzekeringen"
            android:textSize="13sp"
            android:orientation="vertical"
            android:background="@drawable/checkbox_theme"
            android:button="@null"
            android:gravity="center"/>
        <RadioButton 
            android:checked="false"
            android:layout_width="100sp"
            android:layout_height="35sp"
            android:id="@+id/rbPersoonlijk"
            android:text="Persoonlijk"
            android:textSize="35sp"
            android:background="@drawable/checkbox_theme"
            android:button="@null" 
            android:gravity="center"/>
        <RadioButton 
            android:checked="false"
            android:layout_width="100sp"
            android:layout_height="30sp"
            android:id="@+id/rbNotities"
            android:text="Notities"
            android:textSize="13sp"
            android:background="@drawable/checkbox_theme"
            android:button="@null" 
            android:gravity="center"/>
    </RadioGroup>
</LinearLayout>

但是现在当我使按钮变大或变小时,它中的文字就像这样消失了(第一张图像的高度是40sp,第二张是35sp,最后一张是30sp): height of the first image is 40sp, the second is 35sp and the last one is 30sp

如何在不剪切文本的情况下缩小背景图像?

1 个答案:

答案 0 :(得分:6)

您是否尝试将drawable设置为android:background而不是按钮?

请参阅here


修改

我不确定,但我认为它取决于9个补丁内容区域(底部和右边缘线),如果将9补丁设置为背景,Android会根据该设置填充。内容区域随图像延伸。