简体   繁体   English

Android按钮背景形状

[英]Android button background shape

I want to create a radio button that looks like this: 我想创建一个如下所示的单选按钮:

在此输入图像描述

That is, when selected, a checkmark shows up to indicate it is selected instead of a dot. 也就是说,选中时,会显示一个复选标记,表示它已被选中而不是一个点。 When unselected, there is only an orange background (or some other color). 未选中时,只有橙色背景(或其他颜色)。

The black background of the page is just the background of the app. 页面的黑色背景只是应用程序的背景。

I know that I could create different drawables for different state for each button, but this is not really extensible if there are a lot of colors. 我知道我可以为每个按钮为不同的状态创建不同的drawable,但如果有很多颜色,这不是真正可扩展的。

I was thinking that I could add a checkmark image as a drawable for the android:button attribute and supply different colors for android:background for the radio buttons. 我想我可以添加一个复选标记图像作为android:button属性的drawable,并为android:background提供不同颜色的单选按钮。 But it doesn't work well because the shape of the background is always square. 但它不能很好地工作,因为背景的形状总是方形的。 So I ended up having a square button. 所以我最终得到一个方形按钮。

What is the best way to implement such a button? 实现这样一个按钮的最佳方法是什么? Or, how do I change the background shape to be round/oval instead of square? 或者,如何将背景形状更改为圆形/椭圆形而不是方形?

This is what I have right now: 这就是我现在所拥有的:

<!-- layout.xml -->
<RadioButton
    android:id="@+id/radio1"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:background="@color/orange"
    android:button="@drawable/radio"/>

<!-- drawable/radio.xml -->
<item 
    android:state_checked="true" 
    android:drawable="@drawable/checkmark_on_oval"
    >
</item>    

Try this may helpful to you. 试试这可能对你有所帮助。 put radio_button properties like 把radio_button属性放在

    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:button="@null"
        android:checked="true"
        android:drawableLeft="@drawable/radio_button_selector"
        android:drawablePadding="5dp"
        android:paddingLeft="2dp" />

In drawable folder put rb_drawable.xml 在drawable文件夹中放入rb_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/radio_button_unchecked" android:state_checkable="true"/> <!-- pressed -->
    <item android:drawable="@drawable/radio_button_checked" android:state_checked="true"/> <!-- focused -->
    <item android:drawable="@drawable/radio_button_unchecked"/> <!-- default -->

</selector>

Here radio_button_unchecked and radio_button_checked are two images for your radio buttons. 这里radio_button_uncheckedradio_button_checked是你的单选按钮的两个图像。

You can create a round shape using the following code 您可以使用以下代码创建圆形

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="oval">
         <solid android:color="#9F2200"/>
         <stroke android:width="2sp" android:color="#fff" />
     </shape>

Try it out.. 试试看..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM