0

我有一个对话框样式的活动,它出现在我在 Android 应用程序中的主要活动上。我怎样才能让背景变得半透明?不透明,但半透明——比如 70% 不透明。我尝试将此主题应用于活动:

    <style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
      </style>  

以及对此的几种变体,但对话活动仍然显示为 100% 不透明。此外,活动本身(及其上显示的元素)的布局 xml,指定背景为“#70000000”。

4

1 回答 1

3

对于完全透明的对话框,您可以使用它:

步骤 1> 在 'res' 下的 'values' 文件夹中创建一个 colors.xml 文件并添加以下行..

<drawable name="transparent">#00000000</drawable>

步骤 2> 在 'res' 下的 'values' 文件夹中创建一个 styles.xml 文件以及以下几行...</p>

<style name="Transparent">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">
@android:style/Animation.Translucent
</item>
<item name="android:windowBackground">@drawable/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:colorForeground">#fff</item>
</style>

(我猜标签和属性是不言自明的......)

Step 3> 其实就是这样……………………</p>

让我们将其添加到对话框中......

第 4 步> 使用以下几行创建一个类……</p>

public class DialogBox extends Dialog {

    public DialogBox(Context context, int theme) {
        super(context, theme);
        setContentView(R.layout.dialog);
        okButton = (Button) findViewById(R.id.dialog_OkButton);
        setListeners();
    }
}

(确保为对话框创建布局)

步骤 5> 接下来创建一个活动类,如下所示……。

public class T_Temp extends Activity {

    private DialogBox dialog;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        dialog = new DialogBox(this, R.style.Transparent);
        dialog.show();
    }
}

或者您可以使用它来使对话框具有吸引力以添加模糊效果....

看看这个:有接近 30% 的透明度......

 dialog = new AlertDialog.Builder(WordCube.this)  
    .setTitle(WordCube.this.getResources().getString(R.string.app_name))  
    .setMessage(s)  
    .setIcon(R.drawable.logo)  
    .setPositiveButton(R.string.btn_close, null)  
    .show();  

下面显示了添加模糊和消除背景变暗所需的代码(因为我认为背景光线充足时模糊看起来更好)。

view plaincopy to clipboardprint?
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();  
lp.dimAmount=0.0f;  
dialog.getWindow().setAttributes(lp);  
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);  
于 2013-03-22T05:40:46.167 回答