我有一个带有从片段创建的侧边菜单的应用程序。
当我想在其中一个片段中创建标签时出现问题。
我尝试过很多例子而且不适合我。
fragment_home.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="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal"
></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
></FrameLayout>
</LinearLayout>
FragmentHome.java
public class FragmentInicio extends Fragment {
private FragmentTabHost mTabHost;
public FragmentInicio() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_home, container, false);
try{
//HERE TABHOST????
}catch(Exception e){
Log.e("UDI", e.getMessage());
}
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.d("EVENT", "OnCreate");
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
((HomeActivity) activity).onSectionAttached(1);
}
}
我该怎么办?
示例:
http://webdemo.com.es/sample2.jpg
谢谢!
答案 0 :(得分:0)
我知道我来晚了,但这可能会对其他人有所帮助。
要在片段中使用FragmentTabHost,可以执行此操作。
创建布局资源文件
fragment_tab_host.xml
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
ID为realtabcontent的FrameLayout将显示当前选项卡的内容,在本例中为FirstFragment,SecondFragment和ThirdFragment
TabHostWidgetFragment.java代码将是
public class TabHostWidgetFragment extends Fragment {
private static final String TAG = TabHostWidgetFragment.class.getSimpleName();
public TabHostWidgetFragment() { }
private FragmentTabHost fragmentTabHost;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
Log.i(TAG, "onCreateView: ");
return inflater.inflate(R.layout.fragment_frag_tab_host_widget, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(TAG, "onViewCreated: ");
fragmentTabHost = view.findViewById(android.R.id.tabhost);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(TAG, "onActivityCreated: ");
fragmentTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("First Tab").setIndicator("First Tab"), FirstFragment.class, null);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Second Tab").setIndicator("Second Tab"), SecondFragment.class, null);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Third Tab").setIndicator("Third Tab"), ThirdFragment.class, null);
}
public class FirstFragment extends Fragment {
private static final String TAG = FirstFragment.class.getSimpleName();
public FirstFragment() { }
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
Log.i(TAG, "onCreateView: ");
return inflater.inflate(R.layout.fragment_first, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(TAG, "onViewCreated: ");
}
}
public class SecondFragment extends Fragment {
private static final String TAG = SecondFragment.class.getSimpleName();
public SecondFragment() { }
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
Log.i(TAG, "onCreateView: ");
return inflater.inflate(R.layout.fragment_second, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(TAG, "onViewCreated: ");
}
}
public class ThirdFragment extends Fragment {
private static final String TAG = ThirdFragment.class.getSimpleName();
public ThirdFragment() { }
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
Log.i(TAG, "onCreateView: ");
return inflater.inflate(R.layout.fragment_third, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(TAG, "onViewCreated: ");
}
}
}
FristFragment,SecondFragment和ThirdFragment的布局资源
fragment_first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/firstTab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/nature1"/>
</LinearLayout>
fragment_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/secondTab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/nature2"/>
</LinearLayout>
fragment_third.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/thirdTab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="centerInside"
android:src="@drawable/nature3"/>
</LinearLayout>
如果要使其可滑动,请在ID为realtabcontent的FrameLayout之后添加ViewPager,如下所示,并实现viewPager逻辑。
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
TabHostWidgetFragment.java类将
创建单独的或内部的MyTabFactory.java类
public class MyTabFactory implements TabHost.TabContentFactory {
private final Context context;
public MyTabFactory(Context context) {
this.context = context;
}
@Override
public View createTabContent(String s) {
View v = new View(context);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
然后像这样将标签添加到tabhost。这对于使用MyTabFactory的setContent是必不可少的。如果您像我在代码片段中提到的那样添加选项卡,就会出现问题,它将添加两次片段,一次是由于TabHost,另一次是由于ViewPager。
fragmentTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("First Tab").setIndicator("First Tab").setContent(new MyTabFactory(getActivity())));
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Second Tab").setIndicator("Second Tab").setContent(new MyTabFactory(getActivity())));
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Third Tab").setIndicator("Third Tab").setContent(new MyTabFactory(getActivity())));
您可以使用 android.support.v4.app.FragmentTabHost 或 TabHost 。这两种方法都相同。