设计手机肖像和平板电脑风景。使用碎片

时间:2014-09-30 07:31:18

标签: android android-fragments

在我的应用程序中 - 我刚刚为手机设计了Fragment选项卡。我想移植此应用程序以满足平板电脑设备。如果它是平板电脑,我希望方向设置为横向,如果是手机,我希望设置为纵向。由于我正在使用片段,我想利用在单个页面上并排显示我的片段的工具,并导航到电话中的不同片段,如下所述:

电话:

enter image description here

平板:

enter image description here

到目前为止,我正在使用Main活动并在此处定义我的标签:

MainActivity

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 

第一个Tab(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监听器 - 在手机中我打开一个新的片段,但是在标签中我想在Landscape模式下并排显示FragmentTab1和这个片段。我该如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

您需要在 res / layout-large / 中为平板电脑大小的屏幕创建单独的布局。或者在 res / layout-large-land / 中。后者是指在平板电脑显示平板电脑时,您只需要平板电脑的备用布局。在其中,您为片段创建了两个占位符,通常是第一个列表和第二个中的详细视图,就像问题中的图像一样。

在onClick Listener中,您可以检查第二个片段占位符的存在,其中包含以下内容:

if (findViewById(R.id.second_fragment_container) == null)
{
    // the single fragment view is displayed - execute as your current listener
    // replacing the fragment container
}
else
{
    // Your duel fragment specific version.
    // replace only the second container, leaving the first displayed.
}

您可能会发现android开发者网站的以下部分有用:

相关问题