如何在 ButtomSheetDialogFragment Android 中设置左右边距?
Posted
技术标签:
【中文标题】如何在 ButtomSheetDialogFragment Android 中设置左右边距?【英文标题】:how to set left and right margin in ButtomSheetDialogFragment Android? 【发布时间】:2017-02-01 21:48:48 【问题描述】:我尝试在 ButtonSheetDialogFragment
布局中设置 Margin 但它不起作用。我试图从布局和编程方式设置边距,但结果相同
这是我的 XML 文件 layout_bts_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:layout_margin="10dp"
android:background="#00000000">
<LinearLayout
android:layout_
android:layout_>
</LinearLayout>
</FrameLayout>
这是我的java代码
public class ButtomSheetFragment extends BottomSheetDialogFragment
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback()
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState)
if (newState == BottomSheetBehavior.STATE_HIDDEN)
dismiss();
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset)
;
@Override
public void setupDialog(Dialog dialog, int style)
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.layout_bts_item, null);
dialog.setContentView(contentView);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
if (behavior != null && behavior instanceof BottomSheetBehavior)
((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
int height = LayoutUtils.getScreenHeight(getActivity());
double desiredHeight = (0.85 * height);
((BottomSheetBehavior) behavior).setPeekHeight((int) desiredHeight);
contentView.getLayoutParams().height = (int) desiredHeight;
((FrameLayout.LayoutParams) contentView.getLayoutParams()).leftMargin = 100;
【问题讨论】:
什么是ButtonSheetDialogFragment
?
另外,你尝试了什么?发生了什么?
请检查 ButtonSheetDialogFragment developer.android.com/reference/android/support/design/widget/…
我无法设置边距。
您尝试设置什么边距?发生了什么,与您想要的有何不同?
【参考方案1】:
将此代码用于BottomSheetDialog
<style name="BottomSheetDialog" parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/bottomSheetStyleWrapper</item>
</style>
<style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal">
<item name="behavior_peekHeight">350dp</item>
<item name="android:layout_margin">30dp</item>
</style>
BottomSheetDialog dialog = new BottomSheetDialog(this, R.style.BottomSheetDialog);
dialog.setContentView(R.layout.custom_dialog);
dialog.show();
【讨论】:
【参考方案2】:完成边距设置后,请致电requestLayout()
。
在你的情况下,像
contentView.requestLayout();
添加左边距后。
【讨论】:
【参考方案3】:你可以通过设置对话框根布局的layoutParams.xxMrgin
来做到这一点;但他还要求使用不同的主题:
Java:
@Override
public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
FrameLayout.LayoutParams layoutParams =
(FrameLayout.LayoutParams) view.getLayoutParams();
int margin_10dp = dpToPx(10);
layoutParams.rightMargin = dpToPx(margin_10dp );
layoutParams.leftMargin = dpToPx(margin_10dp);
view.setLayoutParams(layoutParams);
view.requestLayout();
private int dpToPx(int dp)
Resources r = getResources();
int px = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp,
r.getDisplayMetrics()
);
return px;
科特林:
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
super.onViewCreated(view, savedInstanceState)
val layoutParams: FrameLayout.LayoutParams =
view.layoutParams as FrameLayout.LayoutParams
layoutParams.bottomMargin = 32.toPx().toInt()
layoutParams.rightMargin = 32.toPx().toInt()
layoutParams.leftMargin = 32.toPx().toInt()
view.layoutParams = layoutParams
fun Number.toPx() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
this.toFloat(),
Resources.getSystem().displayMetrics
)
改变主题:
选项 1:简单整洁
创建这种去除背景颜色的样式:
<style name="NoBackgroundDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowBackground">@null</item>
</style>
然后通过覆盖 getTheme()
将其应用于对话框:
// Kotlin
override fun getTheme(): Int
return R.style.NoBackgroundDialogTheme
// Java
@Override
public int getTheme()
return R.style.NoBackgroundDialogTheme;
选项 2:
为对话框设置android.R.style.Theme_Translucent
主题:
@Override
public int getTheme()
// Step 1
return android.R.style.Theme_Translucent;
但这需要再次保留变暗的背景:
getDialog().getWindow().setBackgroundDrawableResource(R.color.color_trans);
全班:
Kotlin 使用选项 1:
class MyDialogFragment() : BottomSheetDialogFragment()
override fun getTheme(): Int
return R.style.NoBackgroundDialogTheme
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View
val view: View = View.inflate(context, R.layout.fragment_bottomsheet, null)
return view
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
super.onViewCreated(view, savedInstanceState)
addMargin(view)
private fun addMargin(view: View)
val layoutParams: FrameLayout.LayoutParams =
view.layoutParams as FrameLayout.LayoutParams
layoutParams.bottomMargin = 32.toPx().toInt()
layoutParams.rightMargin = 32.toPx().toInt()
layoutParams.leftMargin = 32.toPx().toInt()
view.layoutParams = layoutParams
fun Number.toPx() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
this.toFloat(),
Resources.getSystem().displayMetrics
)
Java 使用选项 2:
public class ButtomSheetFragment extends BottomSheetDialogFragment
//...
@Override
public int getTheme()
// Step 1
return android.R.style.Theme_Translucent;
@Override
public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
// Step 2
getDialog().getWindow().setBackgroundDrawableResource(R.color.color_trans);
// Step 3
addMargin(view);
private void addMargin(View view)
FrameLayout.LayoutParams layoutParams =
(FrameLayout.LayoutParams) view.getLayoutParams();
int margin_10dp = dpToPx(10);
layoutParams.rightMargin = dpToPx(margin_10dp );
layoutParams.leftMargin = dpToPx(margin_10dp);
view.setLayoutParams(layoutParams);
view.requestLayout();
private int dpToPx(int dp)
Resources r = getResources();
int px = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp,
r.getDisplayMetrics()
);
return px;
【讨论】:
以上是关于如何在 ButtomSheetDialogFragment Android 中设置左右边距?的主要内容,如果未能解决你的问题,请参考以下文章
如何在异步任务中调用意图?或者如何在 onPostExecute 中开始新的活动?