我需要对话框来填充屏幕,除了顶部和底部的一些空间。
我正在寻找一个解决方案,但找不到一个可能是因为我在onClickListener
中声明它。
有人可以给出解决方案吗?
活动代码:
sort.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder sort = new AlertDialog.Builder(HomeScreen.this);
// Get the layout inflater
LayoutInflater inflater = HomeScreen.this.getLayoutInflater();
View sortView = inflater.inflate(R.layout.sort_layout, null);
sort.setView(sortView);
sort.create().show();
}
});
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:background="@color/white"
android:orientation="vertical" android:layout_gravity="center">
+ some more stuff here I dont think it's relevant
</LinearLayout>
答案 0 :(得分:39)
此处将屏幕宽度和宽度设置为对话框
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = width;
lp.height = height;
dialog.getWindow().setAttributes(lp);
或在你的风格中创建一个这样的风格
<style name="DialogTheme" parent="android:Theme.Dialog">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<!-- No backgrounds, titles or window float -->
<item name="android:windowBackground">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
</style>
创建一个像这样的对话框对象
dialog = new Dialog(this, R.style.DialogTheme);
编辑::
为它将填充的任何设备获得这样的宽度和高度..
WindowManager manager = (WindowManager) getSystemService(Activity.WINDOW_SERVICE);
int width, height;
LayoutParams params;
if (Build.VERSION.SDK_INT > VERSION_CODES.FROYO) {
width = manager.getDefaultDisplay().getWidth();
height = manager.getDefaultDisplay().getHeight();
} else {
Point point = new Point();
manager.getDefaultDisplay().getSize(point);
width = point.x;
height = point.y;
}
答案 1 :(得分:11)
首先在style.xml文件中为对话框创建自定义样式:
<style name="full_screen_dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
然后将此主题设置为警报对话框:
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.full_screen_dialog));
或者您可以调用新活动并将此自定义样式作为清单文件中的主题提供给该活动...
答案 2 :(得分:3)
试试这个
具有Theme.Dialog样式集的活动,执行以下操作:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
}
答案 3 :(得分:3)
这可能对某人有帮助。 我想要一个对话框来占据整个屏幕宽度。搜索了很多,但没有发现有用。最后这对我有用:
[arm64-v8a] Compile++ : ndkDemo <= main.cpp
[arm64-v8a] Compile++ : ndkDemo <= face_detector.cpp
[arm64-v8a] Compile++ : ndkDemo <= face_tracker.cpp
[arm64-v8a] Compile++ : ndkDemo <= ft_data.cpp
[arm64-v8a] Compile++ : ndkDemo <= patch_model.cpp
[arm64-v8a] Compile++ : ndkDemo <= shape_model.cpp
[arm64-v8a] Compile++ : dlib <= source.cpp
[arm64-v8a] StaticLibrary : libdlib.a
[arm64-v8a] SharedLibrary : libndkDemo.so
添加后,我的对话框显示在整个屏幕宽度。
答案 4 :(得分:2)
制作如下主题
的对象Dialog objD = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
答案 5 :(得分:0)
我想你应该试试这个:
在 dialogFragment :
内@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}
答案 6 :(得分:0)
Dialog dialog2 = new Dialog(context, R.style.DialogTheme);
dialog2.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog2.getWindow().setBackgroundDrawable(null);
dialog2.setContentView(R.layout.dialog_img);
WindowManager.LayoutParams lp = dialog2.getWindow().getAttributes();
Window window = dialog2.getWindow();
lp.copyFrom(window.getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(lp);
lp.gravity = Gravity.CENTER;
final ImageView imgprofile=(ImageView)dialog2.findViewById(R.id.img_centre);
Picasso.with(context)
.load(arrayImages.get(position).get("image"))
.resize(800,1000)
.centerInside()
.into(imgprofile, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError() {
imgprofile.setImageResource(R.drawable.user);
}
});
dialog2.show();
在你的dialog_img.xml文件中添加一个带有scaleType(fitXY)的Imageview。
答案 7 :(得分:0)
创建全屏对话框的最简单方法是创建如下样式:
<style name="DialogFullScreenTheme" parent="Theme.AppCompat.Light.DialogWhenLarge">
<item name="windowNoTitle">true</item>
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
</style>
然后:
dialog = new Dialog(this, R.style.DialogFullScreenTheme);