ViewPage+tabWidget

Android 同时被 2 个专栏收录
4 篇文章 0 订阅
1 篇文章 0 订阅

在这里插入图片描述在这里插入图片描述

xml

<TabHost 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"
    android:id="@+id/tab_host">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.viewpager.widget.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/tab_divider">
        </androidx.viewpager.widget.ViewPager>
        <FrameLayout
            android:visibility="gone"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@android:id/tabcontent"></FrameLayout>
        <View
            android:id="@+id/tab_divider"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#dfdfdf"
            android:layout_above="@android:id/tabs"
            />
        <TabWidget
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:id="@android:id/tabs"
            android:layout_alignParentBottom="true"
           ></TabWidget>
    </RelativeLayout>
</TabHost>

**

MainActivity.java

package com.example.freeprogramma;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.example.freeprogramma.fragment.AllNoteFragment;
import com.example.freeprogramma.fragment.NewNoteFragment;
import com.example.freeprogramma.fragment.UserFragment;

public class MainActivity extends AppCompatActivity implements TabHost.TabContentFactory {

    private View mView;
    private ImageView mIcon;
    private TextView mTitle;
    private RelativeLayout mTabBg;
    private int[] mDrawableIds;
    private int[] mTitleIDs;
    private TabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //设置底部menu
        setMenuTab();

    }

    /**
     * 设置底部menu
     */
    private void setMenuTab() {
        //初始化总布局
        mTabHost = findViewById(R.id.tab_host);
        mTabHost.setup();

        //初始化数据
        mTitleIDs = new int[]{R.string.all_note,R.string.new_note,R.string.my_msg};
        mDrawableIds = new int[]{R.mipmap.allnote,R.mipmap.plus_circle,R.mipmap.user};
        AllNoteFragment allNoteFragment = new AllNoteFragment();
        NewNoteFragment newNoteFragment = new NewNoteFragment();
        UserFragment userFragment = new UserFragment();
        final Fragment[] fragments = new Fragment[]{allNoteFragment,newNoteFragment,userFragment};

        //对三个tab做处理
        for (int index = 0; index < mTitleIDs.length; index++) {
            //获取all_note_layout并从其布局中获取ImageView 、TextView、LinearLayout控件
            mView = getLayoutInflater().inflate(R.layout.all_note_layout,null,false);
            mIcon = mView.findViewById(R.id.all_note_tab_icon);
            mTitle = mView.findViewById(R.id.all_note_tab_txt);
            mTabBg = mView.findViewById(R.id.tab_bg);
            //1.设置文本 2.设置图标 3.设置背景颜色
            mTitle.setText(mTitleIDs[index]);
            mIcon.setImageResource(mDrawableIds[index]);
            mTabBg.setBackgroundColor(getResources().getColor(R.color.white));
            //添加Tab
            mTabHost.addTab(
                   mTabHost.newTabSpec(getString(mTitleIDs[index]))
                    .setIndicator(mView)
                    .setContent(this)
            );
        }
        final ViewPager viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return fragments.length;
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(mTabHost!=null){
                    mTabHost.setCurrentTab(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String s) {
                if(mTabHost!=null){
                    int position = mTabHost.getCurrentTab();
                    viewPager.setCurrentItem(position);
                }
            }
        });


    }

    @Override
    public View createTabContent(String s) {
        View view = new View(this);
        return view;
    }
}
``

**
![在这里插入图片描述](https://img-blog.csdnimg.cn/2019110822502682.png)
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值