简体   繁体   English

调用 setTextSize 后 RadioButton 的文本不显示

[英]RadioButton's Text not displaying after setTextSize call

I have programmatically added several RadioButton s to a pre-existing RadioGroup .我以编程方式将几个RadioButton添加到预先存在的RadioGroup中。 The buttons themselves are there and are clickable, but their texts just won't appear.按钮本身在那里并且可以点击,但它们的文本不会出现。 I have set their texts using the setText method, and by using Toast and RadioButton 's getText method, I was able to confirm that all of the buttons do possess the texts they should have - they just don't display them.我已经使用setText方法设置了它们的文本,并且通过使用ToastRadioButtongetText方法,我能够确认所有按钮确实拥有它们应该拥有的文本——它们只是不显示它们。

I have made sure to set their alpha to 1, the text size to 20sp, their visibility to View.Visible , and I tried changing the text color and the background color several times to contrasting colors, but the text still didn't appear.我确保将它们的 alpha 设置为 1,将文本大小设置为 20sp,将它们的可见性设置为View.Visible ,并且我尝试多次更改文本颜色和背景颜色以形成对比 colors,但文本仍然没有出现。

Can someone please help me figure out how to make the text appear?有人可以帮我弄清楚如何让文字出现吗?


The Java code: Java代码:

        for(int i = 0; i < CERTAIN_CONSTANT_VALUE; i++)
        {
            RadioButton r = new RadioButton(this);
            android.widget.LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            r.setLayoutParams(params);
            r.setId(View.generateViewId());
            r.setText(answers[i]);
            r.setOnClickListener(this::onBtnClicked);
            r.setTextSize(20, TypedValue.COMPLEX_UNIT_SP);
            r.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
            r.setTextColor(Color.BLACK);
            r.setAlpha(1);
            r.setVisibility(View.VISIBLE);
            r.setBackgroundColor(Color.GREEN);
            this.radioGroup.addView(r);
        }

The RadioGroup in the XML file: XML文件中的RadioGroup

<RadioGroup
    android:id="@+id/radioGroup"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="200dp">
</RadioGroup>

Minimal, Reproducible Example最小的、可重现的例子

Java: Java:

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RadioGroup radioGroup = findViewById(R.id.radioGroup);
        String[] answers = {"first", "second", "third", "fourth"};
    
        for (int i = 0; i < 4; i++)
        {
            RadioButton r = new RadioButton(this);
            android.widget.LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            r.setLayoutParams(params);
            r.setId(View.generateViewId());
            r.setText(answers[i]);
            r.setOnClickListener(this::onBtnClicked);
            r.setTextSize(20, TypedValue.COMPLEX_UNIT_SP);
            r.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
            r.setTextColor(Color.BLACK);
            r.setAlpha(1);
            r.setVisibility(View.VISIBLE);
            r.setBackgroundColor(Color.GREEN);
            radioGroup.addView(r);
        }
    }

    public void onBtnClicked(View view){}
}

XML: XML:

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

    <RadioGroup
        android:id="@+id/radioGroup"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </RadioGroup>
</LinearLayout>

just change setTextSize to this:只需将 setTextSize 更改为:

r.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM