在文档中,据说TabActivity不推荐使用:在此输入链接描述.我跟着他们的例子使用了FragmentTabHost它,但它也崩溃了.所以我尝试创建一个简单的activity并添加一个intent作为内容TabSpec,但应用程序崩溃了!那么如何创建一个TabHost包含ListView?
这是我做的:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.onglet_parcelle_batiment);
mTabHost = (TabHost)findViewById(R.id.onglets);
mTabHost.setup();
Bundle dataSent = getIntent().getExtras();
bien_code = dataSent.getString("bien_code");
afficherOngletParcelle();
afficherOngletBatiments();
mTabHost.setCurrentTab(0);
}
private void afficherOngletParcelle() {
TabHost.TabSpec parcelle = mTabHost.newTabSpec("parcelle");
View onglet = getLayoutInflater().inflate(R.layout.template_tab_onglets, null);
TextView label = (TextView) onglet.findViewById(R.id.tabLabel);
label.setText(getResources().getString(R.string.parcelle));
parcelle.setIndicator(onglet);
Intent i = new Intent(OngletParcelleListeBatiments.this, ParcelleActivity.class);
i = i.putExtra("bien_code", bien_code);
parcelle.setContent(i);
mTabHost.addTab(parcelle);
}
private void afficherOngletBatiments() {
TabHost.TabSpec batiment = mTabHost.newTabSpec("batiments");
View onglet = getLayoutInflater().inflate(R.layout.template_tab_onglets, null);
TextView label = (TextView) onglet.findViewById(R.id.tabLabel);
label.setText(getResources().getString(R.string.titre_liste_batiments));
batiment.setIndicator(onglet);
Intent i = new Intent(OngletParcelleListeBatiments.this, ListeBatimentsActivity.class);
i = i.putExtra("bien_code", bien_code);
batiment.setContent(i);
mTabHost.addTab(batiment);
}
Run Code Online (Sandbox Code Playgroud)
我很高兴您提出这个问题,因为我已经挣扎了超过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>
Run Code Online (Sandbox Code Playgroud)
2)为包含组件中引用的每个选项卡创建布局.例如"layout_1.xml".
您可以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="@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>
Run Code Online (Sandbox Code Playgroud)
3)创建具有所需规范的自定义指标,例如:
<?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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
5)在Activity或Fragment中,初始化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;
}
Run Code Online (Sandbox Code Playgroud)
我希望这能帮到您.随意问任何问题 :)
| 归档时间: |
|
| 查看次数: |
2411 次 |
| 最近记录: |