【问题标题】:Android: Format Font in Alert DialogAndroid:在警报对话框中格式化字体
【发布时间】:2010-12-27 18:32:43
【问题描述】:

我有两个问题

1) 有谁知道如何将样式或格式应用于警报对话框。我目前使用Builder builder = new AlertDialog.Builder(this); 并使用setMessage() 方法来设置内容。

2) 另外我想知道如何更改由 linkify 创建的链接的颜色。我不想要默认的蓝色。

【问题讨论】:

    标签: android dialog android-alertdialog linkify


    【解决方案1】:

    第一季度。您必须充气或自定义并创建样式并应用于 AlertDialog

    以下是您如何扩展布局并将其应用到 AlertDialog

    LayoutInflater li = LayoutInflater.from(ctx);
    View view = li.inflate(R.layout.formatted_dialog, null);
    
    AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
    builder.setTitle("Formatted");
    builder.setView(view);
    

    定义您指定的布局所需的所有格式和样式。

    您可以使用膨胀视图访问布局中定义的特定文本视图,即

    LayoutInflater li = LayoutInflater.from(ctx);
    View view = li.inflate(R.layout.formatted_dialog, null);
    TextView label=(TextView)view.findViewById(R.id.i_am_from_formatted_layout_lable);
    

    第二季度。 android:textColorLink="#FF00FF" 可用于指定链接颜色。

    编辑:

    保存为 res/layout/link.xml 的示例布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
    
      <TextView
       android:id="@+id/text"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="http://www.google.com"
       android:autoLink="web"
       android:textColorLink="#FF00FF"
      />
    
    </LinearLayout>
    

    在您的 onCreate() 或您想调用 AlertDialog 的位置或时间

    LayoutInflater li = LayoutInflater.from(this);
    View view = li.inflate(R.layout.link, null);
    
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Formatted");
    builder.setView(view).create().show();
    TextView text=(TextView) findViewById(R.id.text);
    

    如果您从其他方法调用,请将 this 替换为上下文对象。

    【讨论】:

    • 我有一些疑问。 formatted_dialog 布局将如何?那么我应该如何添加我的内容?我应该在哪里指定android:textColorLink
    • 你能告诉我,如何在 AlertDialog 中使用字体
    • 在自定义警报对话框 setview 上使用 findviewid 并使用 setTypeface 方法
    【解决方案2】:

    您可以使用以下代码通过从默认的 alertDialog 中提取 TextView 来更改字体和文本颜色:

    TextView txtAlertMsg = (TextView)alert.findViewById(android.R.id.message);
    txtAlertMsg.setGravity(Gravity.CENTER);
    

    【讨论】:

    • 嗨,阿南德,我按照你的说法试过了,但我不知道我在哪里做错了。我在重力集得到 NullPointerException ......我的代码如下所示: AlertDialog.Builder builder=new AlertDialog.Builder(context); builder.setIcon(0); builder.setTitle("我的标题"); builder.setMessage(msg); builder.setNeutralButton("OK", null);警报对话框 alertDialog=builder.create(); alertDialog.show(); ((TextView)alertDialog.findViewById(android.R.id.title)).setGravity(Gravity.CENTER); ((TextView)alertDialog.findViewById(android.R.id.message)).setGravity(Gravity.CENTER);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多