Android导航抽屉 - 可以设置扩展距离吗?

时间:2014-01-24 23:08:51

标签: android

我已经实现了导航抽屉,看起来它延伸了大约80%的屏幕。

根据我显示的内容,它在手机上看起来延伸距离太小,在10英寸平板电脑上看起来太多了。

是否可以自定义导航抽屉延伸的距离?

编辑:布局XML添加

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

    <!-- MAIN CONTENT -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <!-- Title Bar -->
            <RelativeLayout 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/title_background">

                <TextView 
                    android:id="@+id/name_textview"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:textColor="@android:color/white"
                    android:textSize="30sp" />

                <Button 
                    android:id="@+id/drawer_btn"
                    android:layout_width="50dp"
                    android:layout_height="match_parent"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:background="@drawable/btn_background" />

            </RelativeLayout>

        </RelativeLayout>

    </FrameLayout>


    <!-- Right drawer with transparent background that pulls out to left and contains a webview to show local content -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:background="#00000000">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:scaleType="fitXY"
            android:src="@drawable/funny_face"
            android:contentDescription="@null" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#000000"
            android:orientation="vertical">

            <TextView 
                android:id="@+id/title_drawer"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:gravity="center"
                android:textColor="@android:color/white"
                android:textSize="30sp" 
                android:background="#000000"/>

            <WebView
                android:id="@+id/webview_drawer"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
        </LinearLayout>
    </LinearLayout>

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

1 个答案:

答案 0 :(得分:1)

可以通过设置抽屉的layout_width

以编程方式更改抽屉扩展距离

注意:默认情况下,如果layout_width设置为match_parent,那么您将在屏幕上获得80%扩展名的默认行为。


示例代码显示如何以编程方式调整抽屉扩展距离

XML布局中的

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

    <!-- MAIN CONTENT -->
    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false" >

        <!-- STUFF GOES IN HERE FOR YOUR MAIN UI -->

    </FrameLayout>


    <!-- Drawer with black background that pulls out to left: see layout_gravity to change direction -->
    <LinearLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:background="#000000">

        <!-- STUFF GOES IN HERE FOR YOUR DRAWER UI -->

    </LinearLayout>

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

在您的活动中:

// class members
private LinearLayout mDrawer;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layoutwithdrawer);

    mDrawer = (LinearLayout) findViewById(R.id.drawer_layout);

    // dynamically adjust the layout width so that the drawer extends more or less (code below uses 95%)
    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int width = displaymetrics.widthPixels;
    DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mDrawer.getLayoutParams();
    params.width = (int) (width * 0.95f);
    mDrawer.setLayoutParams(params);
}