gpt4 book ai didi

android - 自定义 RadioButton 图像未填充空间

转载 作者:太空狗 更新时间:2023-10-29 16:24:51 26 4
gpt4 key购买 nike

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

这是广播组

<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

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

最佳答案

您是否尝试将可绘制对象设置为 android:background 而不是按钮?

参见 here .


编辑:

我不确定,但我认为这取决于 9-patch 内容区域(底部和右边缘线),如果将 9-patch 设置为背景,Android 将基于此设置填充。内容区域随图像一起拉伸(stretch)。

关于android - 自定义 RadioButton 图像未填充空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4681902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com