gpt4 book ai didi

android - 在单选按钮的中心显示自定义绘图和文本

转载 作者:行者123 更新时间:2023-11-30 02:35:02 25 4
gpt4 key购买 nike

我正在编写一个应用程序,我需要在相等的水平空间中绘制两个单选按钮。我通过为两个单选按钮设置 android:layout_weight="1" 属性来实现。

现在我需要在中心显示自定义可绘制按钮,并需要在该可绘制按钮内放置文本,如下所示:

enter image description here

这里 draw drawable 和 text 的行为都是动态的。

有没有什么办法可以同时实现这两点。

最佳答案

尝试以下代码以获得所需的输出。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<LinearLayout
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rd_bg"
android:button="@null"
android:gravity="center"
android:text="15"/>
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:background="@drawable/rd_bg"
android:gravity="center"
android:text="30"/>
</LinearLayout>
</LinearLayout>

下面是drawable duration_selector的代码

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_checked="true"
android:state_focused="true">
<shape android:shape="oval">
<solid android:color="@color/login_bg"/>
<size
android:width="50dp"
android:height="50dp"/>
</shape>
</item>

<item
android:state_checked="false"
android:state_focused="true">
<shape android:shape="oval">
<solid android:color="@color/login_bg"/>
<size
android:width="50dp"
android:height="50dp"/>
</shape>
</item>

<item
android:state_checked="true">
<shape android:shape="oval">
<solid android:color="@color/login_bg"/>
<size
android:width="50dp"
android:height="50dp"/>
</shape>
</item>

<item
android:state_checked="false">
<shape android:shape="oval">
<solid android:color="@android:color/transparent"/>
<stroke android:color="@color/login_bg"
android:width="1dp"/>
<size
android:width="50dp"
android:height="50dp"/>
</shape>
</item>

@color/login_bg 是您要使用的颜色代码。

关于android - 在单选按钮的中心显示自定义绘图和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26710100/

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