安卓常见布局:RadioButton配合Fragment实现底部菜单栏切换


本次讲的布局特别常见,代码完成后和QQ底部是一个样子



我们现在开始


首先建立几个Fragment,这里只举一例

public class DeviceFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }
}



底部菜单栏的布局 bottom_tabbar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:layout_alignParentBottom="true"
    android:orientation="vertical">
    <include layout="@layout/line" />
    <RadioGroup
        android:background="@color/tabbg"
        android:id="@+id/m_RG_Content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/m_RB_Device"
            style="@style/bottomtab"
            android:drawableTop="@drawable/selector_tabbar_icon_device"
            android:text="@string/device" />
        <RadioButton
            android:id="@+id/m_RB_Gallery"
            style="@style/bottomtab"
            android:drawableTop="@drawable/selector_tabbar_icon_gallery"
            android:text="@string/gallery" />
        <RadioButton
            android:id="@+id/m_RB_Setting"
            style="@style/bottomtab"
            android:drawableTop="@drawable/selector_tabbar_icon_setting"
            android:text="@string/setting" />
    </RadioGroup>
</LinearLayout>



其中的style


<!-- 底端tab -->
    <style name="bottomtab">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_weight">1</item>
        <item name="android:button">@null</item>
        <item name="android:background">@null</item>
        <item name="android:drawablePadding">4dp</item>
        <item name="android:gravity">center</item>
        <item name="android:textColor">@drawable/selector_text_color</item>
        <item name="android:textSize">24sp</item>
    </style>
注意,background这个属性要写,否则会有系统自带的点击特效,如在6.0上有水波纹的点击特效


文字的selector selector_text_color.xml

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



图片的selector(这里只举一例) selector_tabbar_icon_device.xml

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



在Activity使用(这里使用了ButterKnife,如果不使用,可以改成findViewById(int id))

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    @BindView(R.id.frame_content)
    FrameLayout frameContent;
    @BindView(R.id.m_RG_Content)
    RadioGroup mRGContent;
    @BindView(R.id.m_RB_Device)
    RadioButton m_RB_Device;
    private FragmentManager mFragmentManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mFragmentManager = getSupportFragmentManager();
        mRGContent.setOnCheckedChangeListener(this);
        m_RB_Device.setChecked(true);
    }
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        Fragment fragment = FragmentFactory.getInstanceByIndex(checkedId);
        transaction.replace(R.id.frame_content, fragment);
        transaction.commit();
    }
}


Activity的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">
    <FrameLayout
        android:id="@+id/frame_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"/>
    <include layout="@layout/bottom_tabbar"/>
</RelativeLayout>



其中的FragmentFactory,用于创建Fragment对象并返回

public class FragmentFactory {
    public static Fragment getInstanceByIndex(int index) {
        Fragment fragment = null;
        switch (index) {
            case R.id.m_RB_Device:
                fragment = new DeviceFragment();
                break;
            case R.id.m_RB_Gallery:
                fragment = new GalleryFragment();
                break;
            case R.id.m_RB_Setting:
                fragment = new SettingFragment();
                break;
        }
        return fragment;
    }
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值