[英]Make alert dialog background transparent
我正在Android Jelly Beans OS上創建一個警告對話框。 一切都運作良好,但我想要的是,而不是警報對話框的黑色背景,我想要一個透明的背景。 我在stackoverflow上閱讀了很多文章和用戶的問題,但沒有一個能幫助我。
這是我的代碼:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.CustomAlertDialog);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog1, int which) {
gActivityResult = REQUEST_PICK_CONTACT;
onResume();
return;
} });
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog1, int which) {
return;
} });
View view = getLayoutInflater().inflate(R.layout.alert_dialog, null);
builder.setTitle(R.string.gender_age);
builder.setInverseBackgroundForced(false);
builder.setView (view);
dialog = builder.create ();
這是我的CustomAlertDialog,它在res / values / styles.xml中定義
<style name="CustomAlertDialog" parent="@android:style/Theme.Dialog">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowBackground">@color/transparent_color</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:gravity">left</item>
</style>
這是color.xml
<resources>
<color name="transparent_color">#00000000</color>
</resources>
但它沒有幫助我。
我的問題:這可行嗎? 如果是的話,請你指導我正確的方向嗎?
我發現這種方式與AlertDialog.Builder兼容。 使用Eclipse中Android“Devices”選項卡中的“轉儲視圖層次結構”按鈕,我看到了許多嵌套的FrameLayouts,看起來背景就是這些,而不是窗口。
遍歷這些視圖並將背景設置為透明的每個實際工作(我的自定義視圖然后浮動在昏暗的應用程序上,沒有框架或背景,並且布局沒有變化)。 我有一個自定義視圖,因此從它遍歷,但從窗口遍歷ViewGroups也應該工作。
我在自己的DialogFragment子類中使用AlertDialog.Builder並使用自定義視圖。
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
configureDialogBuilder(builder);
final LayoutInflater inflater = getActivity().getLayoutInflater();
customView = inflater.inflate(customViewId, null);
builder.setView(customView);
final AlertDialog dialog = builder.create();
return dialog;
}
@Override
public void onStart() {
super.onStart();
clearBackgrounds(customView);
}
private void clearBackgrounds(View view) {
while (view != null) {
view.setBackgroundResource(android.graphics.Color.TRANSPARENT);
final ViewParent parent = view.getParent();
if (parent instanceof View) {
view = (View) parent;
} else {
view = null;
}
}
}
如果您還沒有閱讀自定義樣式的AlertDialog問題的樣式屬性 ,那么您應該繼續閱讀。 答案建議使用Dialog
而不是Alert Dialog
。 閱讀為什么LayoutInflater會忽略我指定的layout_width和layout_height布局參數? LayoutInflater
問題可能會更清楚一點。 希望能幫助到你。 嘗試一下,讓我知道它是否有效。
我必須為我的一個項目做類似的事情。 我所做的是為AlertDialog
創建一個Activity。
不確定這是否是您所追求的,但在此處發布,以防它幫助您...
活動
public class ShowAlertDialogActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alert_dialog);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// create the builder
AlertDialog alert = builder.create();
alert.show();
}
}
我在布局中將背景設置為透明( alert_dialog.xml )...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/transparent">
</LinearLayout>
並將活動添加到清單中......
<activity
android:name=".ShowAlertDialogActivity"
android:label="@string/app_title"
android:theme="@style/AppTheme"
android:launchMode="singleTask" />
這對我來說很有幫助。
希望這可以幫助。
只需打電話,
customDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
就在此之前
customDialog.show();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.