2017-12-11 43 views
1

Ich versuche, den Titel der Alertdialog Box zu ändern, aber die Ausgabe ist nicht das, was ich genau wollte. Ich schaffe den folgenden Stil in styles.xml:Titel Titel von AlertDialog ändern

<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> 

Der Java-Code ist wie folgt:

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

Das Bild Alertdialog angebracht ist. enter image description here

+0

Ihren Stil zu schaffen Problem in t sein Fall. Versuchen Sie es mit anderen Stil –

+0

Möchten Sie Actionbar ausblenden? – KeLiuyue

Antwort

1

aus dem Stil - Ich denke, Ihr Dialog einig Header-Layout muss es festgelegt, dass der Titel an der Spitze zu sein, verhindert, aber wenn das der Fall ist oder nicht - man kann leicht set a custom title für den Dialog Header nur gibt ihm das Header-Layout, und so erhalten Sie die volle Kontrolle für den Dialog Header:

// 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(); 

und für das Header-Layout (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

Vielen vielen Dank. Das ist genau das, was ich wollte. – John

+0

Sie sind willkommen, froh, dass Sie Ihr Problem gelöst haben :) –