麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > Android > 正文

使用RadioButton+Fragment實現底部導航欄效果

2019-10-21 21:49:45
字體:
來源:轉載
供稿:網友

底部導航欄,在我們App項目中是非常常用!而且實現它的方式很多,今天我們就來使用RadioButton+Fragment實現底部導航欄!

下面就讓我們動手吧,首先我們打開RadioButtonDemo這個項目,首先修改activity_main.xml文件如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.jackhu.radiobuttondemo.MainActivity"> <FrameLayout  android:id="@+id/mFragment"  android:layout_width="match_parent"  android:layout_height="0dp"  android:layout_weight="1"></FrameLayout> <RadioGroup  android:layout_marginBottom="2dp"  android:id="@+id/mRadioGroup"  android:orientation="horizontal"  android:layout_width="match_parent"  android:layout_height="48dp">  <RadioButton   android:drawableTop="@drawable/rbhome"   android:button="@null"   android:checked="true"   android:textColor="@color/colorRadioButtonP"   android:id="@+id/mRb_home"   android:gravity="center"   android:layout_width="0dp"   android:text="Home"   android:layout_weight="1"   android:layout_height="match_parent" />  <RadioButton   android:drawableTop="@drawable/rb_message"   android:button="@null"   android:textColor="@color/colorRadioButtonN"   android:id="@+id/mRb_message"   android:gravity="center"   android:layout_width="0dp"   android:text="Message"   android:layout_weight="1"   android:layout_height="match_parent" />  <RadioButton   android:drawableTop="@drawable/rbfind"   android:button="@null"   android:textColor="@color/colorRadioButtonN"   android:id="@+id/mRb_find"   android:gravity="center"   android:layout_width="0dp"   android:text="Find"   android:layout_weight="1"   android:layout_height="match_parent" />  <RadioButton   android:drawableTop="@drawable/rbmy"   android:button="@null"   android:textColor="@color/colorRadioButtonN"   android:id="@+id/mRb_my"   android:gravity="center"   android:layout_width="0dp"   android:text="My"   android:layout_weight="1"   android:layout_height="match_parent" /> </RadioGroup></LinearLayout>

這里我們在布局文件Fragment控件:用于顯示界面的切換。 

RadioGroup控件包含了4個RadioButton:用于顯示按鈕。我們給第一個按鈕check為true默認選中。其中android:button=”@null” 取消圓點。

drawableTop屬性:

<?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/home_p"/> <item android:drawable="@drawable/home_n"/></selector>

顯示選擇和未選中的狀態的圖標

創建Fragment,加載Fragment布局文件,類代碼如下:

