[英]Android : Alert dialog background color not changing behind positive button
我在使用帶有edittext的custome AletDialog時遇到問題,當我將背景顏色設置為對話框時,除了包含正按鈕和負按鈕的部分之外,后面的顏色確實發生了變化。
對話框的布局文件:
<?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="match_parent"
android:background="@color/colorNoteBook"
android:padding="16dp">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="INSTRUCTIONS:" />
<com.example.recical.ui.MyLab.LinedEditText
android:id="@+id/editText_instructions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView2"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp"
android:background="@color/colorNoteBook"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:minLines="8" />
</RelativeLayout>
Java Class
public class CustomDialog extends AppCompatDialogFragment {
private EditText et_in_dialog;
private CustomDialogListener listener;
@NonNull
@Override**strong text**
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
LayoutInflater inflater=getActivity().getLayoutInflater();
View view=inflater.inflate(R.layout.layout_dialog,null);
builder.setView(view)
// .setTitle("Instructions")
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String instructions=et_in_dialog.getText().toString();
listener.applyText(instructions);
}
});
et_in_dialog=view.findViewById(R.id.editText_instructions);
return builder.create();
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
listener=(CustomDialogListener)context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()+"must implement CustomDialogListener");
}
}
public interface CustomDialogListener{ // this the interface tabbed activity will call
void applyText(String instructions);
}
}
我應該如何在 Ok 和自定義按鈕后面設置該區域的樣式? 我想讓它完全變黃
在樣式中添加主題
<style name="AlertDialog" parent="Base.Theme.AppCompat.Dialog.Alert">
<item name="android:windowBackground">@color/sub_primary_color</item>
</style>
並按照以下方式構建樣式
val builder = AlertDialog.Builder(activity, R.style.AlertDialog)
val dialog = builder.create()
dialog.show()
它對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.