选项​​卡布局和Android的按钮布局、选项、按钮、Android

2023-09-07 12:21:41 作者:贱人矫情必自毙。

我已经建立了使用Tab键布局的Andr​​oid应用程序,它有2个标签。我希望在每个选项卡不同的按钮。但是,如果我在main.xml中定义按钮,我得到两个标签相同的按钮。

I have built an application in android using Tab Layout and it has 2 tabs. I want a different button on each of these tabs. But if I define button in main.xml, I get same button on both the tabs.

我甚至尝试分别定义在类中每个选项卡的按钮,但得到一些奇怪的错误。有人可以帮我了这一点。下面是我的code:

I even tried defining the buttons separately in the class of each tab, but was getting some weird errors. can somebody please help me out with this. Below is my code:

FinalProj.java:的

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 的

For 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 的

Tab 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中:的

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>

我想有一个按钮,在TextView的和iFallApp.java一个按钮,并在EDITTEXT沿Settings.java

I want to have one button along with TextView in iFallApp.java and one button and editText in Settings.java.

推荐答案

有关大家有没有发现这样一个问题:

For anyone else finding this question:

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什么。在这种情况下,视图被设置为从R.layout.main其对应于/layout/main.xml加载(R是由机器人框架生成所生成的查找类)。

The setContentView(R.layout.main) tells the activity what to display on its UI. In this case the view is being set to load from R.layout.main which corresponds to /layout/main.xml (R being the generated lookup class generated by the android framework).

更改main.xml中会改变两个标签,因为这两个选项卡都包含/中/在 TabHost 里面定义的视图的main.xml 这里:

Changing the main.xml would change both tabs because both tabs are contained /within/ the FinalProj activity inside the TabHost view defined inside of main.xml here:

<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">

整个标签系统在这里全面详细介绍:标签布局| Android开发者