Android 如何使用选项卡视图的选项卡布局项
我有一个选项卡布局文件,其中包括两个选项卡。布局如下:Android 如何使用选项卡视图的选项卡布局项,android,Android,我有一个选项卡布局文件,其中包括两个选项卡。布局如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TabHost android:id="@+id/myTabHost" a
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TabHost
android:id="@+id/myTabHost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
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" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include
android:id="@+id/first"
android:layout_width="fill_parent"
android:layout_height="match_parent"
layout="@layout/tab_layout1" />
<include
android:id="@+id/second"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/tab_layout2" />
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
虽然我可以访问选项卡小部件布局项,但当我想为每个选项卡布局设置背景时,我不能。知道为什么会发生这种情况吗?您正在启动第二个活动,并且该活动再次膨胀second.xml,它将无法获取您在主活动中创建的图像集。您需要在第二个活动中再次设置图像。首先,您对“不工作”的意思是什么?第二,为什么不将
path
发送到SecondActivity
并在该活动中执行此操作?它不会设置图像,当我运行它时,页面是空白的。而且它只是一个示例项目。原因可以追溯到我的主应用程序,其中我有选项卡式视图,并包含其他布局作为选项卡小部件,在那里我想访问小部件项目。那么问题是什么?设置图像时,图像是否未显示?你得到了NPE或者什么?我没有任何例外。它只是不显示图像。正如我向Shayan解释的,我不想在第二次活动中设置图像。以此为例,查看是否有显示图像的方法。在创建第二个活动之前,不能设置图像。这就是它的工作方式。除非你们创建了一个基本的活动类,把你们所有的东西都膨胀起来,然后把它衍生到第二类。虽然在我看来这是个错误的设计。你现在要做的就是在你购买之前先吃点东西。请考虑一下:我有一个有标签视图和几个标签小部件的活动的布局文件。我已经包含了几个布局文件作为每个选项卡小部件的布局,在我的活动中,我将主布局文件设置为主要内容。现在我想以选项卡小部件的形式访问其他布局文件中的项目。我该怎么做?你有更好的主意吗?
TabHost host = (TabHost)findViewById(R.id.myTabHost);
host.setup();
TabSpec firstSpec = themesHost.newTabSpec("tab_layout1");
firstSpec.setIndicator("layout1", getResources().getDrawable(android.R.drawable.ic_menu_view));
firstSpec.setContent(R.id.first);
host.addTab(firstSpec);
TabSpec secondSpec = themesHost.newTabSpec("tab_layout2");
secondSpec.setIndicator("layout2", getResources().getDrawable(android.R.drawable.ic_menu_view));
secondSpec.setContent(R.id.second);
host.addTab(secondSpec);