更改对话框片段中自定义对话框的高度
我创建了一个自定义对话框,代码如下。问题是,对话框的高度变成了wrap_content,即它与我在xml中提到的高度无关。我检查了其他问题,它们对我没有帮助。
public class PointsDialogFragment extends DialogFragment{
private static final String TAG = PointsDialogFragment.class.getSimpleName();
public static PointsDialogFragment newInstance(){
PointsDialogFragment pdf = new PointsDialogFragment();
Bundle newBundle = new Bundle();
pdf.setArguments(newBundle);
return pdf;
}
private View mRoot;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().requestWindowFeature(STYLE_NO_TITLE);
mRoot = inflater.inflate(R.layout.fragment_points_dialog, null);
return mRoot;
}
}
fragment_points_dialog.xml 的 xml 是
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="313dp"
android:layout_width="fill_parent"
android:background="@color/green_gradient_start"
>
<RelativeLayout
android:layout_width="173dp"
android:layout_height="242dp"
android:background="@drawable/reward_box"
android:id="@+id/reward_box"
android:layout_alignParentLeft="true"
>
<TextView
android:layout_centerInParent="true"
style="@style/WallSectionHeading"
android:text="5"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="173dp"
android:layout_height="250dp"
android:background="@drawable/reward_mascot"
android:layout_alignParentRight="true"
>
<LinearLayout
android:id="@+id/reward_cloud"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:background="@drawable/reward_cloud"
android:layout_alignParentBottom="true"
>
<TextView
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You have gained 5 Delight Points"
android:textColor="@color/white"
android:textStyle="italic"
android:paddingLeft="15dp"
android:paddingRight="10dp"
/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
我正在显示这样的对话框。
PointsDialogFragment pdf = PointsDialogFragment.newInstance();
pdf.show(getFragmentManager(), "dialog");
我想知道一种可以在对话框片段中更改对话框高度的方法。
I have created a custom dialog, the code is below. The problem is that, the height of the dialog is becoming wrap_content, i.e it is independent of what ever the height i mention in the xml. I have checked other questions, they din't help me.
public class PointsDialogFragment extends DialogFragment{
private static final String TAG = PointsDialogFragment.class.getSimpleName();
public static PointsDialogFragment newInstance(){
PointsDialogFragment pdf = new PointsDialogFragment();
Bundle newBundle = new Bundle();
pdf.setArguments(newBundle);
return pdf;
}
private View mRoot;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().requestWindowFeature(STYLE_NO_TITLE);
mRoot = inflater.inflate(R.layout.fragment_points_dialog, null);
return mRoot;
}
}
and the xml for fragment_points_dialog.xml is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="313dp"
android:layout_width="fill_parent"
android:background="@color/green_gradient_start"
>
<RelativeLayout
android:layout_width="173dp"
android:layout_height="242dp"
android:background="@drawable/reward_box"
android:id="@+id/reward_box"
android:layout_alignParentLeft="true"
>
<TextView
android:layout_centerInParent="true"
style="@style/WallSectionHeading"
android:text="5"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="173dp"
android:layout_height="250dp"
android:background="@drawable/reward_mascot"
android:layout_alignParentRight="true"
>
<LinearLayout
android:id="@+id/reward_cloud"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:background="@drawable/reward_cloud"
android:layout_alignParentBottom="true"
>
<TextView
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You have gained 5 Delight Points"
android:textColor="@color/white"
android:textStyle="italic"
android:paddingLeft="15dp"
android:paddingRight="10dp"
/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
I am showing dialog like this..
PointsDialogFragment pdf = PointsDialogFragment.newInstance();
pdf.show(getFragmentManager(), "dialog");
I would like know a way i can change the height of the dialog with in the dialog fragment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题在于,当您膨胀视图时,您没有提供 ViewParent。当您不提供该参数时,视图根目录中的任何“layout_X”参数都将被忽略,因为这些值将提供给父级(在当前情况下为 null)。您可以做的就是在膨胀时提供 ViewParent 或将 View XML 包装在另一个 ViewGroup 中,将绝对布局参数保留在第二层。
示例代码:
mRoot = inflater.inflate(R.layout.fragment_points_dialog,container, false);
I think the problem is that when you inflate the View, you're not supplying the ViewParent. When you don't supply that parameter, any "layout_X" parameters in the root of your View will be ignored, as those values are supplied to the parent (which is null in your current situation). What you can do is either supply a ViewParent when inflating or wrap your View XML in another ViewGroup, leaving the absolute layout parameters in the 2nd level.
Sample code:
mRoot = inflater.inflate(R.layout.fragment_points_dialog, container, false);
在 DialogFragment xml 中,不使用 LinearLayout,而是使用relativelayout,并将layout_height 设置为wrap_content,它将自动调整视图大小。
确保不要使用对齐父底部,否则它将填满屏幕。
In your DialogFragment xml, instead of using LinearLayout use a RelativeLayout and set the layout_height to wrap_content it will resize automatically the view.
Make sure to not use align parent bottom otherwise it will fill the screen.