Android 在视图中使用选项卡
我在使用视图选项卡时有点问题。 首先,我刚刚复制了选项卡与activitys一起使用的示例代码: 我的布局文件如下所示:Android 在视图中使用选项卡,android,Android,我在使用视图选项卡时有点问题。 首先,我刚刚复制了选项卡与activitys一起使用的示例代码: 我的布局文件如下所示: <?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_paren
<?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">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
这一点与预期相符。
但是当我取消对TextView行的注释并调用setContent(Test.getId())而不是setContent(intent)时,应用程序崩溃了。
我还尝试在layoutfile中创建一个textview,并调用setContent(R.id.test),
这也让它崩溃了
所以这是一个问题
第二点是。我不想使用activitys,因为我希望能够调用这些类上的方法,这些类将表示选项卡内容。
所以我最初的想法是,从视图中派生一些类。每个选项卡1个,并传递其ID。但是,上面的代码示例需要首先工作
Uzaku你好我知道你说过你在布局文件中尝试了
文本视图
,但这应该可以
更改框架布局
部分,如下所示
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<TextView
android:id="@+id/test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="TEST" />
</FrameLayout>
你不能使用
setContent(Test.getId())
,因为getId()
会失败,因为你没有给Test
一个id。试试Test.setId(1234)
然后使用setContent(1234)
谢谢你的评论,但是问题仍然存在于显示崩溃/堆栈跟踪的日志上。日志很长,所以我上传了它,这里有一个链接:这是有效的,我想我一定是犯了一个错误,当我尝试这样做。在源代码中创建TextView时,您有没有解释为什么它不起作用?很高兴它对您起作用。不,恐怕我想不出它为什么不能用代码创建TextView
。我相信这是有充分理由的。也许您需要在代码中获取FrameLayout
视图,然后在调用setContent(…)
之前将TextView
添加到其中。如果我在1个选项卡中显示1个文本视图,则没有问题;如果我尝试在两个选项卡中显示相同的文本视图,则两个选项卡中的文本视图彼此重叠。为什么呢?我觉得我错过了一个重要的教程。看看这个例子——它应该会有所帮助。
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<TextView
android:id="@+id/test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="TEST" />
</FrameLayout>
tH.addTab(tH.newTabSpec("t1").setIndicator("Tab1").setContent(R.id.test));