在android中更改alertdialog的标题大小?

时间:2012-11-12 12:58:28

标签: android alertdialog

关于消息中的文字没有问题。我不能改变(个性化)标题警报颜色,字体,样式?

有什么办法?

谢谢!

5 个答案:

答案 0 :(得分:6)

关于字体大小,您可以使用:

SpannableStringBuilder ssBuilser = new SpannableStringBuilder("Sample");
StyleSpan span = new StyleSpan(Typeface.ITALIC);
ScaleXSpan span1 = new ScaleXSpan(1);
ssBuilser.setSpan(span, 0, 5, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
ssBuilser.setSpan(span1, 0, 5, Spanned.SPAN_INCLUSIVE_INCLUSIVE);

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
   builder.setTitle(ssBuilser);
   builder.show();

AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show();
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
textView.setTextSize(40);

答案 1 :(得分:3)

点击此链接:Creating dialogs

这应该有所帮助。

Alert Dialog Builder

答案 2 :(得分:1)

这是一个完整的例子,我将如何做到这一点。请参阅最后一行代码。

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(cntx);
alertDialogBuilder
.setMessage(Html.fromHtml(cntx.getString(R.string.UltimateDemoText)))
.setCancelable(false)
.setPositiveButton(cntx.getString(R.string.Ok), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,int id) { 
    dialog.cancel(); 
    }
 });
AlertDialog alertDialog = alertDialogBuilder.create();    
alertDialog.show();

((TextView)alertDialog.findViewById(android.R.id.title)).setTextSize(TypedValue.COMPLEX_UNIT_SP, 9);

答案 3 :(得分:1)

还有另一种方法可以将新的自定义视图设置为对话框标题。我们可以定义每个自定义视图,例如TextView,并添加一些自定义属性并将其设置为对话框标题:

 AlertDialog.Builder builder = new AlertDialog.Builder(OrderItemsActivity.this);

TextView title_of_dialog = new TextView(getApplicationContext());
title_of_dialog.setHeight(50);
title_of_dialog.setBackgroundColor(Color.RED);
title_of_dialog.setText("Custom title");
title_of_dialog.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
title_of_dialog.setTextColor(Color.WHITE);
title_of_dialog.setGravity(Gravity.CENTER);

builder.setCustomTitle(title_of_dialog);
builder.create().show();

在这里,我定义了一个动态TextView并为其设置了一些属性。最后,我使用setCustomTitle()将其设置为对话框标题。

答案 4 :(得分:0)

让我们以这个自定义对话框为例。以下是用于制作它的文件:

res / layout / dialog.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">  <EditText
    android:id="@+id/et_pc_forDialog"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/tv_pc_dialogTitle"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="20dp"
    android:ems="10"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:hint="Enter Something to search .."
    android:singleLine="true"
    android:textColor="#FFFFFF"  >
    <requestFocus />
</EditText>
<TextView
    android:id="@+id/tv_pc_dialogTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="5dp"
    android:text="SEARCH"
    android:textColor="#FFFFFF"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
    android:id="@+id/bt_pc_goButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/et_pc_forDialog"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="14dp"
    android:text="GO"
    android:textColor="#FFFFFF"
    android:textSize="25sp" /> </RelativeLayout>

<强> RES /布局/ activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="177dp"
    android:text="Open Dialog" />
</RelativeLayout>

MainActivity.java:

package com.bhavit.stackoverflow;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.Dialog;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.app.Dialog;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button openDialog = (Button) findViewById(R.id.button1);
    openDialog.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            Dialog dialog = new Dialog(MainActivity.this); // here write the name of your activity in place of "YourActivity"
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.dialog);
            TextView tv = (TextView)dialog.findViewById(R.id.tv_pc_dialogTitle);
            Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/GOTHIC.TTF"); // here GOTHIC.TTF is the font-file I have pasted in the asset/fonts folder in my project 
            tv.setTypeface(tf); // Set the typeface like this

            Button bt = (Button) dialog.findViewById(R.id.bt_pc_goButton);
            bt.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    // Write here, what do you want to do on button click

                }
            });

            dialog.show();


        }
    });



    }
 }

这是您可以创建自定义对话框以及自定义该对话框组件的方法。在这里,我将自定义字体设置为Dialog的标题。