以编程方式更改导航抽屉的边距

时间:2014-01-12 14:59:24

标签: android android-fragments navigation-drawer margins

我有一个导航抽屉,但我想设置一些边距,就像在XML文件中设置android:layout_marginTop="72dp"一样,但我想以编程方式设置它们。

我的activity_main.xml:

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fitsSystemWindows="false"
    android:background="@drawable/back_blue_translucent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <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"
        android:fitsSystemWindows="false"
        tools:context="com.bentleycarr.mentalmaths.MainActivity">

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#f9f9f9"/>

        <fragment android:id="@+id/navigation_drawer"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:layout_marginTop="72dp" <!--I want to change this -->
            android:name="com.bentleycarr.mentalmaths.NavigationDrawerFragment" />

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

上面,它说android:layout_marginTop="72dp" - 我想在MainActivity.java中将'that'更改为另一个数字,但我只能找到有关如何对LinearLayout或RelativeLayout等执行此操作的信息,而不是<fragment android:id="@+id/navigation_drawer"非常感谢任何帮助/建议。

我试过Aashir的答案,但它崩溃了。这是日志: (暂时忽略这个 - 我可能添加了错误的ID。)

Process: com.bentleycarr.mentalmaths, PID: 23729
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bentleycarr.mentalmaths/com.bentleycarr.mentalmaths.MainActivity}: java.lang.ClassCastException: android.support.v4.widget.DrawerLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassCastException: android.support.v4.widget.DrawerLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
        at com.bentleycarr.mentalmaths.MainActivity.onCreate(MainActivity.java:55)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

2 个答案:

答案 0 :(得分:0)

尝试:

FrameLayout root = (FrameLayout) findViewById(R.id.yourID);
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)root.getLayoutParams();
   params.setMargins(left, top, right, bottom); 
   root.setLayoutParams(params);

答案 1 :(得分:0)

我有一项任务是从屏幕边框(左/右侧)设置精确尺寸的边距,因此我在抽屉布局中覆盖默认generateLayoutParams(attrs)以调整其viewPort的大小:

public class CustomMarginDrawerLayout extends DrawerLayout {
    private static final int DRAWER_MARGIN_IN_DP = 48;
    private final Context context;

    public CustomMarginDrawerLayout(Context context) {
        super(context);
        this.context = context;
    }

    public CustomMarginDrawerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public CustomMarginDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
    }

    @Override
    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
        ViewGroup.LayoutParams params = super.generateLayoutParams(attrs);
        int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DRAWER_MARGIN_IN_DP, getResources().getDisplayMetrics());
        params.width = getScreenWidth(context) - margin;
        return params;
    }

    private int getScreenWidth(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(metrics);
        return metrics.widthPixels;
    }
}