博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android:UI控件风格与主题、selector、Theme
阅读量:5896 次
发布时间:2019-06-19

本文共 6239 字,大约阅读时间需要 20 分钟。

注:默认设置放在最后。

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,如需转载请自行联系原作者
你可能感兴趣的文章
JAVA 设计模式 策略模式
查看>>
openstack nova修改实例路径,虚拟磁盘路径
查看>>
java.sql.SQLException: Lock wait timeout exceeded --转
查看>>
使用C#进行图像处理的几种方法(转)
查看>>
Ajax原理学习
查看>>
easyui------dialog如何固定位置
查看>>
sap scriptfom 多语言翻译
查看>>
GSON学习笔记之初识GSON
查看>>
Spring 极速集成注解 redis 实践
查看>>
Python安装模块
查看>>
实现超级简单的bug管理系统
查看>>
Python爬虫从入门到放弃(十一)之 Scrapy框架整体的一个了解
查看>>
MVC webapi,Action的分别拦截器
查看>>
BZOJ 4152: [AMPPZ2014]The Captain(最短路)
查看>>
Java集合中的AbstractMap抽象类
查看>>
没什么技术含量的Remove Before Flight
查看>>
【转】Java 有值类型吗?
查看>>
图像滤镜艺术---(Lightleaks Filter)漏光滤镜
查看>>
[LeetCode] Find Anagram Mappings 寻找异构映射
查看>>
--Too small initial heap for new size specified
查看>>