Android基礎篇(二)

StateListDrawable

針對不同的視圖狀態加載不同的圖片資源來顯示的資源選擇器

    android:state_pressed   按下
    android:state_selected  選中(檢點狀態)
    android:state_checked   選擇框的選擇狀態
    android:stata_enable    可用狀態

使用方法:

        可以在drawable下定義跟標籤爲selector的xml文件
        以選擇框的狀態爲例,文件名爲check_button_drawable.xml;
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--  未選中狀態 -->
<item android:drawable="@drawable/button_un_checkedl" android:state_checked="false"/>
<!--  選中狀態 -->
<item android:drawable="@drawable/button_checked" android:state_checked="true"/>
</selector>

佈局中直接使用 @drawable來加載選擇器即可

視圖的基本狀態(以按鈕爲例)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--  可用未按下 -->
<item android:drawable="@drawable/btn_nomal" android:state_enabled="true" android:state_pressed="false"/>
<!--  可用按下 -->
<item android:drawable="@drawable/btn_press" android:state_enabled="true" android:state_pressed="true"/>
<!--  不可用 -->
<item android:drawable="@drawable/btn_no_enable" android:state_enabled="false"/>
</selector>
使用(設置到背景)
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 點擊有驚喜 "
android:background="@drawable/btn_bg_drawable" /

針對視圖的可用狀態在xml中可以使用android:enable屬性

    android:enabled="false" false 表示不可用,默認 true 表示可用
    該屬性在 Java 對應的是
    btn.setEnable(true) btn.setEnable(false);
    判斷是否可用
    btn.isEnable();

ColorStateListDrawable

顏色選擇器,可以根據不同狀態使用不同的顏色值,需要在 res 下新建一個 color 文件夾,然後在 color 文件夾中新建 xml 文件,根節點爲 selector ,以按鈕上的
文本爲例,新建一個文件: tx_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--  未按下 -->
<item android:state_enabled="true" android:state_pressed="false" android:color="#000000"/>
<!--  按下 -->
<item android:state_enabled="true" android:state_pressed="true" android:color="@color/white"/>
<!--  不可用 -->
<item android:state_enabled="false" android:color="#666666"/>
</selector>

注意:如果針對視圖的背景 ( 也就是需要用圖像、圖像來顯示的地方 ) 使用顏色,則顏色選擇的定義屬於 drawable ,定義方式需要在 drawable 中定義選擇器

shape

可以通過定義 shape 實現形狀的顯示,一般放在 drawable 文件夾中,跟標籤爲 shape

shape 屬性表示形狀類型,默認 rectangle 矩形, oval 橢圓, line 線條, ring 圓環
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!--  漸變填充 , 跟 solid 二選一 -->
<!--
<gradient
android:endColor="#EE941A" 起始顏色
android:centerColor="#ff0000" 中間顏色
android:startColor="#F6E5AE" 結束顏色
android:type="sweep" 類型,默認 linea 線性, radial 放射, sweep 掃描
android:angle="90" 角度(只對 linear 類型有效 0 從左到右, 90 從上到下)
android:centerX="50%" 中心 ( 針對放射以及掃描 )x 座標
android:centerY="50%" /> 中心 y 座標
-->
<!--  圓角 ( 針對矩形 ) -->
<corners
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp" />
<!--  純色填充,跟 gradient 二選一 -->
<solid android:color="#ff0000" />
<!--  邊框 -->
<stroke 如果是虛線需要添加 dashGapdashWidth 兩個屬性
android:dashGap="5dp" 虛線之間短線間距
android:dashWidth="15dp" 虛線短線長度
android:width="2dp"
android:color="#0000ff" />
<!--  內容邊距 -->
<padding
android:bottom="3dp"
android:left="5dp"
android:right="5dp"
android:top="3dp" />
</shape>

形狀一般用來做背景
如果一個視圖中不同狀態有不同形狀,可以在 selector 的 item 中單獨定義,也可以直接用 @drawable/xxx 方式加載

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="false">
        <shape>
            <corners android:radius="10dp"/>
            <solid android:color="#ff0000"/>
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape>
            <corners android:radius="10dp"/>
            <solid android:color="#ffff00"/>
        </shape>
    </item>
</selector>

Activity之間的跳轉

1、定義Activity類(定義的類繼承Activity 或者其子類,設置佈局 )

    public class LoginActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //  設置界面佈局
            setContentView(R.layout.activity_login);
        }
    }

2 、在 manifest.xml 中註冊 Activity

<activity
android:name="com.xykj.day5.LoginActivity"
android:label="@string/login_activity_label" >
</activity>

3 、創建 Intent 對象,使用 startActivity 來啓動

// 創建一個意圖
// 第一個參數是一個 Context 對象 (Activity\Service\Application)
// 第二個參數表示意圖要關聯的目標,使用的是 Class 實例
Intent it = new Intent(MainActivity.this,LoginActivity.class);
// 啓動窗口
startActivity(it);
發佈了27 篇原創文章 · 獲贊 7 · 訪問量 7661
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章