Android Can';无法获得透明的对话框片段
我有一个对话片段,看起来像这样Android Can';无法获得透明的对话框片段,android,android-dialogfragment,Android,Android Dialogfragment,我有一个对话片段,看起来像这样 AlertDialog ad = builder.create(); Drawable d = new ColorDrawable(Color.BLACK); d.setAlpha(130); ad.getWindow().setBackgroundDrawable(d); 这段代码的背景是半透明的。但底部还是有白色的部分。我想去掉白色,只有半透明的背景 我已经尝试了很多我在其他帖子中看到的东西 我不知道在DialogFragment、AlertDialog和
AlertDialog ad = builder.create();
Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(130);
ad.getWindow().setBackgroundDrawable(d);
这段代码的背景是半透明的。但底部还是有白色的部分。我想去掉白色,只有半透明的背景
我已经尝试了很多我在其他帖子中看到的东西
我不知道在DialogFragment、AlertDialog和LinearLayout之间必须更改的对象是什么
它可能不是线性布局,因为当我增加边际时,没有任何东西在移动
这是我的密码:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// setStyle(DialogFragment.STYLE_NORMAL, 0);
// setStyle(STYLE_NO_FRAME, R.style.CustomDialog);
// setStyle(STYLE_NO_FRAME, 0);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(
R.layout.share_or_die, null);
AlertDialog ad = builder.create();
Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(130);
ad.getWindow().setBackgroundDrawable(d);
ad.setCanceledOnTouchOutside(true);
ad.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
ad.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
return ad;
}
当用户单击“上一步”按钮时,我只是在mainActivity中调用它:
@Override
public void onBackPressed() {
if (isUserConnected && !hasShared) {
shareOnExitDialog = new ShareOnExitDialog();
shareOnExitDialog.setCancelable(true);
shareOnExitDialog.show(getSupportFragmentManager(), "Exit");
} else {
finish();
}
}
任何帮助都将不胜感激 问题在默认对话框主题中。基于答案和答案,实现目标要容易得多 活动应如下所示:
public class MyActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
}
@Override
public void onBackPressed() {
MyDialogFragment.newInstance("title title").show(getSupportFragmentManager(), "dialog");
}
}
和片段:
public class MyDialogFragment extends android.support.v4.app.DialogFragment {
/**
* Create a new instance of MyDialogFragment, providing "title"
* as an argument.
*/
static MyDialogFragment newInstance(String title) {
MyDialogFragment frag = new MyDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
final View view = getActivity().getLayoutInflater().inflate(R.layout.share_or_die, null);
final Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(130);
dialog.getWindow().setBackgroundDrawable(d);
dialog.getWindow().setContentView(view);
final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.CENTER;
dialog.setCanceledOnTouchOutside(true);
return dialog;
}
}
另外,请确保执行以下操作:在活动中的setContentView()
之前,应添加getWindow().requestFeature(Window.FEATURE\u NO\u TITLE)
,以便使用*NoTitleBar
样式
结果是(我使用了LinearLayout
和singleTextView
inside):
我希望它能帮助你
@Override
public void onStart() {
super.onStart();
Window window = getDialog().getWindow();
WindowManager.LayoutParams windowParams = window.getAttributes();
windowParams.dimAmount = 0.0f;
window.setAttributes(windowParams);
}
上述解决方案对我都不起作用。我发现有效的方法是确保导入了android.support.v7.app.AlertDialog而不是通用的app.AlertDialog
希望这对其他人也有帮助。您确定R.layout.share\u或\u die中没有白色背景吗?是的!我只是在里面有一个linearLayout,但是我更改了linearLayout属性,白色仍然保持不变。求救!您是否尝试过将单个文本视图而不是LL放入对话框中?另外,您使用该膨胀视图的位置在哪里?看起来问题中的代码不完整。我从不使用我的膨胀视图。当我放置单个textview时,它仍然显示为白色:(我得到一个:AndroidRuntimeException:requestFeature()必须在添加内容之前调用。而且我从未显式调用requestFeature()…仍然是相同的异常:(可能有用,请提一下我使用了compatibility FragmentDialog(v4),然后使用ttri来显示对话框(例如,我试图从onResume()中显示它。)当用户单击“上一步”按钮时,我正在显示对话框。我更新了代码以显示如何显示它。即使我这样做了,也无法正常工作:requestWindowFeature(Window.FEATURE\u NO\u TITLE);在activity的onCreate中,必须调用仍然抛出requestFeature()。非常感谢您的回答,这真的很有帮助)这是完全删除背景的必要步骤