Android 하단 메뉴 표시 줄(RadioGroup+Fragment)미화

알다 시 피 안 드 로 이 드 의 아래쪽 메뉴 표시 줄 은 너무 중요 합 니 다.평소에 프로젝트 를 사용 해 야 하 는데 인터넷 에서 이 분야 에 관 한 demo 가 너무 못 생 겨 서 끔찍 합 니 다.그래서 여 기 는 RadioGroup+Fragment 방식 으로 하 나 를 썼 습 니 다.그리고 미화 도 했 습 니 다.필요 한 것 은 볼 수 있 습 니 다.
효과 그림:

프로젝트 구조

MainActivity.java

public class MainActivity extends AppCompatActivity {
 
 private FrameLayout frameLayout;
 private RadioGroup radioGroup;
 private Fragment[] mFragments;
 private int mIndex;
 
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initFragment();
  setRadioGroupListener();
 
 
 }
 
 private void initFragment() {
  radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
  frameLayout = (FrameLayout) findViewById(R.id.fl_content);
  HomeFragment homeFragment = new HomeFragment();
  ShopFragment shopFragment = new ShopFragment();
  LiveFragment liveFragment = new LiveFragment();
  ShoppingCarFragment shoppingCarFragment = new ShoppingCarFragment();
  MineFragment mineFragment = new MineFragment();
  //     
  mFragments = new Fragment[]{homeFragment, shopFragment, liveFragment, shoppingCarFragment, mineFragment};
  //    
  FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
  //    
  ft.add(R.id.fl_content, homeFragment).commit();
  //      0 
  setIndexSelected(0);
 }
 
 private void setIndexSelected(int index) {
  if (mIndex == index) {
   return;
  }
  FragmentManager fragmentManager = getSupportFragmentManager();
  FragmentTransaction ft = fragmentManager.beginTransaction();
  //  
  ft.hide(mFragments[mIndex]);
  //      
  if (!mFragments[index].isAdded()) {
   ft.add(R.id.fl_content, mFragments[index]).show(mFragments[index]);
  } else {
   ft.show(mFragments[index]);
  }
  ft.commit();
  //    
  mIndex = index;
 
 }
 
 private void setRadioGroupListener() {
  radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
   @Override
   public void onCheckedChanged(RadioGroup radioGroup, int i) {
    switch (i) {
     case R.id.rb_home:
      setIndexSelected(0);
      break;
     case R.id.rb_shop:
      setIndexSelected(1);
      break;
     case R.id.rb_live:
      setIndexSelected(2);
      break;
     case R.id.rb_shopping_car:
      setIndexSelected(3);
      break;
     case R.id.rb_mine:
      setIndexSelected(4);
      break;
     default:
      setIndexSelected(0);
      break;
    }
   }
  });
 }
 
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_BACK) {
   //  activity task (     activity)    ,        task  activity  ,
   //            HOME    ;                  ;
   moveTaskToBack(false);
   return true;
  }
  return super.onKeyDown(keyCode, event);
 }
}
Fragment,여 기 는 HomeFragment 만 표시 되 어 있 습 니 다.다른 것 은 모두 같 습 니 다.

public class HomeFragment extends BaseFragment {
 
 public HomeFragment() {
 }
 
 
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  View view=inflater.inflate(R.layout.fragment_home,container,false);
  return view;
 }
 
 
}
activity_main.xml 레이아웃 파일

<?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">
 
 <FrameLayout
  android:id="@+id/fl_content"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_above="@+id/line" />
 
 <View
  android:id="@+id/line"
  android:layout_width="match_parent"
  android:layout_height="@dimen/line_size"
  android:layout_above="@+id/radioGroup"
  android:background="#9e9e9e" />
 
 <RadioGroup
  android:id="@+id/radioGroup"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:clickable="true"
  android:gravity="center"
  android:orientation="horizontal"
  android:padding="3dp">
 
  <RadioButton
   android:id="@+id/rb_home"
   style="@style/RadioButtonStyle"
   android:checked="true"
   android:drawableTop="@drawable/btn_home"
   android:text="@string/home" />
 
  <RadioButton
   android:id="@+id/rb_shop"
   style="@style/RadioButtonStyle"
   android:drawableTop="@drawable/btn_shop"
   android:text="@string/shop" />
 
  <RadioButton
   android:id="@+id/rb_live"
   style="@style/RadioButtonStyle"
   android:drawableTop="@drawable/btn_live"
   android:text="@string/live" />
 
  <RadioButton
   android:id="@+id/rb_shopping_car"
   style="@style/RadioButtonStyle"
   android:drawableTop="@drawable/btn_shopping_car"
   android:text="@string/shopping_car" />
 
  <RadioButton
   android:id="@+id/rb_mine"
   style="@style/RadioButtonStyle"
   android:drawableTop="@drawable/btn_mine"
   android:text="@string/mine" />
 
 </RadioGroup>
 
</RelativeLayout>
RadioButton 스타일

<style name="RadioButtonStyle">
  <item name="android:layout_width">0dp</item>
  <item name="android:layout_weight">1</item>
  <item name="android:layout_height">match_parent</item>
  <item name="android:layout_marginRight">10dp</item>
  <item name="android:layout_marginLeft">10dp</item>
  <item name="android:button">@null</item>
  <item name="android:gravity">center</item>
  <item name="android:textColor">@color/color_radiobutton</item>
  <item name="android:textSize">10sp</item>
</style>
데모 다운로드 주소:하단 메뉴 표시 줄
따뜻 한 알림:앞으로 제 가 쓴 demo 는 모두 Android Studio 로 썼 습 니 다.Eclipse 학생 이 원 하면 스스로 고 쳐 야 합 니 다.시대 가 발전 하고 도구 도 업그레이드 되 고 있 습 니 다!
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기