【问题标题】:Designing Portrait for phones and Landscape for Tablets. Using Fragments为手机设计肖像,为平板电脑设计风景。使用片段
【发布时间】: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 和这个片段。我如何实现这一目标?

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

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

    在您的 onClick 侦听器中,您可以使用以下内容检查第二个片段占位符是否存在:

    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 开发者网站的以下部分很有帮助:

    【讨论】:

    • 我想只为平板设备提供横向模式,而我希望手机和平板手机显示纵向模式。实现这一目标的最佳方法是什么?此外,您的回答消除了许多担忧。谢谢 :) 我会带着结果回来的。
    • 对于检测表,您可以查看 stackoverflow.com/questions/5832368/tablet-or-phone-androidstackoverflow.com/questions/9884517/…(标记为前者的副本),但也可以搜索 [android] detect tablet有很多关于这个的帖子。我个人的看法是,用户对手机、平板手机和平板电脑的看法越来越模糊。作为应用设计师,我们应该仔细考虑分界点和用户期望,尤其是那些拥有较大手机的用户。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多