Android Studio 入门:(二) 主页面的Tab切换

 一、实现效果

点击不同的tab,出现不同的页面

Android Studio 入门:(二) 主页面的Tab切换_第1张图片  Android Studio 入门:(二) 主页面的Tab切换_第2张图片  Android Studio 入门:(二) 主页面的Tab切换_第3张图片

二、编码思路

Android Studio 入门:(二) 主页面的Tab切换_第4张图片

三、代码展示

//MainActivity
package com.e.spirit;

//import androidx.appcompat.app.AppCompatActivity;
//import androidx.fragment.app.Fragment;
//import androidx.fragment.app.FragmentManager;
//import androidx.fragment.app.FragmentTransaction;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;


import android.app.Fragment;
//import androidx.fragment.app.Fragment;


public class MainActivity extends Activity implements View.OnClickListener{

    private Fragment todayTabPage = new today_Fragment();
    private Fragment createTabPage = new create_Fragment();
    private Fragment myTabPage = new my_Fragment();

    private FragmentManager fm;


//    private LinearLayout mTodayTabPage;
//    private LinearLayout mCreateTabPage;
//    private LinearLayout mMyTabPage;

    private  LinearLayout mTodayTabButton;
    private  LinearLayout mCreateTabButton;
    private  LinearLayout mMyTabButton;

    private ImageButton mTodayImageButton;
    private ImageButton mCreateImageButton;
    private ImageButton mMyImageButton;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); //没有标题栏
        setContentView(R.layout.activity_main);
        initView();
        initFragment();
        selectFragment(0);
        initEvent();
    }

    private void  initFragment() {
        fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(R.id.id_content,todayTabPage);
        transaction.add(R.id.id_content,createTabPage);
        transaction.add(R.id.id_content,myTabPage);
        transaction.commit();

    }

    private void initView() {
//        mTodayTabPage = (LinearLayout) findViewById(R.id.id_tabPage_today);
//        mCreateTabPage = (LinearLayout) findViewById(R.id.id_tabPage_create);
//        mMyTabPage = (LinearLayout) findViewById(R.id.id_tabPage_create);

        mTodayImageButton=(ImageButton) findViewById(R.id.id_imgButton_today);
        mCreateImageButton=(ImageButton) findViewById(R.id.id_imgButton_create);
        mMyImageButton=(ImageButton) findViewById(R.id.id_imgButton_my);




        mTodayTabButton = (LinearLayout)findViewById(R.id.id_tabButton_today);
        mCreateTabButton = (LinearLayout)findViewById(R.id.id_tabButton_create);
        mMyTabButton = (LinearLayout)findViewById(R.id.id_tabButton_my);


    }

    private  void hideFragment(FragmentTransaction transaction) {
        transaction.hide(todayTabPage);
        transaction.hide(createTabPage);
        transaction.hide(myTabPage);

    }

    private void selectFragment(int i) {
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i) {
            case 0:
                transaction.show(todayTabPage);
                mTodayImageButton.setImageResource(R.drawable.tab_sun);
                break;
            case 1:
                transaction.show(createTabPage);
                mCreateImageButton.setImageResource(R.drawable.tab_create);
                break;
            case 2:
                transaction.show(myTabPage);
                mMyImageButton.setImageResource(R.drawable.tab_my);
                break;
            default:
                break;
        }
        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        resetImg();
        switch (v.getId()) {
            case R.id.id_tabButton_today:
                selectFragment(0);
                break;
            case R.id.id_tabButton_create:
                selectFragment(1);
                break;
            case R.id.id_tabButton_my:
                selectFragment(2);
                break;
            default:
                break;
        }
    }

    private  void initEvent() {
        mTodayTabButton.setOnClickListener(this);
        mCreateTabButton.setOnClickListener(this);
        mMyTabButton.setOnClickListener(this);

    }

    public  void  resetImg() {
        mTodayImageButton.setImageResource(R.drawable.tab_sun2);
        mCreateImageButton.setImageResource(R.drawable.tab_create2);
        mMyImageButton.setImageResource(R.drawable.tab_my2);

    }
}
//create_Fragment
package com.e.spirit;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

//import androidx.annotation.NonNull;
//import androidx.annotation.Nullable;
//import androidx.fragment.app.Fragment;

public class create_Fragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.create,null);
        return view;
    }
}




    

    






    

        

        
    

    

        

        
    

    

        

        
    




















四、学到

TextView控件在它的父布局里水平居中 android:layout_gravity="center_horizontal" 

你可能感兴趣的:(Android Studio 入门:(二) 主页面的Tab切换)