2017-08-24 41 views
2

我有一个自定义DialogFragment类,看起来像这样:DialogFragment不会尊重WRAP_CONTENT

/** 
* Dialog Fragment containing rating form. 
*/ 
public class RatingDialogFragment extends DialogFragment { 

    public static final String TAG = "RatingDialog"; 

    // ... 


    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, 
          @Nullable ViewGroup container, 
          @Nullable Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.dialog_rating, container, false); 
     ButterKnife.bind(this, v); 

     return v; 
    } 

    // ... 
} 

的根视图是一个LinearLayout

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="16dp"> 

每一个儿童观点有android:layout_height="wrap_content"

但它看起来像这样。我可能做错了什么?

enter image description here

+0

每个组件都有一个最小高度。它看起来对我是正确的。你能否澄清你的问题? – SoroushA

+0

我想DialogFragment包装其内容,所以它会在“提交”按钮结束。 –

+0

@hatboysam最好你发布你的整个xml dialogFragment来解决问题。我的一个代码中有一个DialogFragment,它可以正确工作('尊重')wrap_content。 –

回答

1

这似乎与布局和对话片段的一个问题。设置它改为手动:

@Override 
public void onResume() { 
    super.onResume(); 
    Window window = getDialog().getWindow(); 
    window.setLayout(your_value, your_value); 
    window.setGravity(Gravity.CENTER); 
} 

如果你需要用片段中的内容,你可以遍历的意见和总结的总高度,然后用其作为布局高度。

+0

我知道我可以手动设置,问题是如何让它实际包装。 –

+0

@hotboysam我在第二部分回答了你的问题。你需要找到高度并总结出来。对话框的高度是在布局之前确定的,这就是为什么你不能仅仅使用xml标签来包装它。 – SoroushA

1

这在我的布局中是部分问题。在我的对话框的底部我有一个按钮栏:

<!-- Cancel and apply buttons --> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

    <Button 
     android:id="@+id/button_cancel" 
     style="@style/Base.Widget.AppCompat.Button.Borderless" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="@string/cancel" 
     android:textColor="@color/greySecondary" 
     android:theme="@style/ThemeOverlay.FilterButton" /> 


    <Button 
     android:id="@+id/button_search" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="@string/apply" 
     android:theme="@style/ThemeOverlay.FilterButton" /> 

</LinearLayout> 

第一View(间隔)正在扩大以填充视口,将其更改为这个固定:

<View 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 

我也有将其添加到onResume中的DialogFragment

@Override 
public void onResume() { 
    super.onResume(); 
    getDialog().getWindow().setLayout(
      ViewGroup.LayoutParams.MATCH_PARENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT); 
}