مرحبًا ، TabWidget كل علامة تبويب ، يرجى الرجوع إلى XML جديد

StackOverflow https://stackoverflow.com/questions/4611009

سؤال

أنا أستخدم Exmaple من Google مرحبا ، tabwidget لكن غيره لتبدو هكذا:

main.xml:

<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">
            <TextView 
                android:text="@+layout/text"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
            <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>

ملف جافا:

public class HelloTabWidget extends TabActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost mTabHost = getTabHost();

        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(R.layout.text));
        mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.textview2));
        mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(R.id.textview3));

        mTabHost.setCurrentTab(0);
    }
}

وهنا text.xml في الدقة/التصميم:

<LinearLayout
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <TextView
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="This is Tab 1" />
</LinearLayout>

ما أحاول القيام به بشكل أساسي هو جعل كل علامة تبويب تشير إلى ملف XML الخاص به بدلاً من كل شيء في Main.xml ، لكن النص في علامة التبويب الأولى لا يظهر.

هل كانت مفيدة؟

المحلول

هل تحاول أن تشير كل علامة تبويب إلى خاصة بها Activity؟ إذا كان الأمر كذلك ، يمكنك إعداد نية كمحتوى لكل علامة تبويب:

intent = new Intent().setClass(this, Test.class);       
spec = tabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(intent);
        tabHost.addTab(spec);

بالطبع ، يجب عليك بعد ذلك الحصول على اختبار مسمى (أو أي شيء آخر) يحدد test.xml كما التصميم.

public class Test extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
    }
}

هذا البرنامج التعليمي سوف يساعدك أكثر إذا كان هذا هو نوع السيناريو الذي تحاول إعداده.

نصائح أخرى

هناك ثلاثة خيارات لعلامات التبويب باستخدام مضيف علامة التبويب المدمجة.

  1. طرق عرض متعددة على نشاط واحد مع ملف تخطيط واحد
  2. أنشطة متعددة
  3. طرق عرض مخصصة مدمجة في tabContentFactory

يبدو أنك تريد #3. ما عليك فعله هو شيء من هذا القبيل:

setContent(new TabHost.TabContentFactory() {
    public View createTabContent(String tag)
    {
        View view = LayoutInflater.from(HelloTabWidget.this).inflate(R.layout.text);
        // Setup the view here
        return view;
    }});
View view = LayoutInflater.from(HelloTabWidget.this).inflate(R.layout.text);

هذا ليس صحيحًا تمامًا حيث يأخذ layoutinflater.inflate معلمتين.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top