更改alertdialog的标题尺寸在android系统?(Change title size of

2019-07-03 18:56发布

有关消息文本中不存在问题。 我不能改变(个性化)的颜色,字体,alertdialog的标题的风格?

有什么办法吗?

谢谢!

Answer 1:

关于字体大小,你可以这样做:

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


Answer 2:

看看这个链接: 创建对话框

这应该帮助。

还是这警报对话框生成器



Answer 3:

下面是一个完整的例子我怎么会做到这一点。 见的最后一行代码。

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


Answer 4:

还有另一种方式来设置一个新的自定义视图对话框标题。 我们可以定义每个自定义视图,如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()



Answer 5:

让我们以这个例子定制对话框的。 下面是用来使它的文件:

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


        }
    });



    }
 }

这是你可以做一个自定义对话框,并定制对话框的组件。 这里我设置自定义字体对话框的标题。



文章来源: Change title size of alertdialog in android?