標(biāo)注:本文為個人學(xué)習(xí)使用,僅做自己學(xué)習(xí)參考使用衅檀,請勿轉(zhuǎn)載和轉(zhuǎn)發(fā)
2018-07-18: 初稿,參考博主coder-pig
0. 引言
- 上一節(jié)的底部的選相框主要使用的是LinearLayout + TextView實(shí)現(xiàn)的底部導(dǎo)航欄的效果哩陕,但是每次店家都需要重置所有TextView的狀態(tài)罩润,然后選中點(diǎn)擊的TextView。
- 本節(jié)主要使用另一種方法脚曾,RadioGroup + RadioButton來實(shí)現(xiàn)
1. 主要思路
- 本節(jié)用到的實(shí)現(xiàn)單選效果是RadioButton东且,簡單電說就是一個RadioGroup包裹著四個RadioButton,和前面一樣采取1:1:1:1
- 另外我們只需重寫RadioGroup的onCheckedChange本讥,判斷checkid即可知道點(diǎn)擊的是哪個RadioButton珊泳。
2. 實(shí)現(xiàn)流程
- 素材還是采用的上一節(jié)中的素材,另外drawable類的資源是將selected狀態(tài)修改為checked拷沸!
Step 1:寫底部選項(xiàng)的一些資源文件
圖片Drawable資源:tab_menu_channel.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/tab_channel_pressed" android:state_checked="true" />
<item android:drawable="@mipmap/tab_channel_normal" />
</selector>
文字資源:tab_menu_text.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/text_yellow" android:state_checked="true" />
<item android:color="@color/text_gray" />
</selector>
背景資源:tab_menu_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape>
<solid android:color="#FFC4C4C4" />
</shape>
</item>
<item>
<shape>
<solid android:color="@color/transparent" />
</shape>
</item>
</selector>
Step 2:編寫我們的Activity布局
- 在前面用TextView實(shí)現(xiàn)底部導(dǎo)航欄我們就發(fā)現(xiàn)了一個問題色查,每個TextView的屬性都幾乎是差不多 的,而在建議那里我們也說讓大家把相同的屬性抽取出來寫到Style中撞芍,可能部分朋友懶或者不知道 如何抽取出來秧了,以及用,這里就給大家示范下:
首先我們?nèi)〕銎渲幸粋€RadioGroup的標(biāo)簽:
<RadioButton
android:id="@+id/rb_channel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/tab_menu_bg"
android:button="@null"
android:drawableTop="@drawable/tab_menu_channel"
android:gravity="center"
android:paddingTop="3dp"
android:text="@string/tab_menu_alert"
android:textColor="@drawable/tab_menu_text"
android:textSize="18sp" />
我們可以把每個RadioButton都相同的屬性抽取出來序无,寫到style.xml文件中:
<style name="tab_menu_item">
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">1</item>
<item name="android:layout_height">match_parent</item>
<item name="android:background">@drawable/tab_menu_bg</item>
<item name="android:button">@null</item>
<item name="android:gravity">center</item>
<item name="android:paddingTop">3dp</item>
<item name="android:textColor">@drawable/tab_menu_text</item>
<item name="android:textSize">18sp</item>
</style>
然后我們的activity_main.xml中的RadioButton就用不著次次都寫相同的代碼了验毡, 只需讓RadioButton的style="@style/tab_menu_item"就可以了!
activity_main.xml:
<RelativeLayout 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:background="@color/bg_gray"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/ly_top_bar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/bg_topbar">
<TextView
android:id="@+id/txt_topbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:text="信息"
android:textColor="@color/text_topbar"
android:textSize="18sp" />
<View
android:layout_width="match_parent"
android:layout_height="2px"
android:layout_alignParentBottom="true"
android:background="@color/div_white" />
</RelativeLayout>
<RadioGroup
android:id="@+id/rg_tab_bar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:background="@color/bg_white"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_channel"
style="@style/tab_menu_item"
android:drawableTop="@drawable/tab_menu_channel"
android:text="@string/tab_menu_alert" />
<RadioButton
android:id="@+id/rb_message"
style="@style/tab_menu_item"
android:drawableTop="@drawable/tab_menu_message"
android:text="@string/tab_menu_profile" />
<RadioButton
android:id="@+id/rb_better"
style="@style/tab_menu_item"
android:drawableTop="@drawable/tab_menu_better"
android:text="@string/tab_menu_pay" />
<RadioButton
android:id="@+id/rb_setting"
style="@style/tab_menu_item"
android:drawableTop="@drawable/tab_menu_setting"
android:text="@string/tab_menu_setting"/>
</RadioGroup>
<View
android:id="@+id/div_tab_bar"
android:layout_width="match_parent"
android:layout_height="2px"
android:layout_above="@id/rg_tab_bar"
android:background="@color/div_white" />
<FrameLayout
android:id="@+id/ly_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/div_tab_bar"
android:layout_below="@id/ly_top_bar"></FrameLayout>
</RelativeLayout>
Step 3:隱藏頂部導(dǎo)航欄
AndroidManifest.xml設(shè)置下theme屬性
android:theme="@style/Theme.AppCompat.NoActionBar"
Step 4:創(chuàng)建一個Fragment的簡單布局與類:
直接照搬上一節(jié)的布局跟Fragment:
fg_content.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_white">
<TextView
android:id="@+id/txt_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="呵呵"
android:textColor="@color/text_yellow"
android:textSize="20sp"/>
</LinearLayout>
MyFragment.java:
public class MyFragment extends Fragment {
private String content;
public MyFragment(String content) {
this.content = content;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fg_content,container,false);
TextView txt_content = (TextView) view.findViewById(R.id.txt_content);
txt_content.setText(content);
return view;
}
}
Step 5:編寫MainActivity.java
這個比起TextView實(shí)現(xiàn)簡單多了帝嗡,就不詳細(xì)講解了晶通,很簡單,直接上代碼:
MainActivity.java
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{
private RadioGroup rg_tab_bar;
private RadioButton rb_channel;
//Fragment Object
private MyFragment fg1,fg2,fg3,fg4;
private FragmentManager fManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fManager = getFragmentManager();
rg_tab_bar = (RadioGroup) findViewById(R.id.rg_tab_bar);
rg_tab_bar.setOnCheckedChangeListener(this);
//獲取第一個單選按鈕丈探,并設(shè)置其為選中狀態(tài)
rb_channel = (RadioButton) findViewById(R.id.rb_channel);
rb_channel.setChecked(true);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
FragmentTransaction fTransaction = fManager.beginTransaction();
hideAllFragment(fTransaction);
switch (checkedId){
case R.id.rb_channel:
if(fg1 == null){
fg1 = new MyFragment("第一個Fragment");
fTransaction.add(R.id.ly_content,fg1);
}else{
fTransaction.show(fg1);
}
break;
case R.id.rb_message:
if(fg2 == null){
fg2 = new MyFragment("第二個Fragment");
fTransaction.add(R.id.ly_content,fg2);
}else{
fTransaction.show(fg2);
}
break;
case R.id.rb_better:
if(fg3 == null){
fg3 = new MyFragment("第三個Fragment");
fTransaction.add(R.id.ly_content,fg3);
}else{
fTransaction.show(fg3);
}
break;
case R.id.rb_setting:
if(fg4 == null){
fg4 = new MyFragment("第四個Fragment");
fTransaction.add(R.id.ly_content,fg4);
}else{
fTransaction.show(fg4);
}
break;
}
fTransaction.commit();
}
//隱藏所有Fragment
private void hideAllFragment(FragmentTransaction fragmentTransaction){
if(fg1 != null)fragmentTransaction.hide(fg1);
if(fg2 != null)fragmentTransaction.hide(fg2);
if(fg3 != null)fragmentTransaction.hide(fg3);
if(fg4 != null)fragmentTransaction.hide(fg4);
}
}
3. 小結(jié)
- FragmentTransaction只能使用一次录择,每次使用都要調(diào)用FragmentManager 的beginTransaction()方法獲得FragmentTransaction事務(wù)對象哦!
- 實(shí)現(xiàn)底部導(dǎo)航欄的第二種方法:RadioGroup + RadioButton碗降,有了單選隘竭,我們 就不用像TextView一樣,每次點(diǎn)擊后先重置所有TextView的Selected狀態(tài)讼渊,再讓點(diǎn)擊的TextView 的Selected為true动看,