以下是我调用DialogFragment的方法:
DialogSelectAccount myDiag=new DialogSelectAccount();
myDiag.show(ft,"Diag" );
这是(部分)我的DialogFragment的创建方式:
public class DialogSelectAccount extends DialogFragment {
public DialogSelectAccount() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog_select_account, container, false);
tvMessage = (TextView) rootView.findViewById(R.id.tvMessage);
btnAccountPublic = (Button) rootView.findViewById(R.id.btnAccountPublic);
btnAccountEnterprise = (Button) rootView.findViewById(R.id.btnAccountEnterprise);
tvMessage.setText(message);
btnAccountPublic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Login.setAccountType = 2;
dismiss();
}
});
btnAccountEnterprise.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Login.setAccountType = 1;
dismiss();
}
});
return rootView;
}
这是我的DialogSelectAccount的 问题是总会显示一个令人生畏的白色背景,如下所示。如何删除它? <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff26b4e9"
android:orientation="vertical" >
<TextView
android:id="@+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="@android:color/black"
android:textSize="15dp"
android:textAlignment="center"
android:gravity="center_horizontal"
android:background="#ff26b4e9"
android:autoText="true">
</TextView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#ff26b4e9"
android:orientation="horizontal" >
<Button
android:id="@+id/btnAccountPublic"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:clickable="true"
android:text="@string/accountPub"
android:textColor="#ffffffff"
android:background = "@drawable/roundedbutton" />
<Button
android:id="@+id/btnAccountEnterprise"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:clickable="true"
android:text="@string/accountEnt"
android:textColor="#ffffffff"
android:background = "@drawable/roundedbutton" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:19)
在onCreateView()
的{{1}}中,替换
DialogFragment
与
View rootView = inflater.inflate(R.layout.dialog_select_account, container, false);
另外,请将其添加到View rootView = inflater.inflate(R.layout.dialog_select_account, container);
:
onViewCreated()
并在XML的最外层getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme);
中,更改
LinearLayout
到
android:layout_width="fill_parent"
android:layout_height="fill_parent"
试试这个。这应该有用。
答案 1 :(得分:1)
您可以为对话框创建样式:
<style name="DialogStyle" parent="Base.Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
并通过方法在代码中使用它:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new Dialog(getActivity(), R.style.DialogStyle);
}
或者您只能在代码中为对话框设置FEATURE_NO_TITLE
,如下面的代码所示:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
答案 2 :(得分:0)
在这里,我认为你试图隐藏标题栏。使用此
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
答案 3 :(得分:0)
我建议您使用DialogFragment的onCreateDialog中的自定义UI创建一个警报对话框。然后,您还可以轻松地向其中添加样式,以删除白色背景。
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val view = activity!!.layoutInflater.inflate(R.layout.dialogfragment_my_custom_view, null)
val builder = AlertDialog.Builder(activity!!, R.style.MyDialogTheme)
return builder
.setView(view)
.create()
}
然后您可以像这样创建“ MyDialogTheme”:
<style name="ProgressDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowBackground">@color/automile_transparent</item>
</style>
答案 4 :(得分:0)
较高的android版本设备会自动删除标题空间。但是对于较低版本,我们必须添加一些代码行。
在onCreateDialog()方法中添加 Window.FEATURE_NO_TITLE 更为有效。如下:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// request a dialog window without the title
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}