简体   繁体   中英

Android app dynamically added button is different colour to layout buttons

I'm trying to add a new button to a DialogFragment, and the button is appearing, but the font and colour are completely different to the other buttons.

The other buttons are generated by a LayoutInflater on a layout in an XML file. The buttons in the XML file look like:

<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>

I am then (in some circumstances), adding a delete button as follows:

    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);
        }

What should I be doing to make this new button the same as the ones created by the layout? Many thanks

You could just add your deleteButton in your layout XML with

android:visibility="gone"

then in your class

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);
    }

set default button properties to your newly added delete button like this,

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);

cheers..!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM