Android在tabcontent中填充framelayout

时间:2014-05-10 06:05:18

标签: android eclipse android-layout android-tabhost tabwidget

我需要你的帮助来解决简单的问题。 如何动态“连接”或将布局扩展为标签内容。 我想用TabsHost项目。对于每个标签,我喜欢不同的布局。非常简单:) 我正在创建默认的android项目:

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

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.tabtest.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

&LT; /清单&GT;

下一步我使用eclipse设计器并将TabHost拖放到activity_main。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

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

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

</RelativeLayout>

下一步我为tab1添加一个名为categories

的新布局文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</FrameLayout>

和类别java类

我在互联网上使用的所有示例都不使用eclipse设计器进行TabHost定义,为什么?

如何在代码中连接两个布局?

感谢。

1 个答案:

答案 0 :(得分:0)

// try this way,hope this will help you...

tab1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:id="@+id/txtTab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tab1"
        android:textSize="25sp"/>

</LinearLayout>

tab2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

   <TextView
       android:id="@+id/txtTab2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Tab2"
       android:textSize="25sp"/>

</LinearLayout>

tab3.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:id="@+id/txtTab3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tab3"
        android:textSize="25sp"/>

</LinearLayout>

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TabHost
        android:id="@+id/mytabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

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

                <include
                    android:id="@+id/tab1"
                    layout="@layout/tab1" >
                </include>

                <include
                    android:id="@+id/tab2"
                    layout="@layout/tab2" >
                </include>
                <include
                    android:id="@+id/tab3"
                    layout="@layout/tab3" >
                </include>

            </FrameLayout>
        </LinearLayout>
    </TabHost>

</RelativeLayout>

MainActivity.java
public class MainActivity extends Activity {

    private TabHost mytabhost;
    private TextView txtTab1;
    private TextView txtTab2;
    private TextView txtTab3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mytabhost =(TabHost) findViewById(R.id.mytabhost);
        txtTab1 =(TextView) findViewById(R.id.txtTab1);
        txtTab2 =(TextView) findViewById(R.id.txtTab2);
        txtTab3 =(TextView) findViewById(R.id.txtTab3);
        mytabhost.setup();
        mytabhost.addTab(mytabhost.newTabSpec("Tab1").setIndicator("MyTab1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab1));
        mytabhost.addTab(mytabhost.newTabSpec("Tab2").setIndicator("MyTab2",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab2));
        mytabhost.addTab(mytabhost.newTabSpec("Tab3").setIndicator("MyTab3",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab3));

        txtTab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"My Tab 1",Toast.LENGTH_SHORT).show();
            }
        });
        txtTab2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"My Tab 2",Toast.LENGTH_SHORT).show();
            }
        });
        txtTab3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"My Tab 3",Toast.LENGTH_SHORT).show();
            }
        });
    }

}