Android 片段底部的制表符
我有一个来自母活动的Android 片段底部的制表符,android,android-fragments,android-tabs,fragment-tab-host,Android,Android Fragments,Android Tabs,Fragment Tab Host,我有一个来自母活动的片段,我想在其中使用FragmentTabHost显示两个选项卡。但是我希望选项卡位于片段的底部,而不是顶部,因为这是默认设置 MainFragment.java: public class MainFragment extends Fragment { private FragmentTabHost tabHost; private View rootView; public View onCreateView(LayoutInflate
片段
,我想在其中使用FragmentTabHost
显示两个选项卡。但是我希望选项卡位于片段的底部,而不是顶部,因为这是默认设置
MainFragment.java:
public class MainFragment extends Fragment {
private FragmentTabHost tabHost;
private View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//tabHost = new FragmentTabHost(getActivity());
rootView = inflater.inflate(R.layout.fragment_main, container, false);
tabHost = (FragmentTabHost) rootView.findViewById(R.id.tabhost);
tabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);;
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab 1"), ConnectFragment.class, null);
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab 2"), ManageFragment.class, null);
tabHost.setCurrentTab(0);
return tabHost;
}
}
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+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" >
<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TabWidget
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
我使用的布局非常基本,当我运行这个版本时,一切都正常,但选项卡显示在片段的顶部
fragment\u main.xml:
public class MainFragment extends Fragment {
private FragmentTabHost tabHost;
private View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//tabHost = new FragmentTabHost(getActivity());
rootView = inflater.inflate(R.layout.fragment_main, container, false);
tabHost = (FragmentTabHost) rootView.findViewById(R.id.tabhost);
tabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);;
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab 1"), ConnectFragment.class, null);
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab 2"), ManageFragment.class, null);
tabHost.setCurrentTab(0);
return tabHost;
}
}
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+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" >
<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TabWidget
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
当我运行一个稍微不同的版本时,根据这个站点的其他线程,应该把标签放在片段的底部,我得到一个错误
fragment\u main\u v2.xml:
public class MainFragment extends Fragment {
private FragmentTabHost tabHost;
private View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//tabHost = new FragmentTabHost(getActivity());
rootView = inflater.inflate(R.layout.fragment_main, container, false);
tabHost = (FragmentTabHost) rootView.findViewById(R.id.tabhost);
tabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);;
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab 1"), ConnectFragment.class, null);
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab 2"), ManageFragment.class, null);
tabHost.setCurrentTab(0);
return tabHost;
}
}
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+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" >
<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TabWidget
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
抛出错误:
public class MainFragment extends Fragment {
private FragmentTabHost tabHost;
private View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//tabHost = new FragmentTabHost(getActivity());
rootView = inflater.inflate(R.layout.fragment_main, container, false);
tabHost = (FragmentTabHost) rootView.findViewById(R.id.tabhost);
tabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);;
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab 1"), ConnectFragment.class, null);
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab 2"), ManageFragment.class, null);
tabHost.setCurrentTab(0);
return tabHost;
}
}
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+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" >
<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TabWidget
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
android.view.InflateException:指定的子级已具有父级。必须首先对子级的父级调用removeView()
我可以想象,这辆车出了问题
android.view.InflateException:指定的子级已具有父级。必须首先对子级的父级调用removeView()