Android FragmentTabHost
Android FragmentTabHost
我已经实施了 FragmentTabHost,但我的 Fragment 超出了 tabhost。
这是我的 Activity:
public class MainActivity extends ActionBarActivity {
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(),android.R.id.tabhost);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
PlaceholderFragment.class,null);
mTabHost.addTab(mTabHost.newTabSpec("contacts")
.setIndicator("Contacts"), PlaceholderFragment2.class, null);
}
}
这是我的片段:
public class PlaceholderFragment2 extends Fragment {
public PlaceholderFragment2() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main2, container, false);
return rootView;
}
}
activity_main.xml
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity"
/>
</android.support.v4.app.FragmentTabHost>
和fragment_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/ic_launcher"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="159dp" />
</FrameLayout>
问题是片段超出了它的选项卡。
这是照片
将您的 activity_main.xml 更改为
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
和您的 activity 的 onCreate 到
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
PlaceholderFragment.class,null);
mTabHost.addTab(mTabHost.newTabSpec("contacts")
.setIndicator("Contacts"), PlaceholderFragment2.class, null);
}
尽管我强烈建议不要 使用 tabhost。我会选择 in the lines of this solution.
我已经实施了 FragmentTabHost,但我的 Fragment 超出了 tabhost。 这是我的 Activity:
public class MainActivity extends ActionBarActivity {
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(),android.R.id.tabhost);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
PlaceholderFragment.class,null);
mTabHost.addTab(mTabHost.newTabSpec("contacts")
.setIndicator("Contacts"), PlaceholderFragment2.class, null);
}
}
这是我的片段:
public class PlaceholderFragment2 extends Fragment {
public PlaceholderFragment2() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main2, container, false);
return rootView;
}
}
activity_main.xml
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity"
/>
</android.support.v4.app.FragmentTabHost>
和fragment_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/ic_launcher"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="159dp" />
</FrameLayout>
问题是片段超出了它的选项卡。
这是照片
将您的 activity_main.xml 更改为
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
和您的 activity 的 onCreate 到
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
PlaceholderFragment.class,null);
mTabHost.addTab(mTabHost.newTabSpec("contacts")
.setIndicator("Contacts"), PlaceholderFragment2.class, null);
}
尽管我强烈建议不要 使用 tabhost。我会选择 in the lines of this solution.