我有一个 DialogFragment
使用 xml 布局,我需要它,假设宽度为屏幕的 75%。当我寻找答案时,我总能找到共同点 weight
-解决方案,这对我没有帮助,因为我的片段覆盖了当前事件,如下所示,并且不占用全屏的大小。我也不想使用“ems”作为固定宽度,因为我不知道用户的屏幕尺寸。
有没有办法(最好在 xml 中)设置根 LinearLayout
的宽度要说75%?我认为定义一整套 LinearLayout
是一种可能的解决方法。 s并使其中一些透明,但这似乎是一个相当丑陋的解决方案......
如果有帮助,这里是片段的布局:
<?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);
}
关于android-layout - 如何以百分比设置 DialogFragment 的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26974068/