[英]Custom AlertDialogs/Dialogs in Android appearance issues
I have implemented the following Dialog in Android but have certain problems that I cant figure out why it is happening. 我已经在Android中实现了以下Dialog,但遇到了一些我无法弄清其原因的问题。
Dialog
and not AlertDialog
, this message disappears when I touch elsewhere on the screen. 当我使用Dialog
而不是AlertDialog
,当我触摸屏幕上的其他位置时,此消息消失。 I want to prevent this from happening and want the message box to be closed only when the user selects any one of the two options. 我想防止这种情况的发生,并希望仅当用户选择两个选项中的任何一个时才关闭消息框。 Here are the XMLs 这是XML
custom_alert.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:background="@drawable/custom_alert_layout"
android:id="@+id/alert_layout"
android:paddingBottom="15dp"
android:paddingTop="10dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/alert_icon_imageview"
android:src="@drawable/alerticon"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/alert_msg"
android:layout_alignStart="@+id/alert_msg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/alert_title"
android:text="TITLE"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#0a4d4a"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/alert_icon_imageview"
android:layout_toEndOf="@+id/alert_icon_imageview"
android:layout_marginLeft="20dp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="10dp"
android:layout_marginRight="4dp"
android:layout_marginLeft="2dp"
android:id="@+id/alert_divider_imageview"
android:layout_below="@+id/alert_title"
android:src="@drawable/alertdivider"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/alert_msg"
android:text="MESSAGE"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="16sp"
android:layout_below="@+id/alert_divider_imageview"
android:textColor="#ff373334"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginRight="4dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="10dp"
android:id="@+id/alert_divider_imageview2"
android:layout_below="@+id/alert_msg"
android:src="@drawable/alertdivider"/>
<Button
android:layout_width="150dp"
android:id="@+id/alert_cancel"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="15dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:text="CANCEL"
android:background="@drawable/custom_alert_cancel"
android:layout_height="wrap_content"
android:layout_below="@+id/alert_divider_imageview2"
/>
<Button
android:layout_width="150dp"
android:id="@+id/alert_ok"
android:text="ERASE"
android:textStyle="bold"
android:layout_marginRight="10dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="15dp"
android:textColor="#ffffff"
android:background="@drawable/custom_alert_ok"
android:layout_toRightOf="@+id/alert_cancel"
android:layout_height="wrap_content"
android:layout_below="@+id/alert_divider_imageview2" />
</RelativeLayout>
custom_alert_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" >
<shape android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="#139977" />
<stroke android:width="2dp" android:color="#0a4d4a" />
</shape>
</item>
</selector>
Can you post your Activity's code ? 您可以发布活动代码吗?
Try this in your Activity : 在您的活动中尝试以下操作:
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.alert_dialog);
Button okBtn = (Button) dialog.findViewById(R.id.ok_btn);
Button cancelBtn = (Button) dialog.findViewById(R.id.cancel_btn);
okBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
doErase();
}
});
cancelBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setCancelable(false);
dialog.show();
to solve 3rd problem, remove buttons from xml and use inbuild button of dialog. 要解决第三个问题,请从xml中删除按钮,然后使用对话框的内置按钮。 methods like setPositiveButton()
and setNegativeButton()
of dialog
class. dialog
类的setPositiveButton()
和setNegativeButton()
之类的方法。 it will give you same size button. 它将为您提供相同尺寸的按钮。
to solve the 1st problem you can use in xml like 解决您可以在xml中使用的第一个问题
<item name="android:background">@android:color/transparent</item>
or in java 或用Java
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Just add a line to your dialog box which remove your actionbar and title bar of the dialog: 只需在对话框中添加一行,即可删除对话框的操作栏和标题栏:
Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //add this line
dialog.setContentView(R.layout.activity_main);
for solve your first problem add this code before setContentView
为了解决您的第一个问题,请在setContentView
之前添加此代码
Window window = actDialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
window.setAttributes(wlp);
window.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
actDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
your second problem solution 您的第二个问题解决方案
actDialog.setCanceledOnTouchOutside(false);
your third problem solution here 您的第三个问题解决方案
place your two Button
in LinearLayout
and give layout_weight
same 将您的两个Button
放在LinearLayout
并给layout_weight
相同
1.To remove white space at top: add these two lines to your dialog , 1.要删除顶部的空白:请将这两行添加到对话框中,
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.alert_dialog);
remove white space at bottom,in your code just remove below line 删除底部的空白,在代码中删除下面的行
<corners android:radius="10dp"/>
from your custom_alert_layout.xml file. 从您的custom_alert_layout.xml文件中。
2.To solve second problem:add below two lines to your code, 2.解决第二个问题:将以下两行添加到您的代码中,
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
3.To get same width for buttons use LinearLayout as parent for both the buttons,and give equal weights for buttons. 3.要获得相同的按钮宽度,请使用LinearLayout作为两个按钮的父级,并为按钮赋予相等的权重。
For your 3rd problem here is code 对于您的第三个问题,这里是代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:id="@+id/alert_cancel"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:layout_marginTop="15dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:text="CANCEL"
android:background="@drawable/custom_alert_cancel"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="match_parent"
android:id="@+id/alert_ok"
android:text="ERASE"
android:textStyle="bold"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="15dp"
android:textColor="#ffffff"
android:background="@drawable/custom_alert_ok"
android:layout_height="wrap_content"
/>
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.