怎么在RadioButton中使用Fragment實(shí)現(xiàn)一個(gè)底部導(dǎo)航欄效果?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
創(chuàng)新互聯(lián)客戶idc服務(wù)中心,提供德陽電信服務(wù)器托管、成都服務(wù)器、成都主機(jī)托管、成都雙線服務(wù)器等業(yè)務(wù)的一站式服務(wù)。通過各地的服務(wù)中心,我們向成都用戶提供優(yōu)質(zhì)廉價(jià)的產(chǎn)品以及開放、透明、穩(wěn)定、高性價(jià)比的服務(wù),資深網(wǎng)絡(luò)工程師在機(jī)房提供7*24小時(shí)標(biāo)準(zhǔn)級(jí)技術(shù)保障。
首先我們打開RadioButtonDemo這個(gè)項(xiàng)目,首先修改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>
這里我們?cè)诓季治募﨔ragment控件:用于顯示界面的切換。
RadioGroup控件包含了4個(gè)RadioButton:用于顯示按鈕。我們給第一個(gè)按鈕check為true默認(rèn)選中。其中android:button=”@null” 取消圓點(diǎn)。
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>
顯示選擇和未選中的狀態(tài)的圖標(biāo)
創(chuàng)建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中的代碼,在這里實(shí)現(xiàn)點(diǎn)擊按鈕切換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); //點(diǎn)擊事件 fragments = getFragments(); //添加布局 //添加默認(rèn)布局 normalFragment(); } //默認(rèn)布局 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(); } //設(shè)置選中和未選擇的狀態(tài) 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; } }
關(guān)于怎么在RadioButton中使用Fragment實(shí)現(xiàn)一個(gè)底部導(dǎo)航欄效果問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
網(wǎng)站名稱:怎么在RadioButton中使用Fragment實(shí)現(xiàn)一個(gè)底部導(dǎo)航欄效果
分享網(wǎng)址:http://muchs.cn/article32/iejdpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、網(wǎng)站排名、網(wǎng)站維護(hù)、網(wǎng)站建設(shè)、域名注冊(cè)、全網(wǎng)營(yíng)銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)