Android 하단 메뉴 표시 줄(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 학생 이 원 하면 스스로 고 쳐 야 합 니 다.시대 가 발전 하고 도구 도 업그레이드 되 고 있 습 니 다!
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.