[英]Create Custom AlertDialog Builder and Change Font of Title & Message
[英]Custom AlertDialog Title with default font
我的應用程序中的所有對話框都使用AlertDialog構建,但我的藍牙選擇器對話框除外。 我的目標是為此創建一個AlertDialog。
問題:
如果我想在右上方添加一個旋轉進度條,我需要使用AlertDialog的Builder的.setCustomTitle(view)方法。 這有效,但我也想為這個標題使用默認的AlertDialog標題樣式,但我不確定如何做到這一點。
所以基本上我希望第二個屏幕截圖的藍色文字看起來與第一個屏幕截圖中的黑色文本完全相同。 我寧願動態地這樣做,而不是僅僅找出AlertDialog使用的確切值並對其進行硬編碼。
我的嘗試:
我查看了AlertDialog的源代碼,發現“AlertDialog使用的實際樣式是私有實現”。 但是我也發現他們使用R.attr.alertDialogTheme作為屬性, 我可以得到主題的resourceId
我嘗試使用該resourceId創建一個TextView,但這不起作用(TextView apeared就像它沒有樣式)。
默認AlertDialog標題樣式:
我目前的頭銜:
您可以使用以下代碼重新創建上面顯示的對話框: https : //gist.github.com/anonymous/34edc1afbdb7e9d10d9adbda63d4cd8c
xml的要求是: content.xml和title.xml (以及colors.xml和styles.xml )
您已經定義的文字顏色 android:textColor="#33B5E5"
在你的TextView的title.xml
。 只需將其更改為您想要的顏色即可。
這是一個例子:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:paddingTop="9dp"
android:paddingBottom="18dp">
<TextView
android:id="@+id/customTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Custom title view"
android:textColor="#000000"
android:textSize="24sp"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/anotherText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/customTitle"
android:paddingTop="9dp"
android:text="Another TextView"
android:textColor="@android:color/holo_purple" />
<LinearLayout
android:id="@+id/lin_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:orientation="vertical"
android:layout_centerVertical="true">
<ProgressBar
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/loading_spinner"
android:visibility="invisible"/>
</LinearLayout>
</RelativeLayout>
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
final View yourTitleView = inflater.inflate(R.layout.title, null);
TextView yourTitle = (TextView) yourTitleView.findViewById(R.id.customTitle);
yourTitle.setText("Your new title text");//Dynamical text
yourTitle.setTextColor(Color.parseColor("#ffa000")); //Dynamical color
//Your AlertDialog
new AlertDialog.Builder(this)
.setCustomTitle(yourTitleView) //The title's TextView's style should be the
// same as the the one of the dialog normal AlertDialog's Title Style (see example above)
.setView(inflater.inflate(R.layout.content, null))
.setPositiveButton("Start", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.setNegativeButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.show();
不要忘記為您的觀點定義好的ID。
要使用自定義對話框格式化文本,只需使用Html函數
Html.fromHtml("<font color='#000000'> Your black text </font>")
例:
new AlertDialog.Builder(YourActivity.this)
.setTitle(Html.fromHtml("<font color='#000000'> Your black title </font>"))
.setView(yourView)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Stuff to do
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Stuff to do
dialog.dismiss();
}
})
.show();
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.