[英]Remove Black background from Alert Dialog Box - Android
我正在開發一個 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);
上面的代碼運行良好,但對話框出現黑色環境。
這是屏幕截圖:
現在我想從對話框中刪除這些黑色環境。
下面是對話框的代碼:
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();
}
});
}
國家列表.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>
我通過 stackoverflow 訪問了許多類似的鏈接,但沒有一個是有幫助的。
希望這會對你有很大幫助
我認為它的默認主題顏色是黑色所以你必須檢查你的AlertDialog
的導入它應該是import android.support.v7.app.AlertDialog;
還有一件事你應該為背景使用自定義顏色,如下所示
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
嘗試這個。
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.