導(dǎo)航:ColorStateList和RadioGroup

用到的圖片資源下載地址http://pan.baidu.com/s/1geqo6iB 密碼:fuv0

ColorStateList可以根據(jù)View的狀態(tài)來(lái)設(shè)置View的顏色,例如背景色,文字顏色等等。用xml定義ColorStateList時(shí),要定義在res/color文件夾中继找。例如,我們?cè)?code>res/color下寫(xiě)了一個(gè)bg_edit.xml。如下所示:

colorstatelist22.png

然后我們?cè)O(shè)置具有輸入事件的EditText的字體的顏色引用為該xml文件反粥。如下代碼所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <!--
        android:textColor="@color/bg_edit"
        設(shè)置EditTexit的字體顏色為引用bg_edit.xml中定義的ColorStateList
    -->
    <EditText
        android:textColor="@color/bg_edit"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="text" >
    </EditText>
    
     <EditText
        android:textColor="@color/bg_edit"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="text" >
    </EditText>

</LinearLayout>

然后運(yùn)行該示例,就可以看見(jiàn)如下的效果:

Untitled.gif

可以看到疲迂,當(dāng)EditText得到焦點(diǎn)時(shí)才顿,文字的顏色變?yōu)榧t色,否則變?yōu)榫G色尤蒿。

有了上面的鋪墊郑气,我們就可以實(shí)現(xiàn)如下的效果:

navigation.gif

示例的底部導(dǎo)航使用的是RadioGroupRadioButton,中間的內(nèi)容區(qū)域使用的是Fragment腰池。RadioGroup中的一組RadioButton每次只能選擇其中一個(gè)尾组。RadioButton具有checked屬性忙芒,因此我們可以在res/color文件夾下定義一個(gè)ColorStateList,根據(jù)RadioButton是否被checked來(lái)設(shè)置RadioButton的字體顏色讳侨。而RadioButton的背景圖片可以使用類(lèi)似的思路在res/drawable文件夾下寫(xiě)一個(gè)xml文件呵萨,根據(jù)RadioButton是否被checked來(lái)設(shè)置其背景圖片。

main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- Fragment的容器 -->
    <FrameLayout
        android:id="@+id/fragment_root"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </FrameLayout>

    <!-- 底部導(dǎo)航按鈕 -->
    <RadioGroup
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="#F0F0F0"
        android:orientation="horizontal"
        android:padding="8dp" >

        <!-- 
            android:button="@null"
                這條屬性取消RadioButton前面的小圓圈
            android:textColor="@color/tab_text_color" 
                這條屬性設(shè)置文本的顏色根據(jù)該RadioButton的狀態(tài)的變化而變化
            android:drawableTop="@drawable/bg_home"
                這條屬性在RadioButton的頂部繪制一個(gè)圖形跨跨,這個(gè)圖形也會(huì)根據(jù)RadioButton的狀態(tài)的變化而變化
         -->
        <RadioButton
            android:id="@+id/home"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:checked="true"
            android:drawableTop="@drawable/bg_home"
            android:gravity="center"
            android:text="@string/home"
            android:textColor="@color/tab_text_color" />

        <RadioButton
            android:id="@+id/friends"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/bg_friends"
            android:gravity="center"
            android:text="@string/friends"
            android:textColor="@color/tab_text_color" />

        <RadioButton
            android:id="@+id/message"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/bg_message"
            android:gravity="center"
            android:text="@string/message"
            android:textColor="@color/tab_text_color" />

        <RadioButton
            android:id="@+id/personal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/bg_personal"
            android:gravity="center"
            android:text="@string/personal"
            android:textColor="@color/tab_text_color" />
    </RadioGroup>

</LinearLayout>

res/color/tab_text_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--RadioButton被checked時(shí)潮峦,設(shè)置其顏色為#00B7FA-->
    <item android:state_checked="true" android:color="#00B7FA"/>
    <!--RadioButton沒(méi)有被checked時(shí),設(shè)置其顏色為#818181-->
    <item android:state_checked="false" android:color="#818181"/>
</selector>

res/drawable-hdip/bg_home.xml

<?xml version="1.0" encoding="utf-8"?>
<!--根據(jù)RadioButton是否被checked來(lái)設(shè)置其背景圖片-->
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:drawable="@drawable/tab_home_pressed"/>
    <item android:state_checked="false" android:drawable="@drawable/tab_home"/>
</selector>

res/drawable-hdip/bg_friends.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:drawable="@drawable/tab_friends_pressed"/>
    <item android:state_checked="false" android:drawable="@drawable/tab_friends"/>
</selector>

res/drawable-hdip/bg_message

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:drawable="@drawable/tab_message_pressed"/>
    <item android:state_checked="false" android:drawable="@drawable/tab_message"/>
</selector>

res/drawable-hdip/bg_personal

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:drawable="@drawable/tab_personal_pressed"/>
    <item android:state_checked="false" android:drawable="@drawable/tab_personal"/>
</selector>

layout/fragment_home

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:gravity="center"
    >
    
    <TextView 
        android:gravity="center"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:padding="8dp"
        android:textSize="30sp"
        android:text="@string/this_is_home_page"
        />

</LinearLayout>

layout/fragment_friends.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="8dp"
        android:text="@string/this_is_friends_page"
        android:textSize="30sp" />

</LinearLayout>

layout/fragment_message.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >
    

    <TextView 
        android:gravity="center"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:padding="8dp"
        android:textSize="30sp"
        android:text="@string/this_is_message_page"
        />
</LinearLayout>

