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

注:默认设置放在最后。


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

<?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

<?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

<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

<?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. 没有标题:
   @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.设置一按钮点击时图片和文字颜色都有高亮效果

        <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代码:

<?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代码:

<?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

<?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代码:

<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代码:

<?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代码:

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);







本文出自 “天空没有痕迹但我飞过” 博客,转载请与作者联系!

你可能感兴趣的:(android,theme,主题,selector)