Android开发描边按钮,点击之后会有颜色填充,其实很简单的
只要shape+selector就可以实现这个功能
上次面试的时候,人家就问我这个
赶紧看看。
上效果图
点击前:
Android 开发描边按钮
点击时:
点击时不好截图,就算了吧,我描述一下点击时,按钮会全部变红。
嗯。
怎么实现效果呢,在res/drawable文件夹下新建shape.xml
xml代码如下:

<?xml version="1.0" encoding="utf-8"?><!--shape的形状,默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="rectangle">
            <stroke android:width="5dp" android:color="#F31313">
            </stroke>
            <corners android:radius="10dp">
            </corners>
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <stroke android:width="5dp" android:color="#F31313">

            </stroke>
            <corners android:radius="10dp">

            </corners>
            <solid android:color="#F31313">

            </solid>
        </shape>
    </item>
</selector>

android:state_pressed="true"代表当点击的时候用这个item写的样式。
除此之外还有类似的。

<!-- 没有焦点时的背景图片 -->    
<item 
 android:state_window_focused="false"
 />     
 
<!-- 非触摸模式下获得焦点并单击时的背景图片 -->    
<item 
 android:state_focused="true" 
 android:state_pressed="true"   
 />   
 
<!-- 触摸模式下单击时的背景图片-->    
<item 
 android:state_focused="false" 
 android:state_pressed="true"   
 />    
 
<!--选中时的图片背景-->    
<item 
 android:state_selected="true" 
 />     
 
<!--获得焦点时的图片背景-->    
<item 
 android:state_focused="true" 
 />     

activity的XML中这么用:

 <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="200px"
        android:text="点击"
        android:background="@drawable/shape"
        />

嗯!OK!

相关文章:

  • 2022-12-23
  • 2021-11-01
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2021-11-28
  • 2021-11-22
  • 2022-01-18
猜你喜欢
  • 2021-12-21
  • 2021-09-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案