2017-12-11 1 views
1

AlertDialog 상자의 제목 헤더를 변경하려고하는데 출력이 정확히 원하는 내용이 아닙니다. 나는 styles.xml에 다음과 같은 스타일을 만드는 오전 다음과 같이AlertDialog의 제목 헤더를 변경하십시오.

<style name="question_dialog" 
    parent="@android:style/Theme.Holo.Dialog"> 
    <item name="android:windowTitleStyle">@style/question_dialog_title</item> 
</style> 

<style name="question_dialog_title" parent="android:Widget.TextView"> 
<item name="android:background">#5cc5cc</item> 
<item name="android:textSize">21sp</item> 
<item name="android:textColor">#ffffff</item> 
</style> 

자바 코드는 다음과 같습니다

new AlertDialog.Builder(this, 
R.style.question_dialog).setTitle("Assam Quiz"). 
setMessage("Hello world Hello world"). 
setPositiveButton("OK", (dialog, which) - > 
{dialog.dismisd(); 
}).show(); 
} 

에 AlertDialog 이미지가 첨부되어 있습니다. 스타일 중 enter image description here

+0

톤에서 문제를 만드는 스타일 그의 사건. 다른 스타일로 시도하십시오 –

+0

액션 바를 숨기시겠습니까? – KeLiuyue

답변

1

-나는 당신의 대화가 상단에로 제목을 방지 그것에 설정 일부 헤더 레이아웃을 가지고 있다고 생각하지만, 그런 경우가 아닌지 경우 - 당신은 쉽게 set a custom title 대화 헤더 수 단지 그것을 헤더 레이아웃을 제공하고, 그래서 당신은 대화 헤더의 전체 제어해야합니다 :

// creating the Dialog 
AlertDialog.Builder builder = new AlertDialog.Builder(context); 
// getting dialog context 
Context mContext = builder.getContext(); 
// building the inflater 
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext); 
// inflate the dialog header layout 
View mView = mLayoutInflater.inflate(R.layout.simple_dialog_header, null); 
// get the TextView for the header (contained in the header layout) 
TextView mTextView = (TextView) mView.findViewById(R.id.title_text); 
// set the text for that TextView 
mTextView.setText(message); 
// set the custom header view for the dialog 
builder.setCustomTitle(mView); 
/* 
here you can set positive , negative ,neutral buttons 
or set the dialog message or any attribute you want 
*/ 
// finally, show the dialog 
builder.show(); 

를 헤더 레이아웃 (R.layout.simple_dialog_header)에 대한 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/primary" 
    android:orientation="vertical" 
    android:paddingBottom="15dp" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:paddingTop="15dp"> 

    <TextView 
     android:id="@+id/title_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:maxLines="1" 
     android:textAlignment="center" 
     android:textColor="@color/primary" 
     android:textSize="16sp" 
     android:textStyle="bold" /> 

</LinearLayout> 
+0

많은 많은 감사. 이것은 내가 정확히 원했던 것이다. – John

+0

오신 것을 환영합니다, 당신이 당신의 문제를 해결해 주셔서 감사합니다 :) –

관련 문제