2017-12-03 15 views
1

맞춤보기 레이아웃을 사용하여 알림 대화 상자를 만들려고하지만 대화 상자가 나타나지 않고 화면이 흐려집니다.맞춤 레이아웃이 표시된 AlertDialog가 표시되지 않습니다.

AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this); 
builder.setView(findViewById(R.id.system_profile_dialog)); 
AlertDialog setSysProfileDialog = builder.create(); 
setSysProfileDialog.show(); 

XML :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/system_profile_dialog" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:hint="@string/system_profile_hint" 
     android:inputType="number" /> 
</LinearLayout> 

답변

1

문제는이 라인은 다음과 같습니다

builder.setView(findViewById(R.id.system_profile_dialog)); 

변경은이에 :

builder.setView(LayoutInflater.from(this).inflate(R.layout.layout_name, null, false)); 

당신은하지 뷰의 id 현재 활동에, 거기에 부풀려을 통과하는 거 야.

0

Dialog dialog = new Dialog(this); 
     dialog.setContentView(R.layout.YourFile); 
     EditText editText = (EditText)dialog.findViewById(R.id.YourEditText); 
     dialog.show(); 
0

는 getApplicationContext를 호출, 실제 Context 객체로 빌더의 인스턴스를 시도 것을 시도()

0

그냥 builder.setView (findViewById (R.id.system_profile_dialog))를 대체하십시오. builder.setView (R.id.system_profile_dialog)를 사용하십시오. 공식 문서를 살펴보면 setView (View)는 뷰를 표시하는 것으로 설정되어 있지만 setView (int layoutResourceId)는 뷰를 팽창시키고 있음을 알 수 있습니다.

+0

해당 호출은 API 21 이상에서 유용합니다. –

+1

네 말이 맞습니다. <21을 목표로한다면 Vucko가 제안한 것을해야합니다. – colens

관련 문제