如何以百分比设置DialogFragment的宽度? [英] How to set the width of a DialogFragment in percentage?
问题描述
我有一个DialogFragment
带有xml布局,我需要它是XML布局的75%.当我寻找答案时,我总是会找到常见的weight
-解决方案,但对我没有帮助,因为我的片段覆盖了当前活动,如下所示,并且没有占据全屏的大小.我也不想使用"ems"作为固定宽度,因为我不知道用户的屏幕尺寸.
I have a DialogFragment
with an xml layout and I need it to be, let's say 75% as wide as the screen. When I look for answers I always find the common weight
-solution, which does not help me, as my fragment overlays the current activity as shown below and does not take the full screen's size. I also don't want to use "ems" as a fix width as I can't know the user's screen size.
是否有一种方法(最好在xml中)将根LinearLayout
的宽度设置为所说的75%?我认为定义一个完整的LinearLayout
集合并使其中一些透明是一种可能的解决方法,但这似乎是一个非常丑陋的解决方案...
Is there a way (preferably in xml) to set the root LinearLayout
's width to said 75%? I think it would be a possible workaround to define a whole set of LinearLayout
s and make some of them transparent but this seems to be a rather ugly solution...
如果有帮助,这是片段的布局:
If it helps, here is the fragment's layout:
<?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>
推荐答案
我认为不使用代码就不可能实现这一目标.
I don't think it is possible to achieve this without using code.
无论如何,我在自定义DialogFragment类中使用此代码.希望对您有所帮助.
Anyway I'm using this code inside my custom DialogFragment class. Hope it helps.
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);
}
这篇关于如何以百分比设置DialogFragment的宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!