2016-10-15 5 views
1

Android 앱을 개발 중입니다. 내 응용 프로그램에서 사용자 지정보기를 설정하는 경고 대화 상자를 표시하려고합니다. 사용자 정의보기 설정은 정상입니다. 하지만 대화 상자의 너비 설정에 문제가 있습니다. 너비를 설정할 수 없습니다. 그것은 항상 기본값입니다. 아래의 시나리오를 참조하십시오.Android에서 사용자 정의 대화 상자의 너비를 설정할 수 없습니다.

이 내가 당신은 내가 100dp 할 수있는 폭을 설정할 볼 수 있듯이

AlertDialog.Builder builder = new AlertDialog.Builder(context); 
      View view = layoutInflater.inflate(R.layout.meme_post_actions_dialog,null); 
      builder.setView(view); 
      builder.create().show(); 

대화 이것은 XML 레이아웃

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:gravity="center" 
    android:orientation="vertical" android:layout_width="100dp" 
    android:layout_height="match_parent"> 
    <TextView 
     android:text="This is dialog" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

입니다 보여주는 방법이다. 그래서 너비가 너무 작습니다. 그러나 이것이 내가 가진 것입니다.

enter image description here

어떻게 사용자 지정 경고 대화의 폭을 설정할 수 있습니까?

+0

안녕하세요, 와이 연의 하인이가 you.http 데 도움이 링크를 희망하려고 @ : //stackoverflow.com/questions/12478520/how-to-set- dialogfragments-width-and-height –

답변

0

제어 할 수있는 방법이 많이있을 수 있습니다. 경고창 너비와 높이를 설정하여 접근 방법 중 하나를 알려 드리겠습니다.

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
View view = layoutInflater.inflate(R.layout.meme_post_actions_dialog,null); 
     builder.setView(view); 
builder.setView(layout); 
alertDialog = builder.create(); 
alertDialog.show(); 
alertDialog.getWindow().setLayout(600, 400); //<--Controlling width and height. 

마지막으로 레이아웃의 부모가 부모와 일치해야합니다.

android:layout_width="match_parent" 
android:layout_height="match_parent" 
+0

고맙습니다. 이것은 효과가 있었다. 정말 고마워. :) –

+0

하지만 wrap_content를 설정하는 방법? –

+0

나는 alertDialog.getWindow()를 의미합니다. setLayout (600, WRAP_CONTENT); –

2

이 시도 :

AlertDialog.Builder builder = new AlertDialog.Builder(context); 
      View view = layoutInflater.inflate(R.layout.meme_post_actions_dialog,null); 
      builder.setView(view); 
int width = (int)(getResources().getDisplayMetrics().widthPixels*0.50); //<-- int width=400; 
int height = (int)(getResources().getDisplayMetrics().heightPixels*0.50);//<-- int height =300; 
AlertDialog alertDialog = builder.create(); 
alertDialog.getWindow().setLayout(width, height); 
+0

NO. 작동 안함. 그냥 똑같아. –