安卓应用布局与导航抽屉

3

我是一名新手Android开发者,正在使用导航抽屉(Navigation Drawer)开发一个练习应用。我希望将主视图设置为一个具有3个选项卡的页面,并且当用户从左到右滑动时,导航抽屉能够显示出来。

这是我的Main.xml代码:

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_layout"
    tools:context=".ExampleMain" >

<!-- 
    Main Content place holder
 -->
<FrameLayout
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<!-- 
    Left Drawer main style
 -->
<ListView
    style="@style/HighlightColor"
    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.support.v4.widget.DrawerLayout>

使用 <Framelayout> 来包含在另一个 xml 文件Main_Content.xml 中定义的实际主视图。

<andriod.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_content_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

我不确定如何将 Viewpager 放置在 Main.xml<FrameLayout> 部分中。请提供一些示例代码或指针。谢谢。

3个回答

4

从技术上讲,你不应该在NavigationDrawer中使用ActionBar选项卡:

"你不应该在Action Bar选项卡中使用导航抽屉。如果你想要一个类似于Google Play音乐的UI界面,你应该手动实现选项卡(并注意在平板电脑上的外观——你不希望在平板电脑上出现全宽度的选项卡栏)。还要确保查看来自今年Google I/O的Android应用设计结构会话,以详细介绍可用于显示应用程序层次结构的各种界面模式。" - Roman Nurik, Google Android开发者倡导者

Main.xml中填充Activity时使用FragmentTransaction

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.main_content, fragment);
fragmentTransaction.commit();

ExampleFragment 将包含方法 onCreateView(),该方法将 Inflate 您的 XML:

@Override public View onCreateView(LayoutInflater layoutInflater, 
                                   ViewGroup container, 
                                   Bundle savedInstanceState) {
    return layoutInflater.inflate(R.layout.Main_Content, container, false);
}

1
没问题。基本上,谷歌建议您在导航选项卡中包含滑动视图。导航抽屉可以通过从左向右滑动打开,并且可以通过从右向左滑动(等等)关闭。现在,想象一下用户从左向右滑动以浏览选项卡。然而,由于滑动既被导航抽屉又被选项卡注册,所以现在滑动变得模糊不清。 - iamreptar
1
我遇到的另一个问题是,在一个选项卡实现了MapView时,在选项卡之间使用滑动导航。当你在地图选项卡上时,滑动只是浏览地图! - CodeMonkey
谢谢,你是正确的。关于滑动冲突问题,实际上我并不需要在分页器中使用片段进行滑动。你能否指出一些方法来禁用分页器的滑动,并仅注册导航抽屉的滑动? - Allan Jiang
2
假设您正在使用ActionBar选项卡,则三个步骤如下: (1)实现ActionBar.TabListener接口。 (2)对于要添加的每个选项卡,实例化一个ActionBar.Tab,并通过调用setTabListener()设置ActionBar.TabListener。 (3)然后通过调用addTab()将每个选项卡添加到操作栏中。 - iamreptar
显示剩余3条评论

1
您可以按照以下方式使用它:
<android.support.v4.widget.DrawerLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_layout"
        tools:context=".ExampleMain" >

    <!-- 
        Main Content place holder
     -->
    <FrameLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <andriod.support.v4.view.ViewPager
      android:id="@+id/main_content_pager"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>

    </FrameLayout>

    <!-- 
        Left Drawer main style
     -->
    <ListView
        style="@style/HighlightColor"
        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.support.v4.widget.DrawerLayout>

NavigationDrawer按照设计的方式进行设计,如果您尝试从屏幕左侧(或右侧)的20dp边距处滑动,则会显示出来。
编辑1: 如果您想将视图放在单独的xml中,则可以很好地执行此操作并在运行时添加视图。
请编写另一个xml,如下所示: view_pager.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
   <android.support.v4.widget.DrawerLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_layout" >

    <!-- 
        Main Content place holder
     -->
    <FrameLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <!-- 
        Left Drawer main style
     -->
    <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.support.v4.widget.DrawerLayout>

然后在您的活动的onCreate中,您可以添加以下代码:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
                ViewPage pager = (ViewPager) LayoutInflater.from(this).inflate(R.layout.view_pager, null, false);
                ViewGroup container = (ViewGroup) findViewById(R.id.main_content);
                container.addView(pager);
               }

我希望它有所帮助!


谢谢您的回答。实际上我尝试过了,但它却引发了一个错误:11-04 23:54:33.414: E/AndroidRuntime(936): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android/com.example.android.ui.ExampleMain}: android.view.InflateException: Binary XML file line #26: Error inflating class andriod.support.v4.view.ViewPager - Allan Jiang
另外,我有一个小顾虑,如果把所有东西都放在一个xml文件中,这是否会导致它们之间紧密耦合。是否有一种解决方案可以将视图组件分成两个文件,在运行时将它们组合在一起? - Allan Jiang

0

你可以通过动态膨胀布局来实现,或者如果是静态布局,可以使用 include 标签。

如果是静态布局:

<FrameLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/your_layout"/>
</FrameLayout>

但是既然您正在使用导航抽屉,我希望您需要的是动态的。

因此,从代码上来说,您需要做的是:

private void onNavigationClicked(){
    View v=LayoutInflater.from(this).inflate(R.layout.your_layout,null);
    FrameLayout fl = (FrameLayout)findViewById(R.layout.main_content);
    fl.removeAllViews();
    fl.addView(v);
    fl.invalidate();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接