我尝试将边距添加到我的BottomSheetDialogFragment中,但是它对边距没有任何作用。
const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
编辑:_________________________________________________________________
我尝试将XML更改为下面的答案,但是它仍然没有为我的下层对话框碎片创造边距。
底部工作表对话框片段类的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="16dp">
<TextView
android:id="@+id/alertdialog_fragmail_newmessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test 1"
android:textStyle="bold"
android:padding="16dp"
android:textColor="@color/colorBlackFont"
android:layout_alignParentTop="true"
android:gravity="center_vertical"
android:drawablePadding="16dp"/>
//More Textviews
</RelativeLayout>
膨胀底表的代码:
public class FragMailMoreDialog extends BottomSheetDialogFragment {
private static final String TAG = "FragMailMoreDialog";
private Context mContext;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getContext();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.alertdialog_layout_fragmailmore, container, false);
ButterKnife.bind(this, view);
return view;
}
}
答案 0 :(得分:4)
一些棘手的解决方案:
我将布局包裹在另一个RelativeLayout中,并使该布局的背景透明。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@android:color/transparent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite"
android:layout_margin="16dp">
<TextView
android:id="@+id/alertdialog_fragmail_newmessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test 1"
android:textStyle="bold"
android:padding="16dp"
android:textColor="@color/colorBlackFont"
android:layout_alignParentTop="true"
android:gravity="center_vertical"
android:drawablePadding="16dp"
android:background="?attr/selectableItemBackground"/>
</RelativeLayout>
</RelativeLayout>
在BottomSheetDialogFragment中,您需要覆盖setupDialog
@Override
public void setupDialog(Dialog dialog, int style) {
View contentView = View.inflate(getContext(), R.layout.alertdialog_layout_fragmailmore, null);
dialog.setContentView(contentView);
((View) contentView.getParent()).setBackgroundColor(getResources().getColor(android.R.color.transparent));
}
贷记给该人:
答案 1 :(得分:1)
覆盖 setupDialog()
对我不起作用,而且我还需要使用 onCreateView()
来扩充对话框布局。因此,这可以在自定义的 BottomSheetDialogFragment
类中以编程方式解决:
onCreateDialog
或 getTheme()
中为对话框设置<style name="NoBackgroundDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowBackground">@null</item>
</style>
layoutParams
设置边距Java:
public class FragMailMoreDialog extends BottomSheetDialogFragment {
//.... Omitted code
@Override
public int getTheme() {
// Step 1
return R.style.NoBackgroundDialogTheme;
}
@Override
public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Step 2
addMargin(view);
}
private void addMargin(View view) {
FrameLayout.LayoutParams layoutParams =
(FrameLayout.LayoutParams) view.getLayoutParams();
int margin_16dp = dpToPx(16);
layoutParams.setMargins(margin_16dp, margin_16dp, margin_16dp, margin_16dp);
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;
}
}
科特林:
class FragMailMoreDialog() : 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.my_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
val margin_16dp = 16.toPx().toInt()
layoutParams.setMargins(margin_16dp, margin_16dp, margin_16dp, margin_16dp)
view.layoutParams = layoutParams
}
fun Number.toPx() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
this.toFloat(),
Resources.getSystem().displayMetrics
)
}
答案 2 :(得分:0)
您可以通过两种方法解决此问题:
第一个创建另一个嵌套布局,然后在此嵌套布局中添加边距而不是根布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp">
<TextView
android:id="@+id/alertdialog_fragmail_newmessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:drawablePadding="16dp"
android:gravity="center_vertical"
android:padding="16dp"
android:text="Test 1"
android:textColor="@color/colorBlackFont"
android:textStyle="bold" />
<!-- More Views -->
</RelativeLayout>
第二个在根布局的每个单独视图中添加页边空白
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/alertdialog_fragmail_newmessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="16dp"
android:drawablePadding="16dp"
android:gravity="center_vertical"
android:padding="16dp"
android:text="Test 1"
android:textColor="@color/colorBlackFont"
android:textStyle="bold" />
<!-- More Views -->
</RelativeLayout>
答案 3 :(得分:0)
不确定为什么要为BottomSheetDialogFragment提供边距。它是一个DialogFragment,显示在您的Activity / Fragment的顶部。向其添加边距不会执行任何操作。 如果您需要的是TextView的顶部填充(Test1)和TextView的底部填充(Test2),则应将padding_top添加到Test1,将padding_bottom添加到Test2,例如:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="16dp">
<TextView
android:id="@+id/alertdialog_fragmail_newmessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test 1"
android:textStyle="bold"
android:paddingTop="32dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingBottom="16dp"
android:textColor="@color/colorBlackFont"
android:layout_alignParentTop="true"
android:gravity="center_vertical"
android:drawablePadding="16dp"/>
</RelativeLayout>
答案 4 :(得分:0)
我为 Constraint 子级添加了边距,并为父级 Constraint 背景设置透明 Bottom Sheet layout Image
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#99000000"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
style="@style/BottomSheetDialogStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_medium"
android:layout_marginEnd="@dimen/margin_medium"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv_add_post_ic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_medium"
android:layout_marginTop="@dimen/margin_form"
android:layout_marginBottom="@dimen/margin_small"
android:src="@drawable/ic_add_post"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv_add_post"
style="@style/Widget.nejmo.Text.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_form"
android:layout_marginHorizontal="@dimen/margin_small"
android:layout_marginStart="@dimen/margin_small"
android:layout_marginBottom="@dimen/margin_small"
android:text="@string/option_add_post_txt"
android:textColor="@color/dusk"
app:layout_constraintBottom_toBottomOf="@+id/iv_add_post_ic"
app:layout_constraintStart_toEndOf="@+id/iv_add_post_ic"
app:layout_constraintTop_toTopOf="@+id/iv_add_post_ic" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv_add_question_ic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_medium"
android:layout_marginTop="@dimen/margin_medium"
android:layout_marginBottom="@dimen/margin_medium"
android:src="@drawable/ic_add_question"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/iv_add_post_ic" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv_add_question"
style="@style/Widget.nejmo.Text.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_form"
android:layout_marginHorizontal="@dimen/margin_small"
android:layout_marginStart="@dimen/margin_small"
android:layout_marginTop="@dimen/margin_medium"
android:layout_marginBottom="@dimen/margin_medium"
android:text="@string/option_add_question_txt"
android:textColor="@color/dusk"
app:layout_constraintBottom_toBottomOf="@+id/iv_add_question_ic"
app:layout_constraintStart_toEndOf="@+id/iv_add_question_ic"
app:layout_constraintTop_toTopOf="@+id/iv_add_question_ic" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
答案 5 :(得分:0)
100% 有效
覆盖 `/Testtt/${isData}`
返回您自己的对话框
onCreateDialog()
答案 6 :(得分:0)
类似于@Zain 的回答:
override fun onCreateView(...) {
...
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// FrameLayout.LayoutParams, CoordinatorLayout.LayoutParams or other container in your root layout.
((view.parent as View).layoutParams as FrameLayout.LayoutParams).topMargin = 100
}