【问题标题】:Tab Layout and buttons in AndroidAndroid中的选项卡布局和按钮
【发布时间】:2011-07-13 01:15:19
【问题描述】:

我已经使用选项卡布局在 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。

【问题讨论】:

  • 我解决了这个问题...请不要打扰...谢谢:)

标签: java android button


【解决方案1】:

对于其他发现此问题的人:

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) 告诉活动在其 UI 上显示什么。在这种情况下,视图被设置为从对应于 /layout/main.xml 的 R.layout.main 加载(R 是由 android 框架生成的生成查找类)。

更改 main.xml 将更改两个选项卡,因为这两个选项卡都包含在 /within/ TabHost 视图内的 FinalProj 活动中,这里定义在 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

【讨论】:

  • 谢谢,这个帮助了我。改变主要是我需要做的!
猜你喜欢
  • 1970-01-01
  • 2012-09-29
  • 1970-01-01
  • 2017-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多