【发布时间】:2015-10-01 14:20:38
【问题描述】:
我正在尝试在一个 android 项目中创建一组垂直选项卡,我一直在查看一些关于 SO 的教程和帖子,但似乎无法将它们拼凑在一起。
我用一个片段创建了一个新项目,所以我有一个 main.xml 和一个 fragment_main.xml、一个主活动类和一个 mainfragment 类。
main.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment"
android:name="com.example.android.MainScreenActivityFragment"
tools:layout="@layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<FrameLayout android:layout_width="0dip"
android:layout_height="fill_parent" android:layout_weight="0.2">
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<LinearLayout
android:id="@+id/tab_btn_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1.0"
android:background="@color/sunshine_blue"
android:id="@+id/patient_tab_btn"
android:onClick="tabHandler"/>
<Button android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1.0"
android:background="@color/sunshine_blue"
android:id="@+id/relations_tab_btn"
android:onClick="tabHandler"/>
<Button android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1.0"
android:background="@color/sunshine_blue"
android:id="@+id/providers_tab_btn"
android:onClick="tabHandler"/>
</LinearLayout>
</FrameLayout>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="0dip"
android:layout_height="fill_parent" android:layout_weight="0.8"/>
</LinearLayout>
主要活动除了默认值之外没有做任何事情,我没有添加它,因为我认为我应该使用一个片段,因为谷歌阳光应用程序使用一个片段作为主屏幕。
主要片段类
private FragmentTabHost mTabHost;
Button patientButton, relationsButton, providersButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.tab_btn_container);
patientButton = (Button)rootView.findViewById(R.id.patient_tab_btn);
relationsButton = (Button)rootView.findViewById(R.id.relations_tab_btn);
providersButton = (Button)rootView.findViewById(R.id.providers_tab_btn);
return rootView;
}
public void tabHandler(View target){
Log.d(LOG_TAG, "Calling tabHandler() method");
patientButton.setSelected(false);
relationsButton.setSelected(false);
providersButton.setSelected(false);
if(target.getId() == R.id.patient_tab_btn){
//mTabHost.setCurrentTab(0);
patientButton.setSelected(true);
} else if(target.getId() == R.id.relations_tab_btn){
//mTabHost.setCurrentTab(1);
relationsButton.setSelected(true);
} else if(target.getId() == R.id.providers_tab_btn){
//mTabHost.setCurrentTab(2);
providersButton.setSelected(true);
}
}
所以我添加了 tabHandler 按钮,xml 在片段主类中膨胀,但按下任何选项卡按钮时都会出错:
java.lang.IllegalStateException: Could not find method tabHandler(View) in a parent or ancestor Context for android:onClick attribute
我也不确定如何更新 tabcontent 窗格,因为所有示例似乎都使用 Activity 并且活动类从 TabActivity 扩展,但使用片段我的类已经扩展了 Fragment。
如何让它与包含选项卡的片段一起使用?
【问题讨论】:
标签: android tabs fragment android-tabhost