Android:警报对话框中的格式字体

时间:2010-12-27 18:32:43

标签: android dialog alertdialog linkify

我有两个问题

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

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

2 个答案:

答案 0 :(得分:12)

Q1。您必须膨胀或自定义并创建样式并应用于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);

Q2。 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替换为上下文对象。

答案 1 :(得分:3)

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

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