【问题标题】:Contents from FrameLayout appears on above Navigation Drawer:AndroidFrameLayout 的内容出现在导航抽屉上方:Android
【发布时间】:2015-04-24 13:07:38
【问题描述】:

我是 Android 新手,我正在实现一个导航抽屉,到目前为止,我已经设法在抽屉中列出我的项目,并在我的框架布局中添加了一个片段。 主页正常工作,但是当我打开抽屉时,我的 FrameLayout 中的按钮出现在导航抽屉的上方。

我需要让框架布局出现在导航抽屉下方。

我的代码如下

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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


    <fragment
        android:id="@+id/navigation_drawer"
        android:name="com.example.shimplyapp.NavigationDrawerFragment"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        tools:layout="@layout/fragment_navigation_drawer" />


</android.support.v4.widget.DrawerLayout>

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >


    </FrameLayout>

</RelativeLayout>

fragment_navigation_drawer.xml

<ListView 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:layout_gravity="start"    
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="@android:color/black"
    tools:context="com.example.shimplyapp.NavigationDrawerFragment" />

home_default.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:gravity="center"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/etxt_sn_enter_number"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="0dp"
                android:hint="enter_search_ item here.."
                android:inputType="text"
                android:maxLength="15"
                android:singleLine="true" />

            <Button
                android:id="@+id/btn_lm_search_number"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_marginTop="15dp"
                android:text="search"
                android:textColor="#ffffff"
                android:textSize="20sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btn_lm_view_stores"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_marginTop="15dp"
                android:text="store"
                android:textColor="#ffffff"
                android:textSize="20sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btn_lm_view_deals"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_marginTop="15dp"
                android:text="deal"
                android:textColor="#ffffff"
                android:textSize="20sp"
                android:textStyle="bold" />            
        </LinearLayout>
</RelativeLayout>

这是我从 MainActivity

调用的片段
@Override
public void onNavigationDrawerItemSelected(int position) {
    Fragment fragment = null;
    FragmentManager fragmentManager;

    //flag a global variable initially flag = 0;    

    if(flag ==0){
        fragment = new HomePage();
        fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
        flag = 1;
    }
        //..... rest is switch cases for calling different fragments 
}

【问题讨论】:

  • 将框架布局放在 xml 中的导航抽屉上方

标签: android navigation-drawer android-framelayout


【解决方案1】:

您的 activity_main.xml 应该是这样的。更改并测试

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

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

        <Fragment
            android:id="@+id/navigation_drawer"
            android:name="com.example.shimplyapp.NavigationDrawerFragment"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            tools:layout="@layout/fragment_navigation_drawer" />

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" >
        </FrameLayout>
    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

【讨论】:

  • 您实际上不需要围绕它的相对布局,但是是的,这是正确的。原因是,抽屉布局需要正好有 2 个子屏幕,主屏幕,然后是抽屉布局。如果需要,这些布局中的每一个都可以有子级。
  • 嗯,谢谢建议不要保留相对布局,忽略它,顺便说一句,当我将代码修改为这个新的 'activity_main.xml' 时发生了什么,当我向右滑动时,抽屉打开了,但是当我向左滑动它时它没有关闭。我必须点击上面的菜单栏才能关闭它。
  • 你能告诉我一些细节,说明你在活动中添加了什么以及如何添加抽屉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-08
  • 1970-01-01
相关资源
最近更新 更多