一、实现效果
点击不同的tab,出现不同的页面
二、编码思路
三、代码展示
//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;
}
}
<!--activity_main.xml-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/id_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></FrameLayout>
<include layout="@layout/bottom" />
</LinearLayout>
<!--bottom.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="100dp"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/id_tabButton_today"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:orientation="vertical">
<ImageButton
android:id="@+id/id_imgButton_today"
android:clickable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
android:contentDescription="@string/app_name"
android:src="@drawable/tab_sun"></ImageButton>
<TextView
android:id="@+id/id_text_today"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="TODAY"></TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/id_tabButton_create"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageButton
android:id="@+id/id_imgButton_create"
android:clickable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
android:contentDescription="@string/app_name"
android:src="@drawable/tab_create2"></ImageButton>
<TextView
android:id="@+id/id_text_create"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="CREATE"></TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/id_tabButton_my"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageButton
android:id="@+id/id_imgButton_my"
android:clickable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
android:contentDescription="@string/app_name"
android:src="@drawable/tab_my2"></ImageButton>
<TextView
android:id="@+id/id_text_my"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="MY"></TextView>
</LinearLayout>
<!-- <Button-->
<!-- android:id="@+id/id_tabButton_today"-->
<!-- android:layout_width="0dp"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_weight="1"-->
<!-- android:text="Today" />-->
<!-- <Button-->
<!-- android:id="@+id/id_tabButton_create"-->
<!-- android:layout_width="0dp"-->
<!-- android:layout_weight="1"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:text="Create" />-->
<!-- <Button-->
<!-- -->
<!-- android:layout_weight="1"-->
<!-- android:layout_width="0dp"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:text="MY" />-->
</LinearLayout>
四、学到
让TextView控件在它的父布局里水平居中 android:layout_gravity="center_horizontal"