Fonts Android中的字体预览警报对话框

Fonts Android中的字体预览警报对话框,fonts,Fonts,我想在“警报”对话框中创建字体预览,但我不能这样做。我可以选择字体,但我不知道如何生成字体预览。如果有人能在这方面帮助我,我将不胜感激 AlertDialog.Builder builderSingle = new AlertDialog.Builder(MainActivity.this); builderSingle.setIcon(R.drawable.lg_logo); builderSingle.setTitle("Select any

我想在“警报”对话框中创建字体预览,但我不能这样做。我可以选择字体,但我不知道如何生成字体预览。如果有人能在这方面帮助我,我将不胜感激

 AlertDialog.Builder builderSingle = new AlertDialog.Builder(MainActivity.this);
            builderSingle.setIcon(R.drawable.lg_logo);
            builderSingle.setTitle("Select any font");

            final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.select_dialog_singlechoice);


            arrayAdapter.add("Font1");
            arrayAdapter.add("Font2");
            arrayAdapter.add("Font3");
AlertDialog.Builder builderSingle=新建AlertDialog.Builder(MainActivity.this);
builderSingle.setIcon(R.drawable.lg_标志);
setTitle(“选择任何字体”);
final ArrayAdapter ArrayAdapter=新的ArrayAdapter(MainActivity.this,android.R.layout.select\u dialog\u singlechoice);
arrayAdapter.添加(“Font1”);
arrayAdapter.添加(“Font2”);
arrayAdapter.添加(“Font3”);

通过这种方式,我可以创建并选择字体,而不是Font1和Font2。我希望显示在线搜索的字体预览,但没有帮助。

如果我没有错的话。您正试图实现以下图像所示的目标

这是你问题的答案。我用单选按钮从列表中进行单选

在这里,我使用的是自定义布局,显示所有带有预览文本视图的单选按钮,并使用setview()方法在alertdialog中充气或使用此布局

下面是此预览布局的xml布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RadioGroup
        android:layout_width="match_parent"
        android:id="@+id/radioGroup"
        android:layout_margin="12dp"
        android:layout_height="wrap_content" >

        <RadioButton
            style="@style/RadioButton"
            android:id="@+id/radioButton"
            android:layout_width="match_parent"
            android:fontFamily="@font/luckiestguyregular"
            android:layout_height="wrap_content"
            android:textSize="18dp"
            android:layout_weight="1"
            android:text="RadioButton" />

        <RadioButton
            style="@style/RadioButton"
            android:textSize="18dp" 
            android:id="@+id/radioButton2"
            android:layout_width="match_parent"
            android:fontFamily="@font/notomonoregular"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="RadioButton" />

        <RadioButton
            style="@style/RadioButton"
            android:textSize="18dp"
            android:id="@+id/radioButton3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/merriweatherblack"
            android:layout_weight="1"
            android:text="RadioButton" />
    </RadioGroup>
</LinearLayout>
为了知道选择了哪个单选按钮,我使用下面的代码

首先使用从custompreviewfont布局中查找无线电组

RadioGroup group=view1.findviewbyd(R.id.RadioGroup)

然后使用名为radioGroup.getCheckedRadioButtonId()的radioGroup视图方法获取当前选中的单选按钮id

最后,我使用if-else-if语句执行字体应用操作

if  (group.getCheckedRadioButtonId() == R.id.radioButton) {
       //code of first radiobutton
}
else if(group.getCheckedRadioButtonId() == R.id.radioButton2){
    //code of second radiobutton
}
等等


希望这对您有所帮助。

谢谢您的帮助。
if  (group.getCheckedRadioButtonId() == R.id.radioButton) {
       //code of first radiobutton
}
else if(group.getCheckedRadioButtonId() == R.id.radioButton2){
    //code of second radiobutton
}