我很高興你問這個問題,因爲我一直在努力超過1天試圖達到你想達到不使用TabActivity
什麼。但我終於做到了。我爲自己製作了一個教程,我將來會在我的博客中發佈它。
下面是它的詳細信息: -
1)在你的XML文件,添加了3個標籤下面。
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="@android:color/white">
<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="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- tab N°1 --> <!-- Inclusion of the interface -->
<include
android:id="@+id/i_layout_1"
layout="@layout/layout_1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- tab N°2 --> <!-- Inclusion of the interface -->
<include
android:id="@+id/i_layout_2"
layout="@layout/layout_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!-- tab N°3 --> <!-- Inclusion of the interface -->
<include
android:id="@+id/i_layout_3"
layout="@layout/layout_3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
</LinearLayout>
</TabHost>
2)創建爲每個標籤的佈局你在包括部件的參考。例如「layout_1.xml」。
你可以人在這裏在此佈局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:padding="10dp">
<TextView
android:id="@+id/my_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Example"
android:textColor="@android:color/black" />
</RelativeLayout>
3)添加ListView
或任何使用必需的規範,例如自定義指標:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/business_tabs_indicator_background"
android:padding="10dp">
<TextView
android:id="@+id/tv_indicator_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:textColor="@android:color/black"
android:textStyle="bold" />
</RelativeLayout>
4)創建自定義選項卡,用於選項卡指示符佈局以處理不同的選擇狀態,重點,..等。例如
business_tabs_indicator_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active tab -->
<item android:drawable="@android:color/darker_gray" android:state_focused="false" android:state_pressed="false" android:state_selected="true" />
<!-- Inactive tab -->
<item android:drawable="@android:color/white" android:state_focused="false" android:state_pressed="false" android:state_selected="false" />
<!-- Pressed tab -->
<item android:drawable="@android:color/darker_gray" android:state_pressed="true" />
<!-- Selected tab (using d-pad) -->
<item android:drawable="@android:color/darker_gray" android:state_focused="true" android:state_pressed="false" android:state_selected="true" />
</selector>
5)在您的活動或片段,初始化TabHost並添加相應的標籤,如下:
private void initializeTabHoster(View rootview) {
tabHost = (TabHost) rootview.findViewById(R.id.tabhost);
//Important
tabHost.setup();
TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab Tag");
TabHost.TabSpec tab2 = tabHost.newTabSpec("Second Tab Tag");
TabHost.TabSpec tab3 = tabHost.newTabSpec("Third tab Tag");
// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
tab1.setIndicator(createTabIndicator("Tab1"));
tab1.setContent(R.id.i_layout_1);
tab2.setIndicator(createTabIndicator("Tab2"));
tab2.setContent(R.id.i_layout_2);
tab3.setIndicator(createTabIndicator("Tab3"));
tab3.setContent(R.id.i_layout_3);
/** Add the tabs to the TabHost to display. */
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
}
private View createTabIndicator(String text) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.tab_indicator, null);
TextView textView = (TextView) view.findViewById(R.id.tv_indicator_label);
textView.setText(text);
return view;
}
我希望這將幫你。隨意問任何問題:)
你可以發佈你迄今爲止所做的? –