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,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
C# 开发者 Windows
基于Material Design风格开源、易用、强大的WPF UI控件库
基于Material Design风格开源、易用、强大的WPF UI控件库
|
2天前
|
XML Java Android开发
Android控件动态使用 (转)
Android控件动态使用 (转)
|
4天前
|
前端开发 Java Android开发
Android UI底层绘制原理
Android UI底层绘制原理
11 0
|
19天前
|
Java Android开发
Android Mediatek 禁用拨号应用的部分UI显示
Android Mediatek 禁用拨号应用的部分UI显示
12 0
|
28天前
|
编解码 Android开发 UED
安卓UI/UX设计原则:打造引人入胜的用户体验
【4月更文挑战第13天】本文探讨了安卓UI/UX设计的关键原则,包括一致性、简洁性、反馈、清晰性、效率和适应性。一致性要求视觉和行为保持一致,利用系统UI;简洁性减少用户行动,简化导航;反馈需即时且明确;清晰性强调表达清晰,布局有序;效率关注性能优化和任务简化;适应性涉及多设备适配和用户多样性。遵循这些原则,可创建出色应用,提供无缝用户体验。设计应持续迭代,适应技术发展和用户需求。
|
1月前
|
XML 移动开发 Android开发
构建高效安卓应用:采用Jetpack Compose实现动态UI
【4月更文挑战第10天】 在现代移动开发中,用户界面的流畅性和响应性对于应用的成功至关重要。随着技术的不断进步,安卓开发者寻求更加高效和简洁的方式来构建动态且吸引人的UI。本文将深入探讨Jetpack Compose这一革新性技术,它通过声明式编程模型简化了UI构建过程,并提升了性能与跨平台开发的可行性。我们将从基本概念出发,逐步解析如何利用Jetpack Compose来创建具有数据动态绑定能力的安卓应用,同时确保应用的高性能和良好用户体验。
19 0
|
1月前
|
XML Java Android开发
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
|
1月前
|
XML 编解码 Java
Android控件之高级控件——ListView、cardView、屏幕适配
Android控件之高级控件——ListView、cardView、屏幕适配
|
1月前
|
Android开发
Android控件——Checkbox复选框、RadioButton单选、ToggleButton开关、SeekBar拖动条
Android控件——Checkbox复选框、RadioButton单选、ToggleButton开关、SeekBar拖动条
|
1月前
|
XML Java Android开发
Android之UI基础控件
Android之UI基础控件