Android 以编程方式更改导航抽屉的边距
我有一个导航抽屉,但我想设置一些边距,就像在XML文件中设置Android 以编程方式更改导航抽屉的边距,android,android-fragments,navigation-drawer,margins,Android,Android Fragments,Navigation Drawer,Margins,我有一个导航抽屉,但我想设置一些边距,就像在XML文件中设置android:layout\u marginTop=“72dp”,但我想以编程方式设置边距 My activity_main.xml: <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:fitsSystemWindows="false" android:backgr
android:layout\u marginTop=“72dp”
,但我想以编程方式设置边距
My 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>
尝试:
我的任务是从屏幕边框(左侧/右侧)设置精确大小的边距,因此我在抽屉布局中覆盖了默认的
generateLayoutParams(attrs)
,以调整其视口的大小:
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;
}
}
它只是崩溃了-(我将编辑原始帖子,因为我无法在评论中发布logcat。请将FrameLayout更改为DrumerLayout并添加其ID。)。
FrameLayout root = (FrameLayout) findViewById(R.id.yourID);
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)root.getLayoutParams();
params.setMargins(left, top, right, bottom);
root.setLayoutParams(params);
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;
}
}