我试图使这个简单的Dialog半透明:
class TestDialog extends SherlockDialogFragment { public TestDialog() { super(); } @Override public Dialog onCreateDialog(Bundle savedInstanceStateBundle ) { AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity()); // Get the layout inflater LayoutInflater inflater1 = getActivity().getLayoutInflater(); // Inflate and set the layout for the dialog // Pass null as the parent view because its going in the dialog layout builder1.setView(inflater1.inflate(R.layout.dlg_test,null)) // Add action buttons .setPositiveButton( "Ok",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog,int id) { // sign in the user ... } }) .setNegativeButton( "Cancel",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog,int id) { } }); return builder1.create(); } }
布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/test" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:layout_marginBottom="4dp" android:hint="User name" /> </LinearLayout>
我尝试了所有的东西,从使用透明的.png作为线性布局的背景可绘制,到将alpha字段设置为0.5,使用等于0的可绘制颜色.我无法使该对话框半透明.即使有可能,我也不知道.
我想要创建的是一个对话框,如下图中的面板:.
谢谢.
注1:所需的最小sdk是版本8,目标是最新的(实际上是v17).
解决方法
代替:
return builder1.create();
试试这个:
Dialog dialog = builder1.create(); Drawable d = new ColorDrawable(Color.BLACK); d.setAlpha(130); dialog.getWindow().setBackgroundDrawable(d); return dialog;