Android 如何为支持平板电脑和手机的应用程序提供导航
我知道以前可能有人问过这个问题,但因为我没有发现任何令人信服的东西,我需要再问一次 当您还需要提供一些分类导航面板(例如)时,如何设计一个支持平板电脑和手机的应用程序? 我想我知道如何使用带有手持设备和平板电脑的纵向导航抽屉,因为在这些情况下,我当时只显示一个窗格(片段)。但对于平板电脑来说,这是完全不同的,因为你有足够的空间来显示至少两个窗格(碎片),而这些窗格(碎片)在大多数情况下都会以一种“主要细节”的方式进行关联 编辑#1Android 如何为支持平板电脑和手机的应用程序提供导航,android,android-fragments,navigation-drawer,android-ui,landscape-portrait,Android,Android Fragments,Navigation Drawer,Android Ui,Landscape Portrait,我知道以前可能有人问过这个问题,但因为我没有发现任何令人信服的东西,我需要再问一次 当您还需要提供一些分类导航面板(例如)时,如何设计一个支持平板电脑和手机的应用程序? 我想我知道如何使用带有手持设备和平板电脑的纵向导航抽屉,因为在这些情况下,我当时只显示一个窗格(片段)。但对于平板电脑来说,这是完全不同的,因为你有足够的空间来显示至少两个窗格(碎片),而这些窗格(碎片)在大多数情况下都会以一种“主要细节”的方式进行关联 编辑#1 只是想提供一点关于我的应用程序的背景信息,以及为什么我认为除了
只是想提供一点关于我的应用程序的背景信息,以及为什么我认为除了导航列表之外,我还需要两个窗格 我的应用程序的主要目标是帮助侍者接受订单,这种情况在以下情况下起作用:
知道导航抽屉的基本结构与此类似:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
每个需要导航抽屉的活动现在都需要扩展BaseActivity
:
欧洲工会活动
public class EuropeanUnionActivity extends BaseActivity{
//...
}
此外,每个活动布局都需要在其代码中包含一个android.support.v4.widget.DrawerLayout
元素,如下所示:
欧洲联盟活动
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/main" />
<ListView
android:id="@+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
这是第86行:
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) { //Line 86
return true;
}
return super.onOptionsItemSelected(item);
}
这是相同的代码
活动\u europeanunion,但对列表视图使用include
标记
欢迎提出任何意见和建议。
谢谢
另外,我发现其中有一句话,说使用导航抽屉是不可取的。对于手机和平板电脑肖像模式,您可以使用如下布局:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/fragments_layout" />
<include layout="@layout/drawer_list" />
</android.support.v4.widget.DrawerLayout>
这可能是平板电脑横向模式的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/drawer_list" />
<include layout="@layout/fragments_layout" />
</LinearLayout>
这是抽屉清单:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="start" >
<ListView
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:background="#111"
android:divider="@android:color/darker_gray"
android:dividerHeight="0dp" />
</RelativeLayout>
和U布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment android:id="+@id/master_fragment" />
<fragment android:id="+@id/detail_fragment" />
</FrameLayout>
在活动中,您必须检查是否处于平板电脑横向模式,并显示两个片段。否则,您将首先显示主片段,隐藏细节
如果导航抽屉在所有活动上都可见,则声明一个负责抽屉的抽象活动。然后,所有的活动都继承自这个抽象活动。您发布的BaseActivity就是一个很好的例子“您如何处理“平板电脑横向”场景?”--嗯,将框架布局
替换为水平线性布局
,以容纳两个窗格?我看不出导航抽屉的影响有多大。您可以采用与不使用导航抽屉相同的方式实现主/细节。您甚至可以将线性布局
放在一个单独的XML文件中,并使用
将其与抽屉布局
一起放在总体布局中,如果这有助于您可视化问题的话。”服务员从左侧菜单(理想情况下是导航抽屉)中选择“接受订单”选项——导航抽屉中的条目应该是名词,根据谷歌的设计指南,不是动词/命令。“我想除了导航列表之外,我必须使用两个窗格”--是的,但是这些窗格可以包含在水平线性布局中DrawerLayout
是限制您在UI中拥有抽屉之外的“单一”东西的因素,但它没有说明“单一”东西的内部内容。如果你想要两个FrameLayout
s在那里,就去吧。当你不需要它时,你总是可以通过setVisibility(View.GONE)
让其中一个窗格消失。如果您正确使用权重,则另一个窗格将展开以填充所有可用空间。“那么,最终为手机和平板电脑(在任何方向)使用抽屉布局是否完全可行?”--当然。“你会使用这种方法吗?”——不是针对你描述的用法,因为我会坚持按照谷歌的设计指南(名词,而不是动词)使用抽屉。但是,除此之外,当然。我怀疑很多导航抽屉应用程序都采用“单一活动”模式,在这种模式下,所有东西都只是片段的来来往往,而不是在活动之间转换。话虽如此,如果您满意的话,我看不出您的方法有什么特别的问题。让我看看,我理解您。手机和平板电脑纵向模式的布局可以放在res\layout\main.xml
中,并且是默认布局,而平板电脑横向模式的布局可以放在res\layout-w720dp\main.xml
中,那又怎么样?java代码才是真正让我困惑的部分。使main.xml版本膨胀的活动是否负责加载总体布局,即指示应加载哪些片段并填充这些“插槽”的活动使用适当的片段并处理由片段中的UI工作触发的任何事件?系统负责加载layout\main.xml或\layout-w720dp\main.xml,但您将负责(在代码上)显示两个片段(平板电脑画面)或一次仅显示一个片段(手机)以及抽屉列表中选择的选项?如果是这样的话,这听起来像是一个单一活动的大量工作,但正如我在问题中提到的,我真的是一个新的碎片,我可能是一座山
private void setupNavDrawer() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//Please pay special attention to the following two lines
mDrawerLinearLayout = (LinearLayout) findViewById(R.id.menu_layout);
mDrawerListView = (ListView) mDrawerLinearLayout.findViewById(R.id.drawer);
mDrawerListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
String[] rows = getResources().getStringArray(R.array.drawer_rows);
mDrawerListView.setAdapter(new ArrayAdapter<String>(
this, R.layout.drawer_row, rows));
mDrawerToggle =
new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.drawer_open,
R.string.drawer_close);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View row,
int position, long id) {
//...
mDrawerLayout.closeDrawers();
}
});
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/fragments_layout" />
<include layout="@layout/drawer_list" />
</android.support.v4.widget.DrawerLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/drawer_list" />
<include layout="@layout/fragments_layout" />
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="start" >
<ListView
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:background="#111"
android:divider="@android:color/darker_gray"
android:dividerHeight="0dp" />
</RelativeLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment android:id="+@id/master_fragment" />
<fragment android:id="+@id/detail_fragment" />
</FrameLayout>