2011-09-09 9 views
11

사용자 지정 대화 상자를 만들려고하는데 내용이 가운데에 놓여 있지만 항상 왼쪽 맞춤으로 끝납니다. 여기 aboutdialog.xml입니다 :Android Dialog의 레이아웃을 가운데에 배치하는 방법은 무엇입니까?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:a="http://schemas.android.com/apk/res/android" 
    a:orientation="vertical" 
    a:id="@+id/AboutDialogLayout" 
    a:layout_width="fill_parent" 
    a:layout_height="fill_parent" 
    a:layout_gravity="center_horizontal" 
    a:gravity="center_horizontal"> 

    <ImageView a:id="@+id/imageView1" 
       a:src="@drawable/pricebook" 
       a:layout_height="wrap_content" 
       a:layout_width="wrap_content"/> 
    <TextView a:layout_height="wrap_content"   
       a:textAppearance="?android:attr/textAppearanceLarge" 
       a:id="@+id/textView1" 
       a:text="Price Book" 
       a:layout_width="wrap_content"/> 
    <TextView a:layout_height="wrap_content" 
       a:id="@+id/textView2" 
       a:layout_width="wrap_content" 
       a:text="foo"/> 
</LinearLayout> 

그리고 내 (관련) 코드 :

Dialog d = new Dialog(this); 
d.setContentView(R.layout.aboutdialog); 
d.setTitle(R.string.app_name); 

내가 무슨 말이냐? 도움을 주셔서 감사합니다.

이미지 : Screen shot of Dialog problem

+0

이상한 일입니다. 그것은 나를 위해 잘 작동합니다. 스크린 샷을 게시 할 수 있습니까? – inazaruk

+3

+1을 사용하는 경우 : android : everywhere 대신에 어떻게 했습니까? –

+0

네, 저도 잘 작동하는지 확인합니다. –

답변

9

시도를 할 것이라고 생각 선형의 경우.

가 중앙에 나와 함께 일을하고 있지만

+3

RelativeLayout에서 전체 XML을 래핑하면됩니다. 두 레이아웃 모두에 대한 layout_width는'fill_parent'이어야하고 둘 다'layout_height'가'wrap_content'로 설정 될 필요가 있습니다 (적어도 그것이 내가 원하는 방식입니다). – Conrad

+0

상대 레이아웃이 트릭을했습니다. 감사합니다. –

+0

하지만 RelativeLayout의 하위 ViewGroup은 layout_width = fill_parent를 가져야 만 작동하며 언젠가는 콘텐츠에 해당 동작을 원하지 않는 경우가 있습니다. 아래의 내 솔루션 참조) –

0

당신은 좋은,하지만 당신은 최 레이아웃에 그것을 할 수 있기 때문에, 그것의 주위에 무엇을 자체는 상대적으로 위치 할 수 layout_gravity을하고 있습니다. 상대 wrap_content에 대한 fill_parent과 부모의 센터 a:layout_gravity="center_horizontal|center_vertical"
또는
사용 Relative_Layout 및 선형 레이아웃을 정렬 :

나는 다른 레이아웃 내부에 위의 레이아웃을 포장하는 것은 그것을 여기에 편집

+0

위의 XML을 다른 LinearLayout으로 래핑해도 차이가 발생하지 않는다. – Conrad

0

방금 ​​중력에있는 LinearLayout의 layout_gravity를 변경하려고 할 수 있습니까? 여기에 설명 된 차이점을 참조하십시오. Gravity and layout_gravity on Android

+0

차이가 없습니다. – Conrad

+0

LinearLayout 내부의 각 뷰에서 layout_gravity = "center"를 설정하는 방법은 무엇입니까? –

+0

이미 시도 - 주사위가 없습니다. – Conrad

2

수정 내용 레이아웃을 유지하는 대화 상자의 ViewGroup (FrameLayout). 정적 메서드를 사용하고 DialogFragments의 onActivityCreated() 내에서 호출합니다.

/** 
* Center dialog content inside available space 
* @param dialog 
*/ 
public static void centerDialogContent(Dialog dialog) { 
ViewGroup decorView = (ViewGroup) dialog.getWindow().getDecorView(); 
View content = decorView.getChildAt(0); 
FrameLayout.LayoutParams contentParams = (FrameLayout.LayoutParams)content.getLayoutParams(); 
contentParams.gravity = Gravity.CENTER; 
content.setLayoutParams(contentParams); 
} 
관련 문제