【发布时间】:2014-09-30 07:31:18
【问题描述】:
在我的应用程序中——它是专为手机设计的,我已经实现了片段标签。我想移植此应用程序以适应平板电脑设备。如果是平板电脑,我希望将方向设置为横向,如果是手机,我希望将方向设置为纵向。由于我已经在使用片段,因此我想利用在单个页面上并排显示片段并导航到电话中的不同片段的功能,如下所述:
电话:
平板电脑:
到目前为止,我正在使用 Main 活动并在这里定义我的标签:
主要活动
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab1 = actionBar.newTab().setIcon(R.drawable.tab1);
Tab1.setTag("Tab1");
Tab2 = actionBar.newTab().setIcon(R.drawable.tab2);
Tab2.setTag("Tab2");
Tab3 = actionBar.newTab().setIcon(R.drawable.tab3);
Tab3.setTag("Tab3");
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
Tab3.setTabListener(new TabListener(fragmentTab3));
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
actionBar.addTab(Tab3);
}
布局 MainActivity: (activity_main)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
我设置了如下标签监听器:
public class TabListener implements ActionBar.TabListener {
Fragment fragment;
public TabListener(Fragment fragment) {
this.fragment = fragment;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
第一个标签片段类签名:
public class FragmentTab1 extends Fragment
第一个选项卡(FragmentTab1)OnCreateView:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragmenttab1, container, false);
}
FragmentTab1 的 XML:(fragmenttab1.xml):
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_app" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="14dp"
android:layout_marginRight="03dp"
android:layout_marginTop="45dp"
android:cacheColorHint="#00000000"
android:fastScrollEnabled="true"
android:listSelector="@android:color/transparent" >
</ListView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="0dp" >
<AutoCompleteTextView
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/search"
android:ems="8"
android:hint="Search Contacts"
android:paddingLeft="36dp"
android:textColor="#FFFFFF" >
</AutoCompleteTextView>
</RelativeLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:layout_marginTop="10dp"
android:visibility="invisible" />
</FrameLayout>
现在在上面我已经为 listview 行设置了一个 onCLick 侦听器——在手机中我打开一个新片段,但在选项卡中我想在横向模式下并排显示 FragmentTab1 和这个片段。我如何实现这一目标?
【问题讨论】: