2016-12-10 8 views
-1

나는 이것이 매우 나쁘다는 것을 안다. 두시간부터 AlertDialog에 대해 theme을 변경하려고하지만 그렇게하지 않으려 고합니다.대화 상자 배경을 변경할 수 없습니까?

부모 테마 :

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="android:background">@color/primary_background</item> 
    <item name="android:colorPrimaryDark">@color/primary_background</item> 
    <item name="android:navigationBarColor">@color/primary_background</item> 
    <item name="android:popupBackground">@color/primary_background</item> 
    <item name="android:alertDialogTheme">@style/myDialog</item> 
</style> 

대화 테마 :

<style name="myDialog" parent="Theme.AppCompat.Dialog"> 
    <item name="dialogPreferredPadding">@dimen/dialog_padding</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowBackground">@color/white</item> 
</style> 

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.myDialog)); 

내가 잘못 무슨 일이 일어나고 있는지 알아낼 수 없습니다입니다. 나는 이것이 매우 쉽다고 생각한다.

답변

0

대신에 xml 스타일을 사용하면 간단히 프로그래밍 방식으로 이해할 수 있습니다.

AlertDialog에 표시 할 XML 파일을 만드십시오.

예 :abc_dialog.xml입니다.

지금이 방법을

LayoutInflater inflater = getLayoutInflater(); 
View dialoglayout = inflater.inflate(R.layout.abc_dialog, (ViewGroup) getCurrentFocus()); 

을하고 AlertDialog에 해당 뷰를 설정합니다.

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setView(dialoglayout); 
builder.show(); 
+0

다음과 같이 구성 요소의 참조를 얻을 수 있습니다 내가 배경 색상, 텍스트 색상을 변경해야하는 경우.? –

+0

@AnkurKhandelwal은 배경색을 변경합니다. 대답에서 제안한대로'abc_dialog.xml'을 작성한 파일에서 배경색을 쉽게 변경할 수 있습니다. – Ironman

+0

그런 식으로 각 대화 상자의 배경색을 변경해야합니다. –

0

I. 대화 상자 배경에 사용자 정의 drawable background_dialog.xml을 선언하십시오.

<?xml version="1.0" encoding="utf-8"?> 
<!-- From: support/v7/appcompat/res/drawable/abc_dialog_material_background_light.xml --> 
<inset xmlns:android="http://schemas.android.com/apk/res/android" 
    android:insetLeft="16dp" 
    android:insetTop="16dp" 
    android:insetRight="16dp" 
    android:insetBottom="16dp"> 

    <shape android:shape="rectangle"> 
     <corners android:radius="2dp" /> 
     <solid android:color="@color/indigo" /> 
    </shape> 

</inset> 

II. styles.xml 파일에서 사용자 정의 스타일을 선언하십시오.

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert"> 
    <!--buttons color--> 
    <item name="colorAccent">@color/pink</item> 
    <!--title and message color--> 
    <item name="android:textColorPrimary">@android:color/white</item> 
    <!--dialog background--> 
    <item name="android:windowBackground">@drawable/background_dialog</item> 
</style> 

iii. AlertDialog.Builder에서 대화 상자를 만들고 매개 변수로 스타일을 사용하십시오.

AlertDialog.Builder builder = 
     new AlertDialog.Builder(this, R.style.MyDialogTheme); 
... 
AlertDialog dialog = builder.create(); 
// display dialog 
dialog.show(); 

자세한 내용은 document을 참조하십시오.

또는 당신은 프로그래밍 방식으로 아래와 같은 대화 상자의 사용자 정의보기를 설정할 수 있습니다

.

LayoutInflater inflater = getLayoutInflater(); 
View dialoglayout = inflater.inflate(R.layout.dialog_layout, (ViewGroup) getCurrentFocus()); 
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setView(dialoglayout); 
builder.show(); 

다음 시간 이후에, 당신은

Button btn = (Button) dialoglayout.findViewById(R.id.button_id); 
+0

이것은 단지 ok와 취소가 기록 된 하단 라인 만 변경합니다. –

+0

테마 솔루션이 작동하지 않는 이유는 무엇입니까? –

+0

' ​​@ color/white –