layout/fragment_personal.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >
    
    <TextView 
        android:gravity="center"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:padding="8dp"
        android:textSize="30sp"
        android:text="@string/this_is_personal_page"
        />
    
</LinearLayout>

MainActivity.java

package com.ddzj.navigation;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity {

    private FragmentManager mFragmentManager;
    private Fragment mHomeFragment = new HomeFragment();
    private Fragment mFriendsFragment = new FriendsFragment();
    private Fragment mMessageFragment = new MessageFragment();
    private Fragment mPersonalFragment = new PersonalFragment();
    private RadioGroup mNavigation;
    
    private OnCheckedChangeListener mNavigationListener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
            case R.id.home:
                mFragmentManager.beginTransaction().replace(R.id.fragment_root, mHomeFragment).commit();
                break;
            case R.id.friends:
                mFragmentManager.beginTransaction().replace(R.id.fragment_root, mFriendsFragment).commit();
                break;
            case R.id.message:
                mFragmentManager.beginTransaction().replace(R.id.fragment_root, mMessageFragment).commit();
                break;
            case R.id.personal:
                mFragmentManager.beginTransaction().replace(R.id.fragment_root, mPersonalFragment).commit();
                break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNavigation = (RadioGroup) findViewById(R.id.navigation);
        mNavigation.setOnCheckedChangeListener(mNavigationListener);
        mFragmentManager = getFragmentManager();
        mFragmentManager.beginTransaction().add(R.id.fragment_root, mHomeFragment).commit();
    }
}

HomeFragment.java

package com.ddzj.navigation;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class HomeFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_home, null);
    }
}

FriendsFragment.java

package com.ddzj.navigation;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class HomeFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_friends, null);
    }
}

MessageFragment.java

package com.ddzj.navigation;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class HomeFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_message, null);
    }
}

PersonalFragment.java

package com.ddzj.navigation;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class HomeFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_personal, null);
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末勇婴,一起剝皮案震驚了整個(gè)濱河市忱嘹,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌耕渴,老刑警劉巖拘悦,帶你破解...
    沈念sama閱讀 212,542評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異萨螺,居然都是意外死亡窄做,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,596評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門(mén)慰技,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)椭盏,“玉大人,你說(shuō)我怎么就攤上這事吻商√图眨” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 158,021評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵艾帐,是天一觀的道長(zhǎng)乌叶。 經(jīng)常有香客問(wèn)我,道長(zhǎng)柒爸,這世上最難降的妖魔是什么准浴? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,682評(píng)論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮捎稚,結(jié)果婚禮上乐横,老公的妹妹穿的比我還像新娘。我一直安慰自己今野,他們只是感情好葡公,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,792評(píng)論 6 386
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著条霜,像睡著了一般催什。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上宰睡,一...
    開(kāi)封第一講書(shū)人閱讀 49,985評(píng)論 1 291
  • 那天蒲凶,我揣著相機(jī)與錄音气筋,去河邊找鬼。 笑死豹爹,一個(gè)胖子當(dāng)著我的面吹牛裆悄,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播臂聋,決...
    沈念sama閱讀 39,107評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼或南!你這毒婦竟也來(lái)了孩等?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 37,845評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤采够,失蹤者是張志新(化名)和其女友劉穎肄方,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體蹬癌,經(jīng)...
    沈念sama閱讀 44,299評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡权她,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,612評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了逝薪。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片隅要。...
    茶點(diǎn)故事閱讀 38,747評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖董济,靈堂內(nèi)的尸體忽然破棺而出步清,到底是詐尸還是另有隱情,我是刑警寧澤虏肾,帶...
    沈念sama閱讀 34,441評(píng)論 4 333
  • 正文 年R本政府宣布廓啊,位于F島的核電站,受9級(jí)特大地震影響封豪,放射性物質(zhì)發(fā)生泄漏谴轮。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,072評(píng)論 3 317
  • 文/蒙蒙 一吹埠、第九天 我趴在偏房一處隱蔽的房頂上張望第步。 院中可真熱鬧,春花似錦藻雌、人聲如沸雌续。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,828評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)驯杜。三九已至,卻和暖如春做个,著一層夾襖步出監(jiān)牢的瞬間鸽心,已是汗流浹背滚局。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,069評(píng)論 1 267
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留顽频,地道東北人藤肢。 一個(gè)月前我還...
    沈念sama閱讀 46,545評(píng)論 2 362
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像糯景,于是被迫代替她去往敵國(guó)和親嘁圈。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,658評(píng)論 2 350

推薦閱讀更多精彩內(nèi)容

  • 參考:五種方式實(shí)現(xiàn)Android底部導(dǎo)航欄Android底部導(dǎo)航欄實(shí)現(xiàn)(二)之RadioGroup 一. 簡(jiǎn)介 1...
    NickelFox閱讀 2,711評(píng)論 0 8
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 171,849評(píng)論 25 707
  • 時(shí)間:2016年5月24日16:44:21作者:JustDo23說(shuō)明:在開(kāi)發(fā)中經(jīng)常會(huì)遇到使用Fragment的情況...
    JustDo23閱讀 969評(píng)論 0 3
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa閱讀 8,855評(píng)論 0 6
  • 六年級(jí)二班第二輪積分抽獎(jiǎng)活動(dòng)開(kāi)始了蟀淮! 獎(jiǎng)品是琳瑯滿(mǎn)目:書(shū)包最住、本子、筆筒怠惶、筆涨缚、各種本子。這些都是王子榮媽媽準(zhǔn)備的策治!謝...
    筱瑋閱讀 397評(píng)論 0 0