将复选框动态添加到 BottomSheetDialog:setMargins() 和 setPadding() 未按预期工作 [英] Adding checkboxes dynamically to BottomSheetDialog: setMargins() and setPadding() not working as expected

查看:25
本文介绍了将复选框动态添加到 BottomSheetDialog:setMargins() 和 setPadding() 未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将复选框动态添加到BottomSheetDialog.但是,按钮并没有像我希望的那样对齐.如果我直接在 XML 上对复选框进行样式设置,则可以对它们进行样式设置,但是,它们根本不会以编程方式设置样式.

I want to add checkboxes dynamically to a BottomSheetDialog. However, the buttons are not aligning as I want them to. Styling the checkboxes works if I style them directly on the XML, however, they don't style at all programmatically.

这是我的带有两个硬编码复选框的 XML 布局代码:

Here's my XML layout code with two hardcoded checkboxes:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/id_layout_bottom_sheet_choices"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="start"
        android:padding="10dp"
        android:text="Escolha uma resposta"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#000000"
        android:layout_marginStart="10dp"
        android:layout_marginBottom="10dp"
        android:textSize="20sp"
        android:textStyle="bold" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginBottom="10dp"
        android:paddingLeft="10dp"
        android:text="CheckBox" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginBottom="10dp"
        android:paddingLeft="10dp"
        android:text="CheckBox" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:orientation="horizontal">

        <Button
            android:id="@+id/cancel_btn"
            style="@style/Widget.AppCompat.Button.Borderless.Colored"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancelar" />

        <Button
            android:id="@+id/confirm_btn"
            style="@style/Widget.AppCompat.Button.Borderless.Colored"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="OK" />
    </LinearLayout>
</LinearLayout>

在这里,我尝试以编程方式添加更多:

Here, I try to add more of them programmatically:

LinearLayout mainLinearLayout = bottomSheetDialog.findViewById(R.id.id_layout_bottom_sheet_choices);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT
);
// margin-left: 10, margin-bottom: 10, like the margins in the two hardcoded xml checkboxes
params.setMargins(10, 0, 0, 10);

for (int i = 0; i < answerChoices.size(); i++) {
    CheckBox checkBox = new CheckBox(getContext());
    checkBox.setText(answerChoices.get(i));
    // left padding
    checkBox.setPadding(10,0,0,0);
    // i + 3 since there's already a textview and two sample checkboxes added, and i want to add the new checkbox after them and before the remaining elements
    mainLinearLayout.addView(checkBox, i + 3, params);
}

添加按钮后,这是带有该代码的BottomSheetModal 中的输出:

Here's of the output in the BottomSheetModal with that code, after the buttons are added:

如您所见,第一个硬编码的复选框已正确对齐,但通过编码添加的另外两个复选框却没有正确对齐.甚至复选框选择颜色也不同(但为什么???)

As you can see, the first hardcoded checkboxes are aligned correctly, however the two other checkboxes added via coding are not. Even the checkbox select colors are different (but why???)

这是我假装的输出(我硬编码了另外两个复选框只是为了展示我希望在编码后的输出)

Here's the output that I pretend (I hardcoded the other two checkboxes just to showcase how I want the output to be after coding)

我该如何解决这个问题?

How can I solve this problem?

推荐答案

因为在 xml 文件中你使用 dp 单位,而在代码中你使用 pixel 单位.在为复选框设置边距之前,您必须将 dp 转换为像素.

Because in xml file you use dp unit but in code you use pixel unit. You must convert from dp to pixel before set margin for checkbox.

LinearLayout mainLinearLayout = bottomSheetDialog.findViewById(R.id.id_layout_bottom_sheet_choices);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT
);

// margin-left: 10, margin-bottom: 10, like the margins in the two hardcoded xml checkboxes
int margin = (int)convertDpToPixel(10F, this);
params.setMargins(margin, 0, 0, margin);

for (int i = 0; i < answerChoices.size(); i++) {
    CheckBox checkBox = new CheckBox(getContext());
    checkBox.setText(answerChoices.get(i));
    // left padding
    checkBox.setPadding(margin, 0, 0, 0);
    // i + 3 since there's already a textview and two sample checkboxes added, and i want to add the new checkbox after them and before the remaining elements
    mainLinearLayout.addView(checkBox, i + 3, params);
}

// Add this method to convert dp to pixel
public static float convertDpToPixel(float dp, Context context){
    return dp * ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}

这篇关于将复选框动态添加到 BottomSheetDialog:setMargins() 和 setPadding() 未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