带抽屉导航的 Android 布局

标签 android android-layout layout android-viewpager android-ui

我是 android 的新手,正在开发一个使用抽屉导航的练习应用程序。我想让主视图成为具有 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"/>

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

最佳答案

从技术上讲,您不应该将 ActionBar 选项卡与 NavigationDrawer 一起使用:

"You shouldn't use navigation drawers with action bar tabs. If you're aiming for a UI similar to that of Google Play Music, you should implement tabs manually (and beware of how this looks on tablet—you don't want full-width tab bars on tablets). Also make sure to check out the Structure in Android App Design session from this year's Google I/O for a detailed run-through of the various interface patterns available for exposing app hierarchy." - Roman Nurik, Android Developer Advocate @ Google

使用 FragmentTransaction在膨胀 Main.xmlActivity 中:

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);
}

关于带抽屉导航的 Android 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19782046/

相关文章:

android - ArrayAdapter 中 ListView 第一行的不同布局

安卓 : How to change width of individual tabs in tab layout?

android - 在零位置插入 RecyclerView 项目 - 始终保持滚动到顶部

java - Jtree 和组合框布局

html - 将侧边栏(放置在内容之后)向左对齐

android - ListView : How to display item selector when rows have clickable widgets?

java - 无法解决方法 getResource().getColor() 错误

android - ImageButtons 没有对齐

css - 在 IE9 中向右浮动图像会下推下表中的文本

java - 通过代码动态选择 Spinner 中的项目