茶树

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

----------------------------------按钮点击变色(源代码下有属性解释)-----------------------------------------------------

 

 

一、shape 样式:(在drawable新建--》new--》Drawable resource file 在父级标签selector添加Item

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

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#73c4f3" />
<stroke android:width="2dp" android:color="#ffffff" />
<corners android:radius="20dp" />
<padding android:bottom="5dp" android:left="10dp" android:right="10dp" android:top="5dp" />
<gradient android:angle="270" android:endColor="#8accf2" android:startColor="#8accf2" android:type="sweep" />
</shape>
</item>

<item>
<shape>
<solid android:color="#3fb3f6" />
<stroke android:width="2dp" android:color="#ffffff" />
<corners android:radius="20dp" />
<padding android:bottom="5dp" android:left="10dp" android:right="10dp" android:top="5dp" />
</shape>
</item>
</selector>

二、style 样式:
<style name="rectangle">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">15sp</item>
<item name="android:textColor">#ffffff</item>
<item name="android:background">@drawable/buttonclickstyle</item>
</style>

三、Button控件调用style样式:


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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ly.blogtest.MainActivity">

<Button
android:id="@+id/button"
style="@style/rectangle"
android:text="@string/btn1" />
</RelativeLayout>

 

----------------------------------按钮点击变色-----------------------------------------------------

 

----------------------------------item属性标记------------------------------

  • android:state_pressed 是否按下,如一个按钮触摸或者点击。  
  • android:state_focused 是否取得焦点,比如用户选择了一个文本框。  
  • android:state_hovered 光标是否悬停,通常与focused state相同,它是4.0的新特性  
  • android:state_selected 被选中,它与focus state并不完全一样,如一个list view 被选中的时候,它里面的各个子组件可能通过方向键,被选中了。  
  • android:state_checkable 组件是否能被check。如:RadioButton是可以被check的。  
  • android:state_checked 被checked了,如:一个RadioButton可以被check了。  
  • android:state_enabled 能够接受触摸或者点击事件  
  • android:state_activated 被激活  
  • android:state_window_focused

 应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了 

注意:如果有多个item,那么程序将自动从上到下进行匹配,最先匹配的将得到应用。(不是通过最佳匹配)  

如果一个item没有任何的状态说明,那么它将可以被任何一个状态匹配。 

----------------------------------item属性标记------------------------------

按钮形状、颜色属性请结合查看  http://www.cnblogs.com/LOVEJIEYING/p/6016319.html

 

posted on 2016-11-03 10:22  "茶树"  阅读(2654)  评论(0编辑  收藏  举报