Android:UI控件风格与主题、selector、Theme
注:默认设置放在最后。 1.颜色xml:values-->resources-->color.xml 1 2 3 4 5 6 <?xmlversion= "1.0" encoding= "utf-8" ?> <resources> <colorname= "yellow" >#FFFF00</color> <colorname= "zise" >#D15FEE</color> <colorname= "lightblue" >#8DB6CD</color> </resources> 2.颜色selector:Color List-->selector-->/color/xxx.xml 1 2 3 4 5 6 <?xmlversion= "1.0" encoding= "utf-8" ?> <selectorxmlns:android= "http://schemas.android.com/apk/res/android" > <itemandroid:state_pressed= "true" android:color= "@color/yellow" /> <itemandroid: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> <!--Defaultscreenmargins,pertheAndroidDesignguidelines.--> <dimenname= "activity_horizontal_margin" >16dp</dimen> <dimenname= "activity_vertical_margin" >16dp</dimen> <dimenname= "btn_head_width" >100dp</dimen> <dimenname= "btn_head_high" >100dp</dimen> <dimenname= "edit_width" >200dp</dimen> <dimenname= "edit_high" >60dp</dimen> <dimenname= "edit_width_xhdpi" >60dp</dimen> <dimenname= "edit_high_xhdpi" >160dp</dimen> <dimenname= "textsize" >20dp</dimen> <dimenname= "EditText_layout_marginTop" >80dp</dimen> <dimenname= "textView_layout_marginTop" >26dp</dimen> </resources> 4.背景图片:Drawable-->selector-->/drawable/xxx.xml 1 2 3 4 5 6 <?xmlversion= "1.0" encoding= "utf-8" ?> <selectorxmlns:android= "http://schemas.android.com/apk/res/android" > <itemandroid:state_pressed= "true" android:drawable= "@drawable/ic_pressed" ></item> <itemandroid: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 <?xmlversion= "1.0" encoding= "utf-8" ?> <selectorxmlns:android= "http://schemas.android.com/apk/res/android" > <itemandroid:state_pressed= "true" android:drawable= "@drawable/home_morecity_pre" ></item> <itemandroid:drawable= "@drawable/home_morecity_nor" ></item> </selector> res/color/selector_home_city.xml代码: 1 2 3 4 5 6 <?xmlversion= "1.0" encoding= "utf-8" ?> <selectorxmlns:android= "http://schemas.android.com/apk/res/android" > <itemandroid:state_pressed= "true" android:color= "@color/cyan" /> <itemandroid:color= "@color/white" /> </selector> 7.创建一个shape的selector 1 2 3 4 5 6 7 8 9 10 11 <?xmlversion= "1.0" encoding= "utf-8" ?> <selectorxmlns:android= "http://schemas.android.com/apk/res/android" > <itemandroid:state_pressed= "true" ><shape> <solidandroid:color= "@color/gray_text" /> </shape></item> <item><shape> <solidandroid: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 <?xmlversion= "1.0" encoding= "utf-8" ?> <selectorxmlns:android= "http://schemas.android.com/apk/res/android" > <!--Nonfocusedstates--> <itemandroid:state_focused= "false" android:state_selected= "false" android:state_pressed= "false" android:drawable= "@drawable/contact" /> <itemandroid:state_focused= "false" android:state_selected= "true" android:state_pressed= "false" android:drawable= "@drawable/contact_sel" /> <!--Focusedstates--> <itemandroid:state_focused= "true" android:state_selected= "false" android:state_pressed= "false" android:drawable= "@drawable/contact_sel" /> <itemandroid:state_focused= "true" android:state_selected= "true" android:state_pressed= "false" android:drawable= "@drawable/contact_sel" /> <!--Pressed--> <itemandroid:state_selected= "true" android:state_pressed= "true" android:drawable= "@drawable/contact_sel" /> <itemandroid: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 StateListDrawabledrawable= new StateListDrawable(); //Nonfocusedstates 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)); //Focusedstates 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)); TextViewtextView=(TextView)findViewById(R.id.TextView_title); textView.setCompoundDrawablesWithIntrinsicBounds( null ,drawable, null , null ); 本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1208480,如需转载请自行联系原作者