[英]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. 我正在尝试向DialogFragment添加新按钮,并且该按钮正在显示,但是字体和颜色与其他按钮完全不同。
The other buttons are generated by a LayoutInflater on a layout in an XML file. 其他按钮由LayoutInflater在XML文件中的布局上生成。 The buttons in the XML file look like:
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>
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 您可以使用以下命令在布局XML中添加deleteButton
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..! 干杯..!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.