2017-08-24 8 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; 
    } 

    // ... 
} 

ルートビューはすべて、単一の子ビューがandroid:layout_height="wrap_content"を持って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"> 

です。

ただし、このように見えます。私は何が間違っていますか?

enter image description here

+0

すべてのコンポーネントは、最小の高さを持っています。それは私に正しいように見える。あなたの質問を明確にすることができますか? – SoroushA

+0

DialogFragmentがコンテンツをラップするようにしたいので、 "Submit"ボタンで終了します。 –

+0

@hatboysamあなたは問題を解決するためにdialogFragmentの全体のxmlを投稿してください。私のコードの1つは、 'wrap_content'のために適切に動作する( 'respect')DialogFragmentです。 –

答えて

1

これは、レイアウトやダイアログ断片に問題があるようです。代わりに、手動で設定します。

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

あなたがフラグメントでコンテンツをラップする必要がある場合は、ビューを横断し、全体の高さを合計して、レイアウトの高さとしてそれを使用することができます。

+0

私はそれを手動で設定できることを知っています、質問は実際にそれをラップする方法です。 –

+0

@hotboysam私は第2部であなたの質問に答えました。あなたは高さを見つけてそれらを合計する必要があります。レイアウトの前にダイアログの高さが決定されているので、xmlタグを使用するだけでそれをラップすることはできません。 – SoroushA

1

これは部分的に私のレイアウトの問題でした。私のダイアログの下部には、私はボタンバーいた:最初のViewは(スペーサー)ビューポートを埋めるために拡大した

<!-- 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 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 

私も持っていましたDialogFragmentonResumeにこれを追加します。

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