我有一个带有 xml 布局的
DialogFragment
,我需要它的宽度为屏幕的 75%。当我寻找答案时,我总是找到常见的 weight
解决方案,这对我没有帮助,因为我的片段覆盖了当前活动,如下所示,并且不采用全屏大小。我也不想使用“ems”作为固定宽度,因为我无法知道用户的屏幕尺寸。
有没有办法(最好是在xml中)将根
LinearLayout
的宽度设置为75%?我认为定义一整套 LinearLayout
并使其中一些透明是一种可能的解决方法,但这似乎是一个相当丑陋的解决方案......
如果有帮助,这里是片段的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_height = "wrap_content"
android:layout_width = "match_parent"
android:orientation = "vertical" >
<EditText
android:hint = "@string/inputhint_feedbackName"
android:id = "@+id/input_feedbackName"
android:imeOptions = "actionNext"
android:inputType = "textPersonName"
android:layout_height = "wrap_content"
android:layout_width = "match_parent"
android:lines = "1"
android:maxLines = "1"
android:singleLine = "true" >
<requestFocus />
</EditText>
<EditText
android:gravity = "top"
android:id = "@+id/input_feedbackMessage"
android:imeOptions = "actionDone"
android:layout_height = "wrap_content"
android:layout_marginBottom = "20dp"
android:layout_width = "match_parent"
android:lines = "10"
android:inputType = "textMultiLine" />
<Button
android:id = "@+id/button_feedbackSend"
android:layout_height = "wrap_content"
android:layout_width = "match_parent"
android:text = "@string/label_go"
android:textSize = "16sp"
android:textStyle = "bold" />
</LinearLayout>
我认为不使用代码就不可能实现这一点。
无论如何,我在自定义 DialogFragment 类中使用此代码。希望有帮助。
public void onResume() {
super.onResume();
Window window = getDialog().getWindow();
Point size = new Point();
Display display = window.getWindowManager().getDefaultDisplay();
display.getSize(size);
int width = size.x;
window.setLayout((int) (width * 0.75), WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
}
您可以通过在对话框片段的 onCreate 方法上设置样式来实现此目的:
setStyle(DialogFragment.STYLE_NORMAL,R.style.yourStyle);
在你的 styles.xml 中
<style name="yourStyle" parent="android:Theme.Dialog">
<item name="android:windowBackground">@drawable/some_background</item>
<item name="android:windowAnimationStyle">@null</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowMinWidthMajor">75%</item>
<item name="android:windowMinWidthMinor">75%</item>
<item name="android:windowNoTitle">true</item>
</style>
覆盖对话框片段的 onResume() 方法,如下所示
override fun onResume() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val windowMetrics =
requireActivity().windowManager.currentWindowMetrics
val insets: Insets = windowMetrics.windowInsets
.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars())
val width = windowMetrics.bounds.width() - insets.left -
insets.right
val height = windowMetrics.bounds.height() - insets.top -
insets.bottom
val window = dialog!!.window
if (window != null) {
window.setLayout((width * 0.90).toInt(), (height *
0.90).toInt()) // for width and height to be 90 % of screen
window.setGravity(Gravity.CENTER)
}
super.onResume()
} else {
val window = dialog!!.window
val size = Point()
// Store dimensions of the screen in `size`
val display = window!!.windowManager.defaultDisplay
display.getSize(size)
// Set the width of the dialog proportional to 90% of the screen width
window.setLayout((size.x * 0.90).toInt(),
ViewGroup.LayoutParams.WRAP_CONTENT)
window.setGravity(Gravity.CENTER)
// Call super onResume after sizing
super.onResume()
}
}
您可以重写 onStart 并将此代码添加到此块
getDialog().getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);