gpt4 book ai didi

android - 如何在Android上使用两个tabHost

转载 作者:搜寻专家 更新时间:2023-11-01 09:05:45 25 4
gpt4 key购买 nike

下面是我的布局 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);是节目。
这个怎么样?

最佳答案

您无法通过使用相同的 id 获得两个不同的 tabhosts。因此,您必须更改第二个 tabhost 的 android:id

例如:android:id="@+id/mySecondTabhost"

稍后您需要使用适当的 R引用您的包(不是 android.R)的类,以便检索您自定义的 id 值。

关于android - 如何在Android上使用两个tabHost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11947689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com