导航抽屉不占用整个屏幕高度

时间:2014-03-10 06:48:41

标签: android android-view navigation-drawer drawerlayout

我的抽屉布局左侧有抽屉。

抽屉上有一个按钮,我想用新视图替换抽屉的视图。 问题是,当新视图被替换时,它不会占据整个屏幕高度。

为了说明我已经为导航抽屉视图提供了背景颜色。

我希望绿色视图占据整个屏幕高度。复制粘贴下面的代码以开始: 的 MainActivity.java

public class MainActivity extends Activity {
LayoutInflater inflater;
DrawerLayout drawer;
RelativeLayout parentRL;
RelativeLayout viewToBeRemovedRL;
View viewToBeAdded;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main_activity);
    inflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    parentRL = (RelativeLayout) findViewById(R.id.parentRL);
    viewToBeRemovedRL = (RelativeLayout) findViewById(R.id.viewToBeReplaced);
}

public void onRemoveView(View view) {
    viewToBeAdded = inflater.inflate(R.layout.add_view, null);
    viewToBeRemovedRL.setVisibility(View.GONE);
    parentRL.addView(viewToBeAdded);
}

public void onOpenDrawer(View view) {
    drawer.openDrawer(parentRL);
}
}

main_activity.xml

<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" >

<RelativeLayout
    android:id="@+id/leftRL"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="75dp"
        android:onClick="onOpenDrawer"
        android:text="Open Drawer" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/parentRL"
    android:layout_width="290dp"
    android:layout_height="match_parent"
    android:layout_gravity="left" >

    <RelativeLayout
        android:id="@+id/viewToBeReplaced"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FF0000" >

        <Button
            android:layout_width="100dp"
            android:layout_height="75dp"
            android:onClick="onRemoveView"
            android:text="Change View" />
    </RelativeLayout>
</RelativeLayout>

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

add_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="290dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00FF00" >

<TextView 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="New view"
/>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

通常情况下,抽屉实际上会占据整个屏幕高度,只是视图的默认背景颜色是清晰的。

将RelativeLayout @ id / parentRL的背景设置为纯色或某些可绘制的,例如

android:background="@android:color/black"

并且您的抽屉应覆盖整个屏幕高度。