本文主要是介绍Android TabHost与FragmentActivity,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
今天看了一篇介绍FragmentActivity的文章,模仿做了一下.。
MainActivity:
package com.tao.test;import android.os.Bundle;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;public class MainActivity extends Activity {private TabHost tabHost;LocalActivityManager activityManager;// 在Activity中使用Tabhost必須要有LocalActivityManager物件來設定@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tabHost = (TabHost) findViewById(android.R.id.tabhost);activityManager = new LocalActivityManager(MainActivity.this, false);activityManager.dispatchCreate(savedInstanceState);tabHost.setup(activityManager);tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1").setContent(new Intent(MainActivity.this, FragmentActivity1.class)));tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2").setContent(new Intent(MainActivity.this, FragmentActivity2.class)));}@Overrideprotected void onPause() {activityManager.dispatchPause(isFinishing());super.onPause();}@Overrideprotected void onResume() {activityManager.dispatchResume();super.onResume();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}
mainactivity的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"android:id="@android:id/tabhost"android:layout_width="fill_parent"android:layout_height="fill_parent" ><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><!-- 实现Tab标签的居底主要是通过设置属性 android:layout_weight="1" --><!-- 还要注意FrameLayout标签的位置,要写在TabWidget标签的前面 --><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1" /><TabWidgetandroid:id="@android:id/tabs"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true" /></LinearLayout></TabHost>
FragmentActivity代码:
package com.tao.test;import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;public class FragmentActivity2 extends FragmentActivity {FragmentManager fragmentManager;@Overrideprotected void onCreate(Bundle arg0) {super.onCreate(arg0);setContentView(R.layout.fragment_activity_2);Button button = (Button) findViewById(R.id.change);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {FragmentTransaction ft = getSupportFragmentManager().beginTransaction();ft.replace(R.id.secound_fragment, new Fragment1());ft.commit();}});}}
最后要在AndroidManifest.xml中加上 android:configChanges="orientation"
这篇关于Android TabHost与FragmentActivity的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!