下面是我的布局 xml 文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/l1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="right"
android:orientation="vertical" >
<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" android:background="@drawable/ic_launcher">
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" android:background="#FF888888"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_above="@android:id/tabs" />
</RelativeLayout>
</TabHost>
</LinearLayout>
<LinearLayout
android:id="@+id/l2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="#000000"
android:orientation="vertical" >
<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" android:background="@drawable/ic_launcher">
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" android:background="#FF888888"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_above="@android:id/tabs" />
</RelativeLayout>
</TabHost>
</LinearLayout>
</LinearLayout>
布局 l1 和 l2 中有 2 个 tabhost。
下面的代码是我使用tabhost的代码:
TabWidget tw;
TabHost tabHost;
tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
Resources res = getResources();
tw = (TabWidget)findViewById(android.R.id.tabs);
intent = new Intent().setClass(this, MyGroup.class);
spec = tabHost.newTabSpec("A").setIndicator("A", res.getDrawable(R.drawable.ic_action_search)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MyGroup.class);
spec = tabHost.newTabSpec("B").setIndicator("B", res.getDrawable(R.drawable.ic_action_search)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MyGroup.class);
spec = tabHost.newTabSpec("C").setIndicator("C", res.getDrawable(R.drawable.ic_action_search)).setContent(intent);
tabHost.addTab(spec);
但它只能控制l1中的tabhost。
如何控制 l2 中的 tabhost?
我做了一些修改如下:
private TabWidget tw;
private TabHost tabHost;
private TabWidget tw1;
private TabHost tabHost1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
Resources res = getResources();
tw = (TabWidget)findViewById(android.R.id.tabs);
intent = new Intent().setClass(this, MyGroup.class);
spec = tabHost.newTabSpec("A").setIndicator("A", res.getDrawable(R.drawable.ic_action_search)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MyGroup.class);
spec = tabHost.newTabSpec("B").setIndicator("B", res.getDrawable(R.drawable.ic_action_search)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MyGroup.class);
spec = tabHost.newTabSpec("C").setIndicator("C", res.getDrawable(R.drawable.ic_action_search)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MyGroup.class);
spec = tabHost.newTabSpec("A").setIndicator("A", res.getDrawable(R.drawable.ic_action_search)).setContent(intent);
tabHost.addTab(spec);
tabHost1 = (TabHost)findViewById(R.id.tabhost1);
TabHost.TabSpec spec1;
Intent intent1;
tw1 = (TabWidget)findViewById(android.R.id.tabs);
intent1 = new Intent().setClass(this, MyGroup.class);
spec1 = tabHost1.newTabSpec("D").setIndicator("D", res.getDrawable(R.drawable.ic_action_search)).setContent(intent1);
tabHost1.addTab(spec1);
intent1 = new Intent().setClass(this, MyGroup.class);
spec1 = tabHost1.newTabSpec("E").setIndicator("E", res.getDrawable(R.drawable.ic_action_search)).setContent(intent1);
tabHost1.addTab(spec1);
intent1 = new Intent().setClass(this, MyGroup.class);
spec1 = tabHost1.newTabSpec("F").setIndicator("F", res.getDrawable(R.drawable.ic_action_search)).setContent(intent1);
tabHost1.addTab(spec1);
intent1 = new Intent().setClass(this, MyGroup.class);
spec1 = tabHost1.newTabSpec("G").setIndicator("G", res.getDrawable(R.drawable.ic_action_search)).setContent(intent1);
tabHost1.addTab(spec1);
}
错误信息如下:
08-14 16:26:26.960: D/AndroidRuntime(9763): Shutting down VM
08-14 16:26:26.960: W/dalvikvm(9763): threadid=1: thread exiting with uncaught exception (group=0x40a291f8)
08-14 16:26:26.960: E/AndroidRuntime(9763): FATAL EXCEPTION: main
08-14 16:26:26.960: E/AndroidRuntime(9763): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testtwolayout/com.example.testtwolayout.MainActivity}: java.lang.NullPointerException
08-14 16:26:26.960: E/AndroidRuntime(9763): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-14 16:26:26.960: E/AndroidRuntime(9763): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-14 16:26:26.960: E/AndroidRuntime(9763): at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-14 16:26:26.960: E/AndroidRuntime(9763): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-14 16:26:26.960: E/AndroidRuntime(9763): at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 16:26:26.960: E/AndroidRuntime(9763): at android.os.Looper.loop(Looper.java:137)
08-14 16:26:26.960: E/AndroidRuntime(9763): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-14 16:26:26.960: E/AndroidRuntime(9763): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 16:26:26.960: E/AndroidRuntime(9763): at java.lang.reflect.Method.invoke(Method.java:511)
08-14 16:26:26.960: E/AndroidRuntime(9763): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-14 16:26:26.960: E/AndroidRuntime(9763): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-14 16:26:26.960: E/AndroidRuntime(9763): at dalvik.system.NativeStart.main(Native Method)
08-14 16:26:26.960: E/AndroidRuntime(9763): Caused by: java.lang.NullPointerException
08-14 16:26:26.960: E/AndroidRuntime(9763): at android.widget.TabHost.addTab(TabHost.java:232)
08-14 16:26:26.960: E/AndroidRuntime(9763): at com.example.testtwolayout.MainActivity.onCreate(MainActivity.java:68)
08-14 16:26:26.960: E/AndroidRuntime(9763): at android.app.Activity.performCreate(Activity.java:4465)
08-14 16:26:26.960: E/AndroidRuntime(9763): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-14 16:26:26.960: E/AndroidRuntime(9763): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-14 16:26:26.960: E/AndroidRuntime(9763): ... 11 more
MainActivity.java:68 行表示 tabHost1.addTab(spec1);
我已经通过这个Android 解决了: TabHost without TabActivity。
和简单的测试代码如下:
private LocalActivityManager mlam;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mlam = new LocalActivityManager(this, false);
mlam.dispatchCreate(savedInstanceState);
tw = (TabWidget)findViewById(android.R.id.tabs);
Resources res = getResources();
tabHost =(TabHost)findViewById(android.R.id.tabhost);
tabHost.setup(mlam);
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, MyGroup.class);
spec = tabHost.newTabSpec("A").setIndicator("A", res.getDrawable(R.drawable.ic_action_search)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MyGroup.class);
spec = tabHost.newTabSpec("B").setIndicator("B", res.getDrawable(R.drawable.ic_action_search)).setContent(intent);
tabHost.addTab(spec);
tabHost1 = (TabHost)findViewById(R.id.tabhost1);
tabHost1.setup(mlam);
TabHost.TabSpec spec1;
Intent intent1;
intent1 = new Intent().setClass(this, MyGroup.class);
spec1 = tabHost1.newTabSpec("C").setIndicator("C", res.getDrawable(R.drawable.ic_action_search)).setContent(intent1);
tabHost1.addTab(spec1);
intent1 = new Intent().setClass(this, MyGroup.class);
spec1 = tabHost1.newTabSpec("D").setIndicator("D", res.getDrawable(R.drawable.ic_action_search)).setContent(intent1);
tabHost1.addTab(spec1);
tabHost1.setCurrentTab(0);
}
MyGroup 如下:
public class MyGroup extends ActivityGroup {
public static ActivityGroup group;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
group = this;
}
@Override
public void onBackPressed() {
group.getLocalActivityManager().getCurrentActivity().onBackPressed();
}
@Override
protected void onResume() {
super.onResume();
Intent it;
Window w;
View view;
it = new Intent(this, AActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
w = group.getLocalActivityManager().startActivity("AActivity", it);
view = w.getDecorView();
group.setContentView(view);
}
}
下面的 AActivity 只显示一个随机数:
public class AActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.a);
TextView tv = (TextView)findViewById(R.id.atv);
int x = (int)(Math.random()*42+1);
String xx = Integer.toString(x);
Log.d("q", xx);
tv.setText(xx);
}
}
但我发现左侧布局的选项卡 0 不会显示数字。
但是行 Log.d("q", xx); 是秀。
这个怎么样?