2012-11-29 4 views
8

DialogFragment를 사용하고 있는데 layout_width 및 minWidth를 고정하여 얼마나 많은 contraints를 강제하려고해도 LinearLayout의 layout_width = match_parent를 만들 때 항상 너비가 항상 전체 화면이됩니다. 상위 뷰 또는 CreateView의 레이아웃 dimens()DialogFragment 크기 (너비) 무시

설정 dialog.xml의

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="@dimen/dialog_width" 
android:layout_height="@dimen/dialog_height" 
android:minWidth="@dimen/dialog_width" 
android:minHeight="@dimen/dialog_height" 
android:orientation="vertical" 
android:paddingTop="5dp"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    ... 
    </LinearLayout> 
</LinearLayout> 

Dialog.java

public class MyDialog extends DialogFragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     ... 
     Resources r = getActivity().getResources(); 
     getDialog().getWindow().setLayout((int)r.getDimension(R.dimen.dialog_width), (int)r.getDimension(R.dimen.dialog_height)); 
     return view; 
    } 
} 

오 내가이 일을 할 수 있습니다 할수 있답니다 방법은 = "@ DIMEN/dialog_width

해킹 dialog.xml 괜찮 작동하지만 대화의 다른 부분에 대한 몇 가지 결과를 초래

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
... > 

<LinearLayout 
    android:layout_width="@dimen/dialog_width" 
    android:layout_height="wrap_content"> 
    ... 
    </LinearLayout> 
... 
</LinearLayout> 

내부의 LinearLayout의 layout_width를 설정하는 것입니다. Android에서 버그입니까?

+0

사용 RelativeLayout의 모든 물건을 넣어 대신 선형 – Sameer

답변

2

이 문서는 아직 보지 못했지만 어딘가에 (아마도 SO 만) 대화 상자가 생성 될 때 루트 레이아웃의 layout_width 및 layout_height 매개 변수가 'wrap_content'로 오버라이드된다는 것을 읽었습니다.

사실인지 아닌지는 잘 모르겠지만 내가 본 레이아웃 동작은 그것이 사실임을 암시합니다. 어쨌든, 나는이 경우를 가정하여 대화 상자를 적절하게 배치했습니다.

가능하면 내부 레이아웃에도 wrap_content를 사용해보세요. 하드 코드 된 너비는 여러 장치에서 잘 작동하지 않는 경향이 있습니다.

-2

만 하나의 LinearLayout이

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="@dimen/dialog_width" 
android:layout_height="@dimen/dialog_height" 
android:minWidth="@dimen/dialog_width" 
android:minHeight="@dimen/dialog_height" 
android:orientation="vertical" 
android:paddingTop="5dp"> 
    ... 
</LinearLayout> 

를 사용하여 당신에게 코드를 교체하고 그것을

관련 문제