【问题标题】:Android inflate framelayout inside tabcontentAndroid在tabcontent中膨胀framelayout
【发布时间】:2014-05-10 06:05:18
【问题描述】:

我需要你的帮助来解决简单的问题。 如何动态地将布局“连接”或膨胀到 Tab 内容中。 我想要 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>

下一步我使用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 添加新的布局文件,称为类别

<?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 Designer 来定义 TabHost,为什么?

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

谢谢。

【问题讨论】:

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


【解决方案1】:
// 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();
            }
        });
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    相关资源
    最近更新 更多