package com.example.jackhu.radiobuttondemo.fragment;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import com.example.jackhu.radiobuttondemo.R;/** * A simple {@link Fragment} subclass. */public class HomeFragment extends Fragment { public HomeFragment() {  // Required empty public constructor } //單例模式 public static HomeFragment newInstance(){  HomeFragment homeFragment=new HomeFragment();  return homeFragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,        Bundle savedInstanceState) {  // Inflate the layout for this fragment  return inflater.inflate(R.layout.fragment_home, container, false); }}

接下來我們來修改MainActivity.class中的代碼,在這里實現點擊按鈕切換Fragment的具體功能,代碼如下:

package com.example.jackhu.radiobuttondemo;import android.support.annotation.IdRes;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.support.v4.content.ContextCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;import com.example.jackhu.radiobuttondemo.fragment.FindFragment;import com.example.jackhu.radiobuttondemo.fragment.HomeFragment;import com.example.jackhu.radiobuttondemo.fragment.MessageFragment;import com.example.jackhu.radiobuttondemo.fragment.MyFragment;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener { private RadioGroup mRadioGroup; private List<Fragment> fragments = new ArrayList<>(); private Fragment fragment; private FragmentManager fm; private FragmentTransaction transaction; private RadioButton rb_Home,rb_Message,rb_Find,rb_My; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  initView(); //初始化組件  mRadioGroup.setOnCheckedChangeListener(this); //點擊事件  fragments = getFragments(); //添加布局  //添加默認布局  normalFragment(); } //默認布局 private void normalFragment() {  fm=getSupportFragmentManager();  transaction=fm.beginTransaction();  fragment=fragments.get(0);  transaction.replace(R.id.mFragment,fragment);  transaction.commit(); } private void initView() {  mRadioGroup = (RadioGroup) findViewById(R.id.mRadioGroup);  rb_Home= (RadioButton) findViewById(R.id.mRb_home);  rb_Message= (RadioButton) findViewById(R.id.mRb_message);  rb_Find= (RadioButton) findViewById(R.id.mRb_find);  rb_My= (RadioButton) findViewById(R.id.mRb_my); } @Override public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {  fm=getSupportFragmentManager();  transaction=fm.beginTransaction();  switch (checkedId){   case R.id.mRb_home:    fragment=fragments.get(0);    transaction.replace(R.id.mFragment,fragment);    Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();    break;   case R.id.mRb_message:    fragment=fragments.get(1);    transaction.replace(R.id.mFragment,fragment);    Toast.makeText(this, "Message", Toast.LENGTH_SHORT).show();    break;   case R.id.mRb_find:    fragment=fragments.get(2);    transaction.replace(R.id.mFragment,fragment);    Toast.makeText(this, "Find", Toast.LENGTH_SHORT).show();    break;   case R.id.mRb_my:    fragment=fragments.get(3);    transaction.replace(R.id.mFragment,fragment);    Toast.makeText(this, "My", Toast.LENGTH_SHORT).show();    break;  }  setTabState();  transaction.commit(); } //設置選中和未選擇的狀態 private void setTabState() {  setHomeState();  setMessageState();  setFindState();  setMyState(); } private void setMyState() {  if (rb_My.isChecked()){   rb_My.setTextColor(ContextCompat.getColor(this,R.color.colorRadioButtonP));  }else{   rb_My.setTextColor(ContextCompat.getColor(this,R.color.colorRadioButtonN));  } } private void setFindState() {  if (rb_Find.isChecked()){   rb_Find.setTextColor(ContextCompat.getColor(this,R.color.colorRadioButtonP));  }else{   rb_Find.setTextColor(ContextCompat.getColor(this,R.color.colorRadioButtonN));  } } private void setMessageState() {  if (rb_Message.isChecked()){   rb_Message.setTextColor(ContextCompat.getColor(this,R.color.colorRadioButtonP));  }else{   rb_Message.setTextColor(ContextCompat.getColor(this,R.color.colorRadioButtonN));  } } private void setHomeState() {  if (rb_Home.isChecked()){   rb_Home.setTextColor(ContextCompat.getColor(this,R.color.colorRadioButtonP));  }else{   rb_Home.setTextColor(ContextCompat.getColor(this,R.color.colorRadioButtonN));  } } public List<Fragment> getFragments() {  fragments.add(new HomeFragment());  fragments.add(new MessageFragment());  fragments.add(new FindFragment());  fragments.add(new MyFragment());  return fragments; }}

好了,這樣的話,所有的代碼就已經完成了,可以運行一下看看完整的效果了最終效果圖:

RadioButton,Fragment,導航欄

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


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天天透天天狠天天爱综合97 | 久久国产精品久久久久久 | 精品xxxx户外露出视频 | 精品久久久久久成人av | 国产亚洲欧美日韩在线观看不卡 | 日韩精品中文字幕在线观看 | 日本黄色大片免费 | 色吧综合网 | 女教师~淫辱の动漫在线 | 欧美成人精品欧美一级乱黄 | 一区二区三区手机在线观看 | 中文字幕在线不卡视频 | 国产精品久久久久无码av | 国产精品久久久久久久av | 欧美高清一级片 | 午夜视频在线观看91 | 国产成人在线视频播放 | 极品一级片 | 欧美亚洲啪啪 | 欧美日韩一 | 91精品国产91久久久久久吃药 | 亚洲成人福利网站 | 毛片在哪里看 | 一级做a爱性色毛片免费1 | 久久久久久久久久亚洲 | 黄色片网站免费看 | 欧美精品免费一区二区三区 | av电影在线网 | 久久久涩| 99视频有精品视频高清 | 国产午夜精品久久久久 | 久久综合久久综合久久 | 国产精品观看在线亚洲人成网 | 九九黄色 | 国产精品久久久久久久久久东京 | 精品国产精品久久 | 国产精品一区二区三区99 | 26uuu成人人网图片 | 污黄视频在线观看 | 成人偷拍片视频在线观看 | 9999免费视频 |