Fragment實(shí)例精講——底部導(dǎo)航欄的實(shí)現(xiàn)(方法3)
本節(jié)引言
前面我們已經(jīng)跟大家講解了實(shí)現(xiàn)底部導(dǎo)航欄的兩種方案,但是這兩種方案只適合普通的情況,如果 是像新浪微博那樣的,想在底部導(dǎo)航欄上的item帶有一個(gè)紅色的小點(diǎn),然后加上一個(gè)消息數(shù)目這樣, 前面兩種方案就顯得無(wú)力了,我們來(lái)看看別人的APP是怎么做的,打開手機(jī)的開發(fā)者選項(xiàng),勾選里面的:顯示布局邊界,然后打開我們參考的那個(gè)App,可以看到底部導(dǎo)航欄是這樣的:
從上面這個(gè)圖我們就可以看出,這種底部導(dǎo)航欄不是簡(jiǎn)單的TextView或者RadioGroup構(gòu)成的, 大概布局方案可能是:外層一個(gè)LinearLayout,中間一個(gè)RelativeLayout,而在中間有一個(gè)TextView, 然后再在TextView的右上角有一個(gè)紅色圓圈背景的TextView或者一個(gè)紅色的小點(diǎn); 大概就這樣,而這些小點(diǎn)平時(shí)的時(shí)候應(yīng)該設(shè)置的不可見,當(dāng)收到信息推送,即有相關(guān)類別信息的 時(shí)候再可見,并且顯示對(duì)應(yīng)的信息數(shù)目!那么下面我們就來(lái)實(shí)現(xiàn)下這種底部導(dǎo)航欄的效果, 另外,為了方便演示,這里就不演示Fragment的切換效果了!另外順道復(fù)習(xí)下Fragment獲得Activity 中的組件的知識(shí)點(diǎn)!
1.實(shí)現(xiàn)效果圖:
為了方便理解,這里通過(guò)點(diǎn)擊按鈕的形式,模擬收到推送信息,然后顯示紅色點(diǎn)!
運(yùn)行效果圖:
2.實(shí)現(xiàn)流程:
好的,接下來(lái)我們就來(lái)實(shí)現(xiàn)上面這個(gè)效果~
Step 1:相關(guān)資源文件的準(zhǔn)備:
和前面一樣,準(zhǔn)備好drawable系列的資源:
文字資源: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_selected="true" /> <item android:color="@color/text_gray" /> </selector>
圖標(biāo)資源:tab_menu_better.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@mipmap/tab_better_pressed" android:state_selected="true" /> <item android:drawable="@mipmap/tab_better_normal" /> </selector>
照著把其他三個(gè)也擼出來(lái)~!
Step 2:編寫activity的布局代碼:
因?yàn)樗膫€(gè)選項(xiàng)的TextView以及右上角的紅點(diǎn)數(shù)字屬性都差不多,如下:
<TextView android:id="@+id/tab_menu_channel" android:layout_marginTop="5dp" android:layout_width="wrap_content" android:layout_height="48dp" android:layout_centerInParent="true" android:textColor="@drawable/tab_menu_text" android:drawableTop="@drawable/tab_menu_channel" android:text="@string/tab_menu_alert"/> <TextView android:layout_width="20dp" android:layout_height="20dp" android:background="../style/images/bg_num" android:layout_toRightOf="@+id/tab_menu_channel" android:layout_marginLeft="-10dp" android:text="99+" android:textSize="12sp" android:gravity="center" android:textColor="@color/text_white"/>
我們將他們抽取出來(lái),寫到style.xml里:
<style name="tab_menu_text"> <item name="android:layout_marginTop">5dp</item> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">48dp</item> <item name="android:layout_centerInParent">true</item> <item name="android:textColor">@drawable/tab_menu_text</item> </style> <style name="tab_menu_bgnum"> <item name="android:layout_width">20dp</item> <item name="android:layout_height">20dp</item> <item name="android:background">@mipmap/bg_num</item> <item name="android:layout_marginLeft">-10dp</item> <item name="android:textSize">12sp</item> <item name="android:gravity">center</item> <item name="android:textColor">@color/text_white</item> </style>
然后開始編寫我們的activity.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" tools:context=".MainActivity"> <RelativeLayout android:id="@+id/ly_top_bar" android:layout_width="match_parent" android:layout_height="48dp" android:background="../style/images/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="../style/images/div_white" /> </RelativeLayout> <LinearLayout android:id="@+id/ly_tab_menu" android:layout_width="match_parent" android:layout_height="56dp" android:layout_alignParentBottom="true" android:background="../style/images/bg_white" android:orientation="horizontal"> <LinearLayout android:id="@+id/ly_tab_menu_channel" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:padding="5dp"> <TextView android:id="@+id/tab_menu_channel" style="@style/tab_menu_text" android:drawableTop="@drawable/tab_menu_channel" android:text="@string/tab_menu_alert" /> <TextView android:id="@+id/tab_menu_channel_num" style="@style/tab_menu_bgnum" android:layout_toRightOf="@+id/tab_menu_channel" android:text="99+" android:visibility="gone" /> </RelativeLayout> </LinearLayout> <LinearLayout android:id="@+id/ly_tab_menu_message" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:padding="5dp"> <TextView android:id="@+id/tab_menu_message" style="@style/tab_menu_text" android:drawableTop="@drawable/tab_menu_message" android:text="@string/tab_menu_profile" /> <TextView android:id="@+id/tab_menu_message_num" style="@style/tab_menu_bgnum" android:layout_toRightOf="@+id/tab_menu_message" android:text="99+" android:visibility="gone" /> </RelativeLayout> </LinearLayout> <LinearLayout android:id="@+id/ly_tab_menu_better" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:padding="5dp"> <TextView android:id="@+id/tab_menu_better" style="@style/tab_menu_text" android:drawableTop="@drawable/tab_menu_better" android:text="@string/tab_menu_pay" /> <TextView android:id="@+id/tab_menu_better_num" style="@style/tab_menu_bgnum" android:layout_toRightOf="@+id/tab_menu_better" android:text="99+" android:visibility="gone" /> </RelativeLayout> </LinearLayout> <LinearLayout android:id="@+id/ly_tab_menu_setting" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:padding="5dp"> <TextView android:id="@+id/tab_menu_setting" style="@style/tab_menu_text" android:drawableTop="@drawable/tab_menu_setting" android:text="@string/tab_menu_alert" /> <ImageView android:id="@+id/tab_menu_setting_partner" android:layout_width="12dp" android:layout_height="12dp" android:layout_marginLeft="-5dp" android:layout_toRightOf="@id/tab_menu_setting" android:visibility="gone" android:src="@mipmap/partner_red" /> </RelativeLayout> </LinearLayout> </LinearLayout> <View android:id="@+id/div_tab_bar" android:layout_width="match_parent" android:layout_height="2px" android:layout_above="@id/ly_tab_menu" android:background="../style/images/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"/> </RelativeLayout>
Step 3:編寫Fragment界面布局以及類
Fragment布局由四個(gè)普通按鈕構(gòu)成:
fg_my.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:orientation="vertical" android:padding="5dp"> <Button android:id="@+id/btn_one" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第一個(gè)顯示信息"/> <Button android:id="@+id/btn_two" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第二個(gè)顯示信息"/> <Button android:id="@+id/btn_three" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第三個(gè)顯示信息"/> <Button android:id="@+id/btn_four" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第四個(gè)顯示信息"/> </LinearLayout>
接著是自定義的Fragment類,這里的話我們通過(guò)getActivity.findViewById()來(lái)獲得Activity 中的小紅點(diǎn),這里僅僅是簡(jiǎn)單的控制顯示而已!MyFragment.java:
public class MyFragment extends Fragment implements View.OnClickListener{ private Context mContext; private Button btn_one; private Button btn_two; private Button btn_three; private Button btn_four; public MyFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fg_my,container,false); //UI Object btn_one = (Button) view.findViewById(R.id.btn_one); btn_two = (Button) view.findViewById(R.id.btn_two); btn_three = (Button) view.findViewById(R.id.btn_three); btn_four = (Button) view.findViewById(R.id.btn_four); //Bind Event btn_one.setOnClickListener(this); btn_two.setOnClickListener(this); btn_three.setOnClickListener(this); btn_four.setOnClickListener(this); return view; } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_one: TextView tab_menu_channel_num = (TextView) getActivity ().findViewById(R.id.tab_menu_channel_num); tab_menu_channel_num.setText("11"); tab_menu_channel_num.setVisibility(View.VISIBLE); break; case R.id.btn_two: TextView tab_menu_message_num = (TextView) getActivity ().findViewById(R.id.tab_menu_message_num); tab_menu_message_num.setText("20"); tab_menu_message_num.setVisibility(View.VISIBLE); break; case R.id.btn_three: TextView tab_menu_better_num = (TextView) getActivity ().findViewById(R.id.tab_menu_better_num); tab_menu_better_num.setText("99+"); tab_menu_better_num.setVisibility(View.VISIBLE); break; case R.id.btn_four: ImageView tab_menu_setting_partner = (ImageView) getActivity ().findViewById(R.id.tab_menu_setting_partner); tab_menu_setting_partner.setVisibility(View.VISIBLE); break; } } }
Step 4:編寫MainActivity
我們?cè)谶@里完成主要的邏輯實(shí)現(xiàn),有些部分和前面TextView實(shí)現(xiàn)底部導(dǎo)航欄的效果類似, 就不具體講解了,代碼如下:
MainActivity.java:
/** * Created by Coder-pig on 2015/8/30 0030. */ public class MainActivity extends AppCompatActivity implements View.OnClickListener { //Activity UI Object private LinearLayout ly_tab_menu_channel; private TextView tab_menu_channel; private TextView tab_menu_channel_num; private LinearLayout ly_tab_menu_message; private TextView tab_menu_message; private TextView tab_menu_message_num; private LinearLayout ly_tab_menu_better; private TextView tab_menu_better; private TextView tab_menu_better_num; private LinearLayout ly_tab_menu_setting; private TextView tab_menu_setting; private ImageView tab_menu_setting_partner; private FragmentManager fManager; private FragmentTransaction fTransaction; private MyFragment fg1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindViews(); ly_tab_menu_channel.performClick(); fg1 = new MyFragment(); fManager = getFragmentManager(); fTransaction = fManager.beginTransaction(); fTransaction.add(R.id.ly_content, fg1).commit(); } private void bindViews() { ly_tab_menu_channel = (LinearLayout) findViewById(R.id.ly_tab_menu_channel); tab_menu_channel = (TextView) findViewById(R.id.tab_menu_channel); tab_menu_channel_num = (TextView) findViewById(R.id.tab_menu_channel_num); ly_tab_menu_message = (LinearLayout) findViewById(R.id.ly_tab_menu_message); tab_menu_message = (TextView) findViewById(R.id.tab_menu_message); tab_menu_message_num = (TextView) findViewById(R.id.tab_menu_message_num); ly_tab_menu_better = (LinearLayout) findViewById(R.id.ly_tab_menu_better); tab_menu_better = (TextView) findViewById(R.id.tab_menu_better); tab_menu_better_num = (TextView) findViewById(R.id.tab_menu_better_num); ly_tab_menu_setting = (LinearLayout) findViewById(R.id.ly_tab_menu_setting); tab_menu_setting = (TextView) findViewById(R.id.tab_menu_setting); tab_menu_setting_partner = (ImageView) findViewById(R.id.tab_menu_setting_partner); ly_tab_menu_channel.setOnClickListener(this); ly_tab_menu_message.setOnClickListener(this); ly_tab_menu_better.setOnClickListener(this); ly_tab_menu_setting.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.ly_tab_menu_channel: setSelected(); tab_menu_channel.setSelected(true); tab_menu_channel_num.setVisibility(View.INVISIBLE); break; case R.id.ly_tab_menu_message: setSelected(); tab_menu_message.setSelected(true); tab_menu_message_num.setVisibility(View.INVISIBLE); break; case R.id.ly_tab_menu_better: setSelected(); tab_menu_better.setSelected(true); tab_menu_better_num.setVisibility(View.INVISIBLE); break; case R.id.ly_tab_menu_setting: setSelected(); tab_menu_setting.setSelected(true); tab_menu_setting_partner.setVisibility(View.INVISIBLE); break; } } //重置所有文本的選中狀態(tài) private void setSelected() { tab_menu_channel.setSelected(false); tab_menu_message.setSelected(false); tab_menu_better.setSelected(false); tab_menu_setting.setSelected(false); } }
好的,至此,就大功告成了~
3.代碼下載:
FragmentDemo3.zip:FragmentDemo3.zip下載
4.本節(jié)小結(jié):
好的,本節(jié)相比前面兩節(jié)稍微復(fù)雜了一點(diǎn),不過(guò)還是比較容易弄懂的! 另外,關(guān)于實(shí)現(xiàn)普通底部導(dǎo)航欄的實(shí)現(xiàn)例子就寫這么多吧,下一節(jié)開始我們來(lái)寫下 在此基礎(chǔ)上的根據(jù)手勢(shì)操作切換頁(yè)面的例子,嗯,就說(shuō)這么多,謝謝~