国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android使用RadioGroup實現底部導航欄

2019-10-22 18:28:58
字體:
來源:轉載
供稿:網友

RadioGroup實現底部導航欄效果,如圖::

 RadioGroup底部導航欄,RadioGroup導航欄,android導航欄

實現可最基本的導航欄功能,不能左右滑動,只能點擊

1.內嵌的fragment的布局:

<?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"> <TextView  android:layout_width="match_parent"  android:layout_height="match_parent"  android:gravity="center"  android:textSize="50sp"  android:textColor="@color/colorPrimary"  android:text="home"/></LinearLayout>

2.fragment的activity代碼:

public class FrHome extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {  View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_home, container, false);  return view; }}

以此為例根據需要編寫不同的fragment布局等等。

3.裝載fragment的界面布局如下(其中使用了selector進行實現點擊改變圖標和文字顏色):

點擊改變文字顏色:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:color="#3F51B5"/> <item android:state_checked="false" android:color="#8f8f8f"/></selector>

點擊改變圖標:

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

界面布局:

 

<?xml version="1.0" encoding="utf-8"?><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" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context="com.lotus.chartspagedemo.ActHome"> <FrameLayout  android:id="@+id/frame_layout"  android:layout_width="match_parent"  android:layout_above="@+id/card_view"  android:layout_height="match_parent"/> <android.support.v7.widget.CardView  android:id="@+id/card_view"  app:cardElevation="25dp"  android:layout_alignParentBottom="true"  android:layout_width="match_parent"  android:layout_height="wrap_content">  <RadioGroup   android:paddingTop="5dp"   android:id="@+id/tab_bar"   android:background="@color/app_white"   android:layout_width="match_parent"   android:layout_height="60dp"   android:gravity="center"   android:orientation="horizontal">   <RadioButton    android:id="@+id/tab_home"    android:gravity="center"    android:button="@null"    android:drawableTop="@drawable/selector_tab_home"    android:layout_width="0dp"    android:layout_weight="1"    android:layout_height="match_parent"    android:textColor="@drawable/selector_tab_color"    android:text="首頁"/>   <RadioButton    android:id="@+id/tab_health"    android:gravity="center"    android:button="@null"    android:drawableTop="@drawable/selector_tab_health"    android:layout_width="0dp"    android:layout_weight="1"    android:layout_height="match_parent"    android:textColor="@drawable/selector_tab_color"    android:text="體檢測評" />   <RadioButton    android:id="@+id/tab_personal"    android:gravity="center"    android:button="@null"    android:drawableTop="@drawable/selector_tab_personal"    android:layout_width="0dp"    android:layout_weight="1"    android:layout_height="match_parent"    android:textColor="@drawable/selector_tab_color"    android:text="個人中心"    />  </RadioGroup> </android.support.v7.widget.CardView></RelativeLayout>

4.裝載fragment的界面的activity代碼(加入雙擊返回鍵則退出應用):

public class ActHome extends FragmentActivity implements RadioGroup.OnCheckedChangeListener { @BindView(R.id.frame_layout) FrameLayout frameLayout; @BindView(R.id.tab_home) RadioButton tabHome; @BindView(R.id.tab_health) RadioButton tabHealth; @BindView(R.id.tab_personal) RadioButton tabPersonal; @BindView(R.id.tab_bar) RadioGroup tabBar; public final static String ACTION_EXIT_SYSTEM = "sys_exit"; private FragmentManager manager; private FragmentTransaction transaction; private FrHome frHome; private FrHealth frHealth; private FrPersonal frPersonal; private long mExitTime; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_home);  ButterKnife.bind(this);  RadioButton tabHome = (RadioButton) tabBar.getChildAt(0);  tabHome.setChecked(true);  tabBar.setOnCheckedChangeListener(this);  initFragment(); } private void initFragment() {  manager = getSupportFragmentManager();  transaction = manager.beginTransaction();  frHome = new FrHome();  transaction.add(R.id.frame_layout,frHome);  transaction.commit(); } @Override public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) {  switch (checkedId) {   case R.id.tab_home:    FragmentTransaction ft1 = manager.beginTransaction();    hideAll(ft1);    if (frHome!=null){     ft1.show(frHome);    }else {     frHome=new FrHome();     ft1.add(R.id.frame_layout,frHome);    }    ft1.commit();    break;   case R.id.tab_health:    FragmentTransaction ft2 = manager.beginTransaction();    hideAll(ft2);    if (frHealth!=null){     ft2.show(frHealth);    }else {     frHealth = new FrHealth();     ft2.add(R.id.frame_layout,frHealth);    }    ft2.commit();    break;   case R.id.tab_personal:    FragmentTransaction ft5 = manager.beginTransaction();    hideAll(ft5);    if (frPersonal!=null){     ft5.show(frPersonal);    }else {     frPersonal = new FrPersonal();     ft5.add(R.id.frame_layout, frPersonal);    }    ft5.commit();    break;  } } private void hideAll(FragmentTransaction ft){  if (ft==null){   return;  }  if (frHome!=null){   ft.hide(frHome);  }  if (frHealth!=null){   ft.hide(frHealth);  }  if (frPersonal!=null){   ft.hide(frPersonal);  } } @Override public void onBackPressed() {  if ((System.currentTimeMillis() - mExitTime) > 2000) {   Toast.makeText(ActHome.this,"再按一次退出程序",Toast.LENGTH_SHORT).show();   mExitTime = System.currentTimeMillis();  } else {   new Handler().postDelayed(new Runnable() {    @Override    public void run() {     onExit(ActHome.this);    }   }, 500);  } } public static void onExit(final Context context) {  try {   Intent intent = new Intent();   intent.setAction(context.getApplicationContext().getPackageName() + ACTION_EXIT_SYSTEM);   context.sendBroadcast(intent);   // MobclickAgent.onKillProcess(context);   new Handler().postDelayed(new Runnable() {    @Override    public void run() {     System.exit(0);    }   }, 200);  } catch (Exception e) {   e.printStackTrace();  } }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西城区| 仁布县| 达拉特旗| 青冈县| 南陵县| 玉龙| 南岸区| 黄浦区| 安宁市| 乌拉特前旗| 龙胜| 海安县| 余庆县| 宜宾县| 永顺县| 临桂县| 日喀则市| 陈巴尔虎旗| 江都市| 洛浦县| 宁陵县| 松原市| 翁源县| 九台市| 呼图壁县| 年辖:市辖区| 贵阳市| 宣化县| 法库县| 瓦房店市| 石城县| 涞水县| 温宿县| 蒙自县| 泾阳县| 天柱县| 井陉县| 革吉县| 静海县| 桃江县| 彰化县|