English 中文(简体)
Android: AwardDialog 中的固定宽度字体
原标题:Android: Fixed width font in AlertDialog

我在AwardDialog中显示一个字符串列表如下:

""https://i.sstatic.net/aXa5h.png" alt="此处输入图像描述"/ >

1. 如何设置固定宽度字体, 如以下所示的字体

""https://i.sstatic.net/vIcv4.png" alt="此处输入图像描述"/ >

2. AwardDialog 具有默认启用的垂直滚动功能。 如何启用水平滚动, 使最后一行保留在前一行?

最佳回答

放大您自己对AwardDialog 和设置和机器人: 类型=“ monospace” 的观点。 为了实现此目的 :

""https://i.sstatic.net/upD6x.png" alt="此处输入图像描述"/ >

使用此布局 :

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

此主活动的代码构建一个 AwardDialog( 将按钮放入默认布局) 。

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
"+
                            "22-05-2012 20:59:15  84 52 00:01:25
"+
                            "22-05-2012 22:49:48  96 51 00:01:32
"+
                            "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
        }
    }
}
问题回答

我知道这是很久以前的事了,但是... 我刚刚发现了这个,认为别人可能会感兴趣。早在19岁时,Tydimace.java就定义了几种不同的字体:其中之一是MONO SPACE。我在科特林的警告对话是:

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
}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签