0

レポートとプロファイルの 2 つのタブがあります。

各タブに別のフォーム (Textview、スピナーなど) を追加したいと考えています。今のところボタンを試していますが、別のタブでは別のボタンにする必要があります。

しかし、私が得るのは、両方のタブに同じボタンがあるということです。ボタン名 ButtonReport を [レポート] タブに表示し、ButtonProfile を [プロファイル] タブに表示する必要があります。

以下はその写真です。

ここに画像の説明を入力

ボタンのコードを 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:orientation="vertical"
    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">
        </TabWidget>

        <FrameLayout android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">



        <!-- Start Interface -->
        <TableLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/tableLayout1" android:alwaysDrawnWithCache="false">  
        <TableRow android:layout_height="fill_parent" android:id="@+id/tableRow3">
            <TableLayout android:id="@+id/tableLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content">

                <Button android:text="Button Report" android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>

            </TableLayout>
            </TableRow>     
        </TableLayout>
        <!-- End Interface -->


        </FrameLayout>



    </LinearLayout>

</TabHost>

これは私のReportActivityクラスとProfileActivityです(同じコードで名前が異なるだけです)

package com.tab;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ReportActivity extends Activity{
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        TextView text = new TextView(this);
        text.setText("Artist");
        setContentView(text);
    }
}

これは私の MainActivity クラスです

package com.tab;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;

public class MainActivity extends TabActivity {
    private TabHost aTab;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Resources res = getResources();
        aTab = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;

        //Report Tab
        intent = new Intent(this, ReportActivity.class);

        spec = aTab.newTabSpec("Report")
            .setIndicator("Report", res.getDrawable(R.drawable.tab_icon))
            .setContent(intent);
        aTab.addTab(spec);

        //Profile Tab
        intent = new Intent(this, ProfileActivity.class);

        spec = aTab.newTabSpec("Profile")
            .setIndicator("Profile", res.getDrawable(R.drawable.tab_icon))
            .setContent(intent);
        aTab.addTab(spec);

        aTab.setCurrentTab(1);
    }
}

別のタブで別のフォームを持つ方法はありますか?

4

4 に答える 4

1

最良の方法(私の意見)はフラグメントを使用することです:http: //developer.android.com/guide/components/fragments.html

フラグメントはAPIレベル11以上用ですが、互換性パッケージを使用できます:http: //android-developers.blogspot.ch/2011/03/fragments-for-all.html

したがって、すべてのタブがフラグメントになります。

于 2012-07-31T05:32:04.137 に答える
1

TabHost のチュートリアルを見てください。そして、TabHostFor Example で異なるコンテンツをどのように使用しているかを見てください -

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

レポート.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <!-- Screen Design for Photos -->
  <Button android:text="Button Report"
            android:textSize="18dip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</LinearLayout>

プロフィール.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <!-- Screen Design for Photos -->
  <Button android:text="Button Profile"
            android:textSize="18dip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</LinearLayout>

あなたのmain.xmlクラスには、 Start Interfaceという別のものが含まれています。これは、(Button)すべてのタブで同じコンテンツを表示するという問題です。これを削除する必要があります。

そして、あなたProfileReportアクティビティでは、これをxml使用してこのファイルを表示するだけsetContentViewで、必要な別のボタンが提供されます。あなたはMainActivityこれに使用できます、それは完璧に思えます。

これがお役に立てば幸いです。

于 2012-07-31T05:40:39.497 に答える
1

アクティビティ (レポートとプロファイル) ごとに異なるレイアウトxmlを設定できます。 ソース: http://www.androidhive.info/2011/08/android-tab-layout-tutorial/

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class TabHostActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabHost = getTabHost();

        TabSpec report = tabHost.newTabSpec("Report");
        report.setIndicator("Report", getResources().getDrawable(R.drawable.icon_report));
        Intent reportIntent = new Intent(this, report.class);
        report.setContent(reportIntent);

TabSpec profile = tabHost.newTabSpec("Profile");
        profile.setIndicator("Profile", getResources().getDrawable(R.drawable.icon_profile));
        Intent profileIntent = new Intent(this, profile.class);
        profile.setContent(profileIntent);


        // Adding all TabSpec to TabHost
        tabHost.addTab(report); // Adding photos tab
        tabHost.addTab(profile); // Adding songs tab
    }
}

次に、res/layouts の下に 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">
        <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>
</TabHost>

次に、レポートとプロファイルのアクションを実行する 2 つの異なるアクティビティ:

//report.java
package com.example;

import android.app.Activity;
import android.os.Bundle;

public class ReportActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.report_layout);
    }
}

//profile.java
package com.example;

import android.app.Activity;
import android.os.Bundle;

public class ProfileActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile_layout);
    }
}

これで、2 つの XML を作成できます。1 つはプロファイル レイアウト用、もう 1 つはレポート レイアウト用です。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <TextView android:text="Profile"
            android:padding="15dip"
            android:textSize="18dip"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <TextView android:text="Report"
            android:padding="15dip"
            android:textSize="18dip"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

</LinearLayout>

それが完了したら、各 xml を変更して、好きなボタンやスピナーなどを持つことができます。

最後に、AndroidManifest を変更してアクティビティを許可し、インテント フィルターを介して tabhostactivity に自動起動するようにしてください。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".TabHostActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".profile" />

        <activity android:name=".report" />


    </application>
于 2012-07-31T05:42:02.387 に答える
0

このコードが役立つと思います。さまざまなインテントを使用して、さまざまなタブ アクティビティを呼び出すことができます。また、さまざまなコンテンツ ビューをさまざまなアクティビティに設定できます。

public class MainActivity extends TabActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    setContentView(R.layout.activity_main);

        Resources ressources = getResources(); 
        TabHost tabHost = getTabHost();

        //Setting Android TAB
        Intent intentAndroid = new Intent().setClass(this, AndroidTab.class);
        TabSpec tabSpecAndroid = tabHost
          .newTabSpec("Android")
          .setIndicator("", ressources.getDrawable(R.drawable.android))
          .setContent(intentAndroid);

        //Setting Apple TAB
        Intent intentApple = new Intent().setClass(this, AppleActivity.class);
        TabSpec tabSpecApple = tabHost
          .newTabSpec("Apple")
          .setIndicator("", ressources.getDrawable(R.drawable.aapl))
          .setContent(intentApple);



        //Add all tabs
        tabHost.addTab(tabSpecAndroid);
        tabHost.addTab(tabSpecApple);


        tabHost.setCurrentTab(0);
}
}

異なるクラスを作成し、android と apple の 2 つのアクティビティを作成する必要があります。その特定のアクティビティでは、異なる contentviews を設定でき、必要なものが得られると思います。これは単なるサンプルであり、同じように必要なものを作成できます。 .

于 2012-07-31T05:36:57.987 に答える