Android:UI控件风格与主题、selector、Theme

注:默认设置放在最后。


1.颜色xml:values-->resources-->color.xml

1
2
3
4
5
6
<?xml version= "1.0"  encoding= "utf-8" ?>
<resources>
     <color name= "yellow" >#FFFF00</color>
     <color name= "zise" >#D15FEE</color>
     <color name= "lightblue" >#8DB6CD</color>
</resources>


2.颜色selector:Color List-->selector-->/color/xxx.xml

1
2
3
4
5
6
<?xml version= "1.0"  encoding= "utf-8" ?>
<selector xmlns:android= "http://schemas.android.com/apk/res/android" >
     <item android:state_pressed= "true"
         android:color= "@color/yellow" />
     <item android:color= "@color/zise" />
</selector>


3.尺寸(文件大小、控件大小等)dimens:values-->dimens.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<resources>
     <!-- Default screen margins, per the Android Design guidelines. -->
     <dimen name= "activity_horizontal_margin" >16dp</dimen>
     <dimen name= "activity_vertical_margin" >16dp</dimen>
     <dimen name= "btn_head_width" >100dp</dimen>
     <dimen name= "btn_head_high" >100dp</dimen>
     <dimen name= "edit_width" >200dp</dimen>
     <dimen name= "edit_high" >60dp</dimen>
     <dimen name= "edit_width_xhdpi" >60dp</dimen>
     <dimen name= "edit_high_xhdpi" >160dp</dimen>
     <dimen name= "textsize" >20dp</dimen>
                         
     <dimen name= "EditText_layout_marginTop" >80dp</dimen>
     <dimen name= "textView_layout_marginTop" >26dp</dimen>
</resources>


4.背景图片:Drawable-->selector-->/drawable/xxx.xml

1
2
3
4
5
6
<?xml version= "1.0"  encoding= "utf-8" ?>
<selector xmlns:android= "http://schemas.android.com/apk/res/android"  >
     <item android:state_pressed= "true"
         android:drawable= "@drawable/ic_pressed" ></item>
     <item android:drawable= "@drawable/ic_default" ></item>
</selector>



5.主题相关

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
常用的系统主题:
1 . 没有标题:
    @android :style/Theme.Black.NoTitleBar
2 . 全屏幕
    @android :style/Theme.Black.NoTitleBar.Fullscreen
3 . 透明
    @android :style/Theme.Translucent
4 . 桌面壁纸作为背景
    @android :style/Theme.Wallpaper
5 . 对话框
    @android :style/Theme.Dialog 
    对Activity的生命周期是否有影响?
练习:第一个Activity中有两个按钮,
       一个弹出对话框主题的Activity,
       另一个弹出对话框,测试二者对
       第一个Activity生命周期是否有影响。     
       
弹出对话框主题的Activity:
       暂停状态,onPause() 
弹出对话框
     生命周期没有影响
       
使用场景:
1 . 对话框主题的Activity
    例如:在桌面弹出短信内容对话框
    对话框后面的Activity是其他App的,这时必须使用对话框主题的
    Activity
2 . 对话框   
    需要使用Activity中的成员变量时


6.设置一按钮点击时图片和文字颜色都有高亮效果

1
2
3
4
5
6
7
8
9
10
11
12
13
         <TextView
             android:id= "@+id/home_tv_city"
             android:layout_width= "wrap_content"
             android:layout_height= "wrap_content"
             android:layout_alignParentLeft= "true"
             android:layout_centerVertical= "true"
             android:drawableLeft= "@drawable/selector_home_citymore"
             android:drawablePadding= "4dp"
             android:gravity= "center"
             android:paddingRight= "12dp"
             android:text= "厦门"
             android:textColor= "@color/selector_home_city"
             android:textSize= "20sp"  />


res/drawable/selector_home_citymore.xml代码:

1
2
3
4
5
6
<?xml version= "1.0"  encoding= "utf-8" ?>
<selector xmlns:android= "http://schemas.android.com/apk/res/android"  >
     <item android:state_pressed= "true"
         android:drawable= "@drawable/home_morecity_pre" ></item>
<item android:drawable= "@drawable/home_morecity_nor" ></item>
</selector>


res/color/selector_home_city.xml代码:

1
2
3
4
5
6
<?xml version= "1.0"  encoding= "utf-8" ?>
<selector xmlns:android= "http://schemas.android.com/apk/res/android" >
     <item android:state_pressed= "true"
         android:color= "@color/cyan" />
     <item android:color= "@color/white" />
</selector>



7.创建一个shape的selector

1
2
3
4
5
6
7
8
9
10
11
<?xml version= "1.0"  encoding= "utf-8" ?>
<selector xmlns:android= "http://schemas.android.com/apk/res/android" >
 
     <item android:state_pressed= "true" ><shape>
             <solid android:color= "@color/gray_text"  />
         </shape></item>
     <item><shape>
             <solid android:color= "@color/touming"  />
         </shape></item>
 
</selector>



8.代码编写selector

范例:

布局xml代码:

1
2
3
4
5
6
7
8
<TextView
         android:id= "@+id/TextView_title"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:focusable= "true"
         android:drawableTop= "@drawable/selector_tabwidget_icon"
         android:textAlignment= "center"
         />


selector的xml代码:

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version= "1.0"  encoding= "utf-8" ?>
<selector xmlns:android= "http://schemas.android.com/apk/res/android"  >
     <!-- Non focused states -->
     <item android:state_focused= "false"  android:state_selected= "false"  android:state_pressed= "false"  android:drawable= "@drawable/contact"  />
     <item android:state_focused= "false"  android:state_selected= "true"  android:state_pressed= "false"  android:drawable= "@drawable/contact_sel"  />
     <!-- Focused states -->
     <item android:state_focused= "true"  android:state_selected= "false"  android:state_pressed= "false"  android:drawable= "@drawable/contact_sel"  />
     <item android:state_focused= "true"  android:state_selected= "true"  android:state_pressed= "false"  android:drawable= "@drawable/contact_sel"  />
     <!-- Pressed -->
     <item android:state_selected= "true"  android:state_pressed= "true"  android:drawable= "@drawable/contact_sel"  />
     <item android:state_pressed= "true"  android:drawable= "@drawable/contact_sel"  />
</selector>


selector的java代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
StateListDrawable drawable =  new  StateListDrawable();
         //Non focused states
         drawable.addState( new  int []{-android.R.attr.state_focused, -android.R.attr.state_selected, -android.R.attr.state_pressed},
                 getResources().getDrawable(R.drawable.contact));
         drawable.addState( new  int []{-android.R.attr.state_focused, android.R.attr.state_selected, -android.R.attr.state_pressed},
                 getResources().getDrawable(R.drawable.contact_sel));
         //Focused states
         drawable.addState( new  int []{android.R.attr.state_focused,-android.R.attr.state_selected, -android.R.attr.state_pressed},
                 getResources().getDrawable(R.drawable.contact_sel));
         drawable.addState( new  int []{android.R.attr.state_focused,android.R.attr.state_selected, -android.R.attr.state_pressed},
                 getResources().getDrawable(R.drawable.contact_sel));
         //Pressed
         drawable.addState( new  int []{android.R.attr.state_selected, android.R.attr.state_pressed},
                 getResources().getDrawable(R.drawable.contact_sel));
         drawable.addState( new  int []{android.R.attr.state_pressed},
                 getResources().getDrawable(R.drawable.contact_sel));
           
         TextView textView = (TextView) findViewById(R.id.TextView_title);
                  
         textView.setCompoundDrawablesWithIntrinsicBounds( null , drawable,  null null );




本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1208480,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值