我使用选项卡布局在 android 中构建了一个应用程序,它有 2 个选项卡。我想在每个选项卡上都有一个不同的按钮。但如果我在 main.xml 中定义按钮,我会在两个选项卡上得到相同的按钮。
我什至尝试在每个选项卡的类中单独定义按钮,但遇到了一些奇怪的错误。有人可以帮我解决这个问题吗?下面是我的代码:
FinalProj.java:
package FinalProj.com;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class FinalProj extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
intent = new Intent().setClass(this, iFallApp.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.icon))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Settings.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.icon))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
}
对于 Tab1 - iFallApp.java
package FinalProj.com;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class iFallApp extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the iFall tab");
setContentView(textview);
}
}
选项卡 2 - Settings.java
package FinalProj.com;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Settings extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the Settings tab");
setContentView(textview);
}
}
Main.xml:
<?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>
我想在 iFallApp.java 中拥有一个按钮和 TextView,在 Settings.java 中拥有一个按钮和 editText。
最佳答案
对于发现此问题的其他人:
public class FinalProj extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setContentView(R.layout.main)
告诉 Activity 在其 UI 上显示什么。在本例中, View 被设置为从对应于/layout/main.xml 的 R.layout.main 加载(R 是由 android 框架生成的生成的查找类)。
更改 main.xml 会更改两个选项卡,因为这两个选项卡都包含在 FinalProj
内。 TabHost
内的 Activity View 定义在 main.xml
内部这里:
<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">
这里详细描述了整个选项卡系统: Tab Layout | Android Developers
关于java - Android 中的选项卡布局和按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5918764/