Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Apply a fixed frame to use when you create a navigation bar at the bottom TabActivity, the following are the main code and the Notes an example.
- xml file.
<?xml version="1.0" encoding="utf-8"?>
<!--根布局必须是TabHost id必须为@android:id/tabhost-->
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabhost">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--页面布局都挂在这个框架下-->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/tab_bar_height"/>
<!--id必须为@android:id/tabs-->
<TabWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="@android:id/tabs"/>
<!--实际的底部标签-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/tab_bar_height"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:orientation="horizontal">
<!--第一个标签-->
<LinearLayout
android:id="@+id/ll_first"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
style="@style/TabButton"
android:drawableTop="@drawable/tab_first_selector"
android:text="@string/tab_first"/>
</LinearLayout>
<!--第二个标签-->
<LinearLayout
android:id="@+id/ll_second"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
style="@style/TabButton"
android:drawableTop="@drawable/tab_second_selector"
android:text="@string/tab_second"/>
</LinearLayout>
<!--第三个标签-->
<LinearLayout
android:id="@+id/ll_third"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
style="@style/TabButton"
android:drawableTop="@drawable/tab_third_selector"
android:text="@string/tab_third"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</TabHost>
- Java code.
package xyz.strasae.androidlearn.group;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TabHost;
@SuppressWarnings("all")
public class TabActivityDemo extends TabActivity implements View.OnClickListener {
private String TAG = "TabActivityDemo";
private TabHost tabHost;
private Bundle bundle = new Bundle();
private LinearLayout ll_first, ll_second, ll_third;
private String FIRST_TAG = "first";//第一个标签的标识串
private String SECOND_TAG = "second";//第二个标签的标识串
private String THIRD_TAG = "third";//第三个标签的标识串
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bundle.putString("tag", TAG);
setContentView(R.layout.activity_tab_demo);
ll_first = findViewById(R.id.ll_first);
ll_second = findViewById(R.id.ll_second);
ll_third = findViewById(R.id.ll_third);
ll_first.setOnClickListener(this);
ll_second.setOnClickListener(this);
ll_third.setOnClickListener(this);
//获取tabhost
tabHost = this.getTabHost();
//添加第一个标签
tabHost.addTab(getNewTab(FIRST_TAG, R.string.tab_first, R.drawable.tab_first_selector, TabFirstActivity.class));
tabHost.addTab(getNewTab(SECOND_TAG, R.string.tab_second, R.drawable.tab_first_selector, TabSecondActivity.class));
tabHost.addTab(getNewTab(THIRD_TAG, R.string.tab_third, R.drawable.tab_first_selector, TabThirdActivity.class));
changeContainerView(ll_first);
}
@Override
public void onClick(View view) {
changeContainerView(view);
}
private TabHost.TabSpec getNewTab(String spec, int label, int icon, Class<?> cls) {
Intent intent = new Intent(this, cls).putExtras(bundle);
return tabHost.newTabSpec(spec).setContent(intent).setIndicator(this.getString(label), this.getResources().getDrawable(icon));
}
private void changeContainerView(View view) {
ll_first.setSelected(false);
ll_second.setSelected(false);
ll_third.setSelected(false);
view.setSelected(true);
if (view.getId() == R.id.ll_first) {
//根据标签设置
tabHost.setCurrentTabByTag(FIRST_TAG);
} else if(view.getId() == R.id.ll_second) {
tabHost.setCurrentTabByTag(SECOND_TAG);
} else if(view.getId() == R.id.ll_third) {
tabHost.setCurrentTabByTag(THIRD_TAG);
}
}
}