Android:AlertDialog中的固定宽度字体

14

我要在AlertDialog中显示一串字符串,如下图所示:

enter image description here

1.如何设置定宽字体,就像下面的那个

enter image description here

2.AlertDialog默认启用垂直滚动。如何启用水平滚动,以便最后一行保持在上一行中?

2个回答

18

将AlertDialog中的视图放大并在该视图中设置android:typeface="monospace"。 实现这一点需要:

enter image description here

使用此布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/dlgView"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <LinearLayout android:id="@+id/dlgLayout"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp">
    <TextView android:id="@+id/dlgText"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:textColor="#FFF"
      android:typeface="monospace"/>
  </LinearLayout>
</ScrollView>

这段主活动的代码构建了一个AlertDialog(将一个按钮放到默认布局中)。

public class MonospacedAlertActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //attach an instance of HandleClick to the Button
        findViewById(R.id.button1).setOnClickListener(new HandleClick());
    }
    private class HandleClick implements OnClickListener{
        public void onClick(View arg0) {
            ShowAlert(MonospacedAlertActivity.this);
        }

        private void ShowAlert(Activity callingActivity) {
            //Generate views to pass to AlertDialog.Builder and to set the text
            View dlg;
            TextView tvText;
            try {
              //Inflate the custom view
              LayoutInflater inflater = callingActivity.getLayoutInflater();
              dlg = inflater.inflate(R.layout.alertmono, (ViewGroup) callingActivity.findViewById(R.id.dlgView));
              tvText = (TextView) dlg.findViewById(R.id.dlgText);
            } catch(InflateException e) {
              //Inflater can throw exception, unlikely but default to TextView if it occurs
              dlg = tvText = new TextView(callingActivity);
            }
            //Set the text
            tvText.setText( "22-05-2012 20:51:13 114 58 00:04:19\n"+
                            "22-05-2012 20:59:15  84 52 00:01:25\n"+
                            "22-05-2012 22:49:48  96 51 00:01:32\n"+
                            "23-05-2012 00:08:52  79 58 00:01:26");
            //Build and show the dialog
            new AlertDialog.Builder(callingActivity)
              .setTitle(callingActivity.getString(R.string.app_name))
              .setCancelable(true)
              .setIcon(R.drawable.ic_launcher)
              .setPositiveButton("OK", null)
              .setView(dlg)
              .show();    //Builder method returns allow for method chaining
        }
    }
}

alertmono和/或R.layout是什么? - DSlomer64
R.layout 是 Android 引用已编译布局的方式,alertmono 是保存的对话框布局文件的名称。 - Daniel S. Fowler

0

我知道这已经很老了,但是...我刚刚发现了这个,并且认为其他人可能会感兴趣。从API 19开始,在Typeface.java中定义了几种不同的字体类型:其中之一是MONOSPACE。我的Kotlin警报对话框如下:

private fun showDialog(mess:String) : Boolean {
    val tv = TextView(requireActivity())
    tv.typeface = Typeface.MONOSPACE
    tv.text = mess
    AlertDialog.Builder(this.requireContext())
        .setTitle("Changes in deck")
        .setView(tv)
        .show()
    return true
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接