您的位置:首页 > 其它

TabHost+TabWidget+FrameLayout实现底部菜单页面

2017-11-17 16:33 441 查看
           TabHost+TabWidget+FrameLayout实现主页面

网上搜了很多,大都是viewpager+tabHost,于是就研究了一下TabHost+TabWidget+FrameLayout实现主页面;

下面我们来看实现;

1.android studio上自己有v4包,我们直接来引用:

build.gradle:

dependencies {
...
compile 'com.android.support:support-v4:25.3.1-alpha1'
...
}


2.创建4个不同的fragment.xml和class类;

/**
* 四个layout一样,就不重写了
* fragment_1_layout.xml
* fragment_2_layout.xml
* fragment_3_layout.xml
* fragment_4_layout.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="match_parent"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="aaaaa"/>
</LinearLayout>

/**
* 创建不同的四个类继承Fragment,分别加载不同的view;
* AFragment.java
* BFragment.java
* CFragment.java
* DFragment.java
* 类加载一样,不通的类名加载不同的xml布局;
*/
public class AFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_1_layout, null);
return view;
}
}


3.以上初始工作做好了,下面我们来做具体实现,创建主xml和activity文件;

main_host_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white" >

<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >

<fragment
android:id="@+id/fragment_1_view"
android:name="com.android.application.AFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

<fragment
android:id="@+id/fragment_2_view"
android:name="com.android.application.BFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

<fragment
android:id="@+id/fragment_3_view"
android:name="com.android.application.CFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

<fragment
android:id="@+id/fragment_4_view"
android:name="com.android.application.DFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

</FrameLayout>

<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@null"
>
</TabWidget>
</LinearLayout>
</TabHost>
</RelativeLayout>


首先创建一个辅助类用来管理Activity;

public class
4000
Application  {
private static ArrayList<Activity> activities = new ArrayList<Activity>();

//添加activity
public static void addActivity(Activity acti) {
activities.add(acti);
}

//用来销毁activity
public static void removeActivity(Activity acti) {
int index = -1;
if ((index = activities.indexOf(acti)) != -1) {
activities.remove(index).finish();
}
}

//退出当前应用
public static void exitApp() {
// 1、关闭所Activity
for (Activity acti : activities) {
acti.finish();
}
// 2、退出Application
System.exit(0);

}
}


下面我们来看MainHostActivity主要逻辑实现;

public class MainHostActivity extends FragmentActivity {

private TabHost mTabHost;
private TabWidget mTabWidget;
private Timer timer;
private boolean mFirstServiceIsSelected;
private String tabIdFlag;
private int pressBackCount;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_host_activity);
findviews();
initViews();
initListeners();
Application.addActivity(this);
}

private void findviews() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabWidget = (TabWidget) findViewById(android.R.id.tabs);
timer = new Timer();
}

private void initViews() {

Drawable drawable = getResources().getDrawable(
R.drawable.function_inquiry_fisrt_normal);
int perWidth = (int) (getWindowManager().getDefaultDisplay().getWidth() / 4.0 + 0.4f);
int perHeight = (int) (perWidth * 0.8 / drawable.getIntrinsicWidth()
* drawable.getIntrinsicHeight() + 0.4f);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(perWidth,
perHeight);
mTabHost.setup();
ImageView image = null;
image = new ImageView(this);
image.setId(0x01);
image.setImageResource(R.drawable.fisrt_pressed);
image.setLayoutParams(lp);
mTabHost.addTab(mTabHost.newTabSpec("4").setIndicator(image)
.setContent(R.id.fragment_1_view));
image = new ImageView(this);
image.setId(0x02);
image.setImageResource(R.drawable.second_normal);
image.setLayoutParams(lp);
mTabHost.addTab(mTabHost.newTabSpec("3").setIndicator(image)
.setContent(R.id.fragment_2_view));
image = new ImageView(this);
image.setId(0x03);
image.setImageResource(R.drawable.thirdly_normal);
image.setLayoutParams(lp);
mTabHost.addTab(mTabHost.newTabSpec("2").setIndicator(image)
.setContent(R.id.fragment_3_view));
image = new ImageView(this);
image.setId(0x04);
image.setImageResource(R.drawable.fourthly_normal);
image.setLayoutParams(lp);
mTabHost.addTab(mTabHost.newTabSpec("1").setIndicator(image)
.setContent(R.id.fragment_4_view));
mTabHost.setCurrentTabByTag("4");
tabIdFlag = "4";
mFirstServiceIsSelected = true;
}

