2013-03-22 60 views
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”。

回答

3

为完全透明的对话框u可以使用这样的:

步骤1>下创建 'RES' 中的 '值' 文件夹中的文件colors.xml和添加以下行..

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

步骤2>中创建下“RES”的“值”文件夹和如下的styles.xml文件...

<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> 

(I猜标记和属性是自解释的...。)

步骤3>其实这就是它........................

让我们将它添加到一个对话框... ..

步骤4>创建具有以下行的一类......

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(); 
    } 
} 

或U可以使用它来进行对话的吸引力增加模糊效果....

只是检查了这一点:有近ABT 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);