[英]Remove Black background from Alert Dialog Box - Android
I am working on an Android Application , Here I have created a custom dialog box , The dialog box was appearing on complete height of the device , but I wanted to leave margins from top and bottom , so I set margin using below code我正在开发一个 Android 应用程序,在这里我创建了一个自定义对话框,该对话框出现在设备的完整高度上,但我想保留顶部和底部的边距,所以我使用下面的代码设置边距
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = Utility.convertDPtoPixel(400, screen);
alertDialog.show();
alertDialog.getWindow().setAttributes(lp);
Above code worked perfectly, but the dialog appears with black surroundings.上面的代码运行良好,但对话框出现黑色环境。
Here is the screen shot:这是屏幕截图:
Now I want to remove these black surroundings from the dialog box.现在我想从对话框中删除这些黑色环境。
Here is the code of dialog box:下面是对话框的代码:
public void showDialog() {
AlertDialog.Builder builder;
final AlertDialog alertDialog;
//final Dialog alertDialog;
Context mContext;
mContext = screen;
LayoutInflater inflater = (LayoutInflater) LoginActivity.screen.getSystemService(LoginActivity.screen.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.countrylist, null);
LinearLayout rellayout = (LinearLayout) layout.findViewById(R.id.rellayout);
RelativeLayout closeAcessCodeDialog = (RelativeLayout)layout.findViewById(R.id.closeAcessCodeDialog) ;
ListView listview = (ListView) layout.findViewById(R.id.listview);
listview.setAdapter(new CustomAdapter1(LoginActivity.screen, countryList));
builder = new AlertDialog.Builder(LoginActivity.screen);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = Utility.convertDPtoPixel(400, screen);
alertDialog.show();
alertDialog.getWindow().setAttributes(lp);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id) {
countryName = idCountry.get(pos);
System.out.println("Country code is: " + countryName);
alertDialog.cancel();
text.setText(countryList.get(pos));
}
});
closeAcessCodeDialog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
}
countrylist.xml国家列表.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#ffe264"
app:cardCornerRadius="10dp"
>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rellayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_above="@+id/listview"
android:gravity="center_vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select your Country"
android:textColor="#000"
android:textSize="18sp"
android:textStyle="bold"
android:layout_centerInParent="true"
/>
<RelativeLayout
android:id="@+id/closeAcessCodeDialog"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:gravity="center">
<ImageButton
android:id="@+id/closeDialog"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/close_country_dialog" />
</RelativeLayout>
</RelativeLayout>
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="@android:color/transparent"
android:divider="#e5e5e5"
android:background="@drawable/list_bacground"
android:dividerHeight="1px" />
</LinearLayout>
I visited many similar links over stackoverflow , but no one was helpfull .我通过 stackoverflow 访问了许多类似的链接,但没有一个是有帮助的。
Hope this will help you a lot希望这会对你有很大帮助
I think its default theme color which is Black so you have to check the import of your AlertDialog
it should be import android.support.v7.app.AlertDialog;
我认为它的默认主题颜色是黑色所以你必须检查你的AlertDialog
的导入它应该是import android.support.v7.app.AlertDialog;
and one more thing you should use custom color for Background like below还有一件事你应该为背景使用自定义颜色,如下所示
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Try this.尝试这个。
Dialog alertDialog = new Dialog(this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.yourlayout);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.