问题
I need yours help to resolve simple issue . How dynamically "connect" or inflate layout into Tab content. I want project with TabsHost . For each tab I like different layouts. Very simple:) I am creating default android project :
<?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>
< /manifest>
Next step I use eclipse designer and drag and drop TabHost into 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>
Next step I adding new layout file for tab1 called 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>
and Categories java class
All examples I used from internet do not use eclipse designer for TabHost definition ,Why?
How do I need to connect two layouts in code ?
Thanks.
回答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();
}
});
}
}
来源:https://stackoverflow.com/questions/23577698/android-inflate-framelayout-inside-tabcontent