Android app动态添加按钮是不同颜色的布局按钮

时间:2015-07-30 09:56:22

标签: java android button

我尝试向DialogFragment添加一个新按钮,然后按钮出现,但字体和颜色与其他按钮完全不同。

其他按钮由XML文件中布局上的LayoutInflater生成。 XML文件中的按钮如下所示:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    ... other parts of the layout...

    <LinearLayout
    android:id="@+id/LL_buttons"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@id/RG1"
    android:layout_toRightOf="@id/RG1"
    android:layout_toEndOf="@id/RG1"
    android:layout_marginLeft="30dp"
    android:layout_marginStart="30dp"
    android:orientation="vertical"
    >
    <Button
        android:id="@+id/ok_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/fragment_add_custom_target_ok"

         />

    <Button
        android:id="@+id/cancel_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/fragment_add_custom_target_cancel"
        />
</LinearLayout>

</RelativeLayout>

然后(在某些情况下),添加删除按钮如下:

    public class CustomTargetPickerFragment extends DialogFragment {
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_add_custom_target, container, false);
        Dialog dialog=getDialog();
        dialog.setTitle(getString(R.string.custom_target_picker_title));

        // Get and process arguments
        Bundle bundle = getArguments();
        if (bundle.getBoolean(TAG_HAS_DELETE)) {
            // Add a delete button
            // Todo: not rendering properly

            LinearLayout layout = (LinearLayout) v.findViewById(R.id.LL_buttons);
            Button deleteButton = new Button(getActivity());
            deleteButton.setText(getString(R.string.custom_target_picker_delete_label));

            deleteButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            deleteButton.setOnClickListener(DeleteButtonListener);
            layout.addView(deleteButton);
        }

我应该怎么做才能使这个新按钮与布局创建的按钮相同? 非常感谢

2 个答案:

答案 0 :(得分:0)

您可以使用

在布局XML中添加deleteButton
android:visibility="gone"

然后在你的班级

if (bundle.getBoolean(TAG_HAS_DELETE)) {
        // Show a delete button

        Button deleteButton = (Button)v.findViewById(R.id.delete_button);
        deleteButton.setVisibility(View.VISIBLE);
        deleteButton.setOnClickListener(DeleteButtonListener);
    }

答案 1 :(得分:0)

将默认按钮属性设置为新添加的删除按钮,如下所示

Button deleteButton = new Button(getActivity());
deleteButton.setText(getString(R.string.custom_target_picker_delete_label));

deleteButton.setBackgroundResource(android.R.drawable.btn_default); //setting default button property.

deleteButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
deleteButton.setOnClickListener(DeleteButtonListener);
ayout.addView(deleteButton);

欢呼声..!