@gogogodeng
2017-08-10T13:43:48.000000Z
字数 2635
阅读 410
android控件
tabhost就是一个tab选项卡切换控件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabHost
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/tabHost" android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/loll01"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView10"
android:src="@drawable/loll02"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView11"
android:src="@drawable/loll03"
android:layout_gravity="center"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
其中tabs和tabcontent这2个id不能更改
package com.testapp.app;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
/**
* Created by develop on 2015/11/17.
*/
public class TabHostActivity extends Activity {
private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost);
tabHost = (TabHost) this.findViewById(R.id.tabHost);
tabHost.setup();
tabHost.addTab(tabHost.newTabSpec("tab_1")
.setContent(R.id.tab1)
.setIndicator("TAB1", null));
tabHost.addTab(tabHost.newTabSpec("tab_2")
.setContent(R.id.tab2)
.setIndicator("TAB2", null));
tabHost.addTab(tabHost.newTabSpec("tab_3")
.setContent(R.id.tab3)
.setIndicator("TAB3", null));
tabHost.setCurrentTab(0);
/**
* 选项卡切换事件
*/
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
}
});
}
}