private void initListeners() {
mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
private boolean mSecondBoxIsSelected;
private boolean mThirdSpecialSellingIsSelected;
private boolean mForthBoxIsSelected;

@Override
public void onTabChanged(String tabId) {
switch (Integer.parseInt(tabId)) {
case 4:
tabIdFlag = tabId;
mTabWidget.setVisibility(View.VISIBLE);
if (!mFirstServiceIsSelected) {
((ImageView) mTabWidget.findViewById(0x01))
.setImageResource(R.drawable.fisrt_pressed);
mFirstServiceIsSelected = true;
((ImageView) mTabWidget.findViewById(0x02))
.setImageResource(R.drawable.second_normal);
mSecondBoxIsSelected = false;
((ImageView) mTabWidget.findViewById(0x03))
.setImageResource(R.drawable.thirdly_normal);
mThirdSpecialSellingIsSelected = false;
((ImageView) mTabWidget.findViewById(0x04))
.setImageResource(R.drawable.fourthly_normal);
mForthBoxIsSelected = false;
}
break;
case 3:
tabIdFlag = tabId;
mTabWidget.setVisibility(View.VISIBLE);
if (!mSecondBoxIsSelected) {
((ImageView) mTabWidget.findViewById(0x01))
.setImageResource(R.drawable.fisrt_normal);
mFirstServiceIsSelected = false;
((ImageView) mTabWidget.findViewById(0x02))
.setImageResource(R.drawable.second_pressed);
mSecondBoxIsSelected = true;
((ImageView) mTabWidget.findViewById(0x03))
.setImageResource(R.drawable.thirdly_normal);
mThirdSpecialSellingIsSelected = false;
((ImageView) mTabWidget.findViewById(0x04))
.setImageResource(R.drawable.fourthly_normal);
mForthBoxIsSelected = false;

}
break;
case 2:
tabIdFlag = tabId;
mTabWidget.setVisibility(View.VISIBLE);
if (!mThirdSpecialSellingIsSelected) {
((ImageView) mTabWidget.findViewById(0x01))
.setImageResource(R.drawable.fisrt_normal);
mFirstServiceIsSelected = false;
((ImageView) mTabWidget.findViewById(0x02))
.setImageResource(R.drawable.second_normal);
mSecondBoxIsSelected = false;
((ImageView) mTabWidget.findViewById(0x03))
.setImageResource(R.drawable.thirdly_pressed);
mThirdSpecialSellingIsSelected = true;
((ImageView) mTabWidget.findViewById(0x04))
.setImageResource(R.drawable.fourthly_normal);
mForthBoxIsSelected = false;

}
d8ef
break;
case 1:
tabIdFlag = tabId;
mTabWidget.setVisibility(View.VISIBLE);
if (!mForthBoxIsSelected) {
((ImageView) mTabWidget.findViewById(0x01))
.setImageResource(R.drawable.fisrt_normal);
mFirstServiceIsSelected = false;
((ImageView) mTabWidget.findViewById(0x02))
.setImageResource(R.drawable.second_normal);
mSecondBoxIsSelected = false;
((ImageView) mTabWidget.findViewById(0x03))
.setImageResource(R.drawable.thirdly_normal);
mThirdSpecialSellingIsSelected = false;
((ImageView) mTabWidget.findViewById(0x04))
.setImageResource(R.drawable.fourthly_pressed);
mForthBoxIsSelected = true;

}
break;
default:
break;
}
}
});
}
  //按下back键进行第一次提示,再次按退出
@Override
public void onBackPressed() {
String tabId = mTabHost.getCurrentTabTag();
if ("fifth".equals(tabId)) {
mTabHost.setCurrentTabByTag(tabIdFlag);
} else {
if (++pressBackCount == 1) {
Toast.makeText(this, "再按一下,退出程序", Toast.LENGTH_SHORT).show();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (pressBackCount == 1) {
pressBackCount = 0;
}
cancel();
}
}, 2000);
} else if (pressBackCount == 2) {
Application.exitApp();
}
}
}
}


不要忘记在AndroidManifest.xml进行注册:

<activity
android:name=".MainHostActivity"
android:roundIcon="@mipmap/ic_launcher_round">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>


以上就算是大功告成了;

附上一直图片,没有专业的美工,自己随便截的图用来代替,有点不好看:



源码下载http://download.csdn.net/download/xiao_yuanjl/10141240
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  tabhost 布局