cho*_*bo2 13 android android-widget
我正在按照本教程https://developer.android.com/guide/tutorials/views/hello-tabwidget.html完成它.现在我实际上想要向这些标签添加一些控件,如文本框(文本编辑).
我该怎么做呢?我使用eclipse作为我的ide转到我的mail.xml,然后转到布局视图,我现在得到一个NullPointerException,所以我甚至无法将内容拖到布局上.
编辑
这就是我所拥有的
<?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">
<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">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a tab" />
<EditText android:text="" android:id="@+id/EditText01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:password="true"></EditText>
</LinearLayout>
<TextView
android:id="@+id/textview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is another tab" />
<TextView
android:id="@+id/textview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a third tab" />
</FrameLayout>
</LinearLayout>
</TabHost>
Run Code Online (Sandbox Code Playgroud)
Ste*_*ley 35
选项卡最初工作有点搞笑,因为有很多代码开销,但是一旦你完成了工作,它们就不会太糟糕了.为了让标签工作,让我们从改进您的XML文件开始,然后我们可以确保您的代码实际加载它们是正确的.
首先,您的XML文件.您应该使用该include功能,而不是直接在main.xml中包含所有内容.顾名思义,这可以让你处理一个单独的xml文件,然后用一行将它包含在你的主文件中.这使得main.xml文件更容易阅读.所以我们修改上面的文件使它看起来像这样:
//No need to change anything above this
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include layout="@layout/tab1"/>
<include layout="@layout/tab2"/>
//and however many other tabs you want to include
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
然后,您需要创建tab1.xml,tab2.xml等.这些是普通的xml文件,因为它们以ViewGroup(即LinearLayout,RelativeLayout)开头,其中包含任意数量的其他小部件.这些小部件可以是EditTexts,按钮,自定义视图,无论你想要什么.唯一的规则是父ViewGroup(顶部的那个)必须有一个唯一的ID,方式是android:id="@+id/someUniqueName".您将使用它来引用代码中的特定布局/选项卡.例如,这将是:
tab1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tab1Layout"
android:orientation="vertical">
<TextView ... />
<EditText ... />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
完成后,我们可以查看您的代码.我假设你可能已经有了这个,但以防万一这是你想要的:
public class YourProject extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1 title",
res.getDrawable(R.drawable.logo1)).setContent(R.id.tab1Layout));
(...)
//You can also fill tabs with a separate activity like so:
Intent intent = new Intent(this, YourClass.class);
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Another title",
res.getDrawable(R.drawable.logo2)).setContent(intent));
tabHost.setCurrentTab(0);
}
}
Run Code Online (Sandbox Code Playgroud)
如上所示,您可以将其中一个选项卡的内容设置为单独的活动.在这种情况下,活动被定义为具有其自己的类,布局等的任何其他活动.通常您不应该这样做而只是使用不同的视图(setContent(R.id.tabXLayout)有时,但有时需要它.例如,如果你想要一个如果您的选项卡有一个列表,那么您需要在其中启动一个扩展ListView的活动,并包含ListViews的所有样板代码.
我希望有所帮助!
| 归档时间: |
|
| 查看次数: |
26076 次 |
| 最近记录: |