仿微信选项卡主页面创建

先看下实现效果图

输入图片说明

MainActivity代码

public class MainActivity extends BaseActivity implements
        RadioGroup.OnCheckedChangeListener {

    private RadioGroup radioGroup;
    private FrameLayout fragmentRoot;
    private HomeFragment homeFragment;
    private CardFragment cardFragment;
    private CollectionFragment collectionFragment;
    private SettingFragment settingFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);//软键盘弹出后页面整体上移
        initView();
        showFragment(0);

    }

    private void initView() {
        radioGroup = (RadioGroup) findViewById(R.id.bottomRg);
        radioGroup.setOnCheckedChangeListener(this);
        fragmentRoot = (FrameLayout) findViewById(R.id.fragmentRoot);
        homeFragment = new HomeFragment();
        cardFragment=new CardFragment();
        collectionFragment=new CollectionFragment();
        settingFragment=new SettingFragment();
    }

    private void showFragment(int index) {
        Fragment fragment = null;
        switch (index) {
            case 0:
                fragment = homeFragment;
                break;
            case 1:
                fragment = cardFragment;
                break;
            case 2:
                fragment = collectionFragment;
                break;
            case 3:
                fragment = settingFragment;
                break;
            default:
                break;
        }
        FragmentManager fm =getFragmentManager();// 获取Fragment管理器
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragmentRoot, fragment);
        ft.commit();

    }

    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int selectIndex = 0;
        switch (checkedId) {
            case R.id.rbOne:
                selectIndex = 0;
                break;
            case R.id.rbTwo:
                selectIndex = 1;
                break;
            case R.id.rbThree:
                selectIndex = 2;
                break;
            case R.id.rbFour:
                selectIndex = 3;
                break;
        }
        showCurTab(selectIndex, checkedId, true);
    }

    private void showCurTab(int index, int checkedId, boolean isSelect) {
        radioGroup.check(checkedId);
        showFragment(index);
        for (int i = 0; i < radioGroup.getChildCount(); i++) {
            if (i == index) {
                changeTabImage(i, true);
            } else {
                changeTabImage(i, false);
            }
        }
    }

    private void changeTabImage(int index, boolean isSelect) {
        RadioButton rButton = (RadioButton) radioGroup.getChildAt(index);
        switch (index) {
            case 0:
                if (isSelect) {
                    rButton.setCompoundDrawablesWithIntrinsicBounds(0,
                            R.drawable.home_select, 0, 0);
                    rButton.setTextColor(getResources().getColor(
                            R.color.color_white));
                } else {
                    rButton.setCompoundDrawablesWithIntrinsicBounds(0,
                            R.drawable.home, 0, 0);
                    rButton.setTextColor(getResources()
                            .getColor(R.color.gray_color));
                }
                break;
            case 1:
                if (isSelect) {
                    rButton.setCompoundDrawablesWithIntrinsicBounds(0,
                            R.drawable.card_select, 0, 0);
                    rButton.setTextColor(getResources().getColor(
                            R.color.color_white));
                } else {
                    rButton.setCompoundDrawablesWithIntrinsicBounds(0,
                            R.drawable.card, 0, 0);
                    rButton.setTextColor(getResources()
                            .getColor(R.color.gray_color));
                }
                break;
            case 2:
                if (isSelect) {
                    rButton.setCompoundDrawablesWithIntrinsicBounds(0,
                            R.drawable.collection_select, 0, 0);
                    rButton.setTextColor(getResources().getColor(
                            R.color.color_white));
                } else {
                    rButton.setCompoundDrawablesWithIntrinsicBounds(0,
                            R.drawable.collection, 0, 0);
                    rButton.setTextColor(getResources()
                            .getColor(R.color.gray_color));
                }
                break;
            case 3:
                if (isSelect) {
                    rButton.setCompoundDrawablesWithIntrinsicBounds(0,
                            R.drawable.me_select, 0, 0);
                    rButton.setTextColor(getResources().getColor(
                            R.color.color_white));
                } else {
                    rButton.setCompoundDrawablesWithIntrinsicBounds(0,
                            R.drawable.me, 0, 0);
                    rButton.setTextColor(getResources()
                            .getColor(R.color.gray_color));
                }
                break;

        }
    }


    int waitTime = 2000;
    long touchTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.getAction() == KeyEvent.ACTION_DOWN && KeyEvent.KEYCODE_BACK == keyCode) {
            long currentTime = System.currentTimeMillis();
            if((currentTime-touchTime)>=waitTime) {
                //让Toast的显示时间和等待时间相同
                Toast.makeText(this, "再按一次退出", Toast.LENGTH_LONG).show();
                touchTime = currentTime;
            }else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}

activity_main.xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:background="@color/color_white"
    android:orientation="vertical"
    tools:context=".Activity.MainActivity">
    <FrameLayout
        android:id="@+id/fragmentRoot"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_weight="1">
    </FrameLayout>
    <FrameLayout
    android:id="@+id/fl_main"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/activity_title_bar_color"
    android:layout_alignParentBottom="true" >
    <RadioGroup
            android:id="@+id/bottomRg"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:paddingTop="8dp" >
            <RadioButton
                android:id="@+id/rbOne"
                style="@style/footbar"
                android:layout_weight="1"
                android:checked="true"
                android:drawableTop="@drawable/home_button_select"
                android:text="@string/fragment_frist_page"
                android:textColor="@color/color_white"
                android:background="@android:color/transparent"
                android:textSize="@dimen/tab_text_size" />
            <RadioButton
                android:id="@+id/rbTwo"
                style="@style/footbar"
                android:layout_weight="1"
                android:drawableTop="@drawable/card"
                android:text="@string/fragment_card_page"
                android:background="@android:color/transparent"
                android:textColor="@color/gray_color"
                android:textSize="@dimen/tab_text_size" />
            <RadioButton
                android:id="@+id/rbThree"
                style="@style/footbar"
                android:layout_weight="1"
                android:drawableTop="@drawable/collection"
                android:text="@string/fragment_collection_page"
                android:background="@android:color/transparent"
                android:textColor="@color/gray_color"
                android:textSize="@dimen/tab_text_size" />
            <RadioButton
                android:id="@+id/rbFour"
                style="@style/footbar"
                android:layout_weight="1"
                android:drawableTop="@drawable/me"
                android:text="@string/fragment_me_page"
                android:background="@android:color/transparent"
                android:textColor="@color/gray_color"
                android:textSize="@dimen/tab_text_size" />
        </RadioGroup>
    </FrameLayout>
</LinearLayout>

res - -》values - -》styles.xml 文件中添加style标签

  <style name="footbar">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:textSize">10sp</item>
        <item name="android:textColor">#FFFFFF</item> <!--白色-->
        <item name="android:singleLine">true</item>
        <item name="android:ellipsize">marquee</item>
        <item name="android:button">@null</item>
        <item name="android:gravity">bottom|center_horizontal</item>
        <item name="android:paddingBottom">2dp</item>
    </style>

转载于:https://my.oschina.net/fltsp/blog/896568

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值