編輯:Android技術基礎
前面我們已經跟大家講解了實現底部導航欄的兩種方案,但是這兩種方案只適合普通的情況,如果 是像新浪微博那樣的,想在底部導航欄上的item帶有一個紅色的小點,然後加上一個消息數目這樣, 前面兩種方案就顯得無力了,我們來看看別人的APP是怎麼做的,打開手機的開發者選項,勾選裡面的: 顯示布局邊界,然後打開我們參考的那個App,可以看到底部導航欄是這樣的:
從上面這個圖我們就可以看出,這種底部導航欄不是簡單的TextView或者RadioGroup構成的, 大概布局方案可能是:外層一個LinearLayout,中間一個RelativeLayout,而在中間有一個TextView, 然後再在TextView的右上角有一個紅色圓圈背景的TextView或者一個紅色的小點; 大概就這樣,而這些小點平時的時候應該設置的不可見,當收到信息推送,即有相關類別信息的 時候再可見,並且顯示對應的信息數目!那麼下面我們就來實現下這種底部導航欄的效果, 另外,為了方便演示,這裡就不演示Fragment的切換效果了!另外順道復習下Fragment獲得Activity 中的組件的知識點!
為了方便理解,這裡通過點擊按鈕的形式,模擬收到推送信息,然後顯示紅色點!
運行效果圖:
好的,接下來我們就來實現上面這個效果~
和前面一樣,准備好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>
圖標資源: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>
照著把其他三個也撸出來~!
因為四個選項的TextView以及右上角的紅點數字屬性都差不多,如下:
<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="@mipmap/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"/>
我們將他們抽取出來,寫到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="@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> <LinearLayout android:id="@+id/ly_tab_menu" android:layout_width="match_parent" android:layout_height="56dp" android:layout_alignParentBottom="true" android:background="@color/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" android:drawableTop="@drawable/tab_menu_channel" android:text="@string/tab_menu_alert" /> <TextView android:id="@+id/tab_menu_channel_num" 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" android:drawableTop="@drawable/tab_menu_message" android:text="@string/tab_menu_profile" /> <TextView android:id="@+id/tab_menu_message_num" 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" android:drawableTop="@drawable/tab_menu_better" android:text="@string/tab_menu_pay" /> <TextView android:id="@+id/tab_menu_better_num" 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" 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="@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"/> </RelativeLayout>
Fragment布局由四個普通按鈕構成:
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="第一個顯示信息"/> <Button android:id="@+id/btn_two" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第二個顯示信息"/> <Button android:id="@+id/btn_three" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第三個顯示信息"/> <Button android:id="@+id/btn_four" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第四個顯示信息"/> </LinearLayout>
接著是自定義的Fragment類,這裡的話我們通過getActivity.findViewById()來獲得Activity 中的小紅點,這裡僅僅是簡單的控制顯示而已! 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; } } }
我們在這裡完成主要的邏輯實現,有些部分和前面TextView實現底部導航欄的效果類似, 就不具體講解了,代碼如下:
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; } } //重置所有文本的選中狀態 private void setSelected() { tab_menu_channel.setSelected(false); tab_menu_message.setSelected(false); tab_menu_better.setSelected(false); tab_menu_setting.setSelected(false); } }
好的,至此,就大功告成了~
FragmentDemo3.zip:FragmentDemo3.zip下載
好的,本節相比前面兩節稍微復雜了一點,不過還是比較容易弄懂的! 另外,關於實現普通底部導航欄的實現例子就寫這麼多吧,下一節開始我們來寫下 在此基礎上的根據手勢操作切換頁面的例子,嗯,就說這麼多,謝謝~
本節引言:本節,我們繼續來研究Service(服務)組件,本節將會學習下Android中的AIDL跨進程通信的一些概念,並不深入到源碼層次,暫時知道
Android有兩種拍照方法,一種是直接調用系統的照相Intent,使用 onActivityResult獲取圖片資源或者指定圖片路徑,拍照返回成功後去指定路徑讀取圖片;
本節引言:本節帶來的是Android 3.0後引入的一個UI控件——ViewPager(視圖滑動切換工具),實在想不到如何來稱呼這個控件,他的大概功
我們可以通過設置ACTION_CALL或者ACTION_DIAL完成在Android中進行電話撥號。二者區別在於:(1)ACTION_CALL:直接撥號;(2)ACTIO