Android 如何使这个简单的对话框片段半透明
我试图使这个简单的对话框半透明:Android 如何使这个简单的对话框片段半透明,android,actionbarsherlock,android-dialogfragment,Android,Actionbarsherlock,Android Dialogfragment,我试图使这个简单的对话框半透明: class TestDialog extends SherlockDialogFragment { public TestDialog() { super(); } @Override public Dialog onCreateDialog(Bundle savedInstanceStateBundle ) { AlertDialog.Builder builder
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:所需的min 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;
您可以在对话框片段引用中尝试这两个主题中的任何一个
yourdialogFragment.setStyle(style,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
或
希望这会有所帮助。您可以尝试使背景透明,如果您的超级类是一个对话框,请使用此选项:
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
或者,如果您的超类是DialogFragment:
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
我的另一个解决方案与“kaushal trivedi”类似。
使用DialogFragments:
public class MyDialogFragment extends DialogFragment {
...
@Override
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.my_dialog, container,false);
}
}
这使您的对话框成为全屏且没有背景,因此您可以在其中放置任何您想要的内容。试试这个
public class Dialogs extends DialogFragment {
public Dialogs() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style. Theme_WallpaperSettings);
}
}
你可以把
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
在对话框的onCreateView中也是如此。这就是我使用AlertDialog实现半透明的方法 创建了自定义样式:
<style name="TranslucentDialog" parent="@android:style/Theme.DeviceDefault.Dialog.Alert">
<item name="android:colorBackground">#32FFFFFF</item>
</style>
链接到我对相关问题的回答:我认为这可能取决于SDK版本。在1.6中,默认情况下对话框是半透明的,但请尝试使用
AlertDialog.Builder(上下文上下文,int-theme)
其中的主题是`android.R.style.theme\u Dialog。您是否尝试过在XML中使用类似于android:background=“#4000”
?(#4000是ARGB#u 4444颜色空间)。我两个都试过了,但都没用。没用。对话总是像地狱一样黑!查看我的更新帖子,看看我想要的对话框是什么。如果你想在对话框和窗口之间留一个空白,这不是一个可行的解决方案。
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
<style name="TranslucentDialog" parent="@android:style/Theme.DeviceDefault.Dialog.Alert">
<item name="android:colorBackground">#32FFFFFF</item>
</style>
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.TranslucentDialog);
AlertDialog dialog = builder.create();