Android CheckBox文字不显示 [英] Android CheckBox text not displaying

查看:1867
本文介绍了Android CheckBox文字不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的一个Android活动中动态创建一些复选框,但它不会呈现文本。

I'm trying to dynamically create some CheckBoxes in one of my Android activities, but it's not rendering the text.

这是我的简化代码...

Here is my simplified code...


  1. 版式XML:

  1. Layout XML:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dip">

    ...
    <LinearLayout
        android:id="@+id/register_attracted_to"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />
    ...
</LinearLayout>


  • 活动代码:

  • Activity code:

    final LinearLayout attractedTo = (LinearLayout) findViewById(R.id.register_attracted_to);
    
    final CheckBox male = new CheckBox(this);
    male.setText("Male");
    attractedTo.addView(male);
    
    final CheckBox female = new CheckBox(this);
    female.setText("Female");
    attractedTo.addView(female);
    


  • 复杂(任何动态),这就是为什么我没有简单地包括在布局本身的复选框。但是,即使是我的代码仍然不能正常显示复选框文本。

    My "real" code is a little more complex (any dynamic) than this, which is why I haven't simply included the checkboxes in the layout itself. However, even dumbing down my code still doesn't render the checkbox text properly.

    这里是一个屏幕截图来演示(参见吸引到部分)额外表明我的垂直布局看起来工作正常,否则:

    Here's a screenshot to demonstrate (see the "Attracted To" section), with a little extra to demonstrate that my vertical layout appears to be working properly otherwise:

    推荐答案

    当然, ;)事实证明,由于我设置我的容器视图的背景颜色为白色,默认白色文本混合。解决方案是设置每个复选框的文本颜色。即:

    Of course I figure this out shortly after posting a bounty. ;) It turns out that since I was setting my container view's background color to white, the default white text was blending in. The solution was to set the text color of each checkbox. i.e.:

    final LinearLayout attractedTo = (LinearLayout) findViewById(R.id.register_attracted_to);
    
    final CheckBox male = new CheckBox(this);
    male.setText("Male");
    male.setTextColor(getResources().getColor(R.color.foreground_text));
    attractedTo.addView(male);
    
    final CheckBox female = new CheckBox(this);
    female.setText("Female");
    female.setTextColor(getResources().getColor(R.color.foreground_text));
    attractedTo.addView(female);
    

    这篇关于Android CheckBox文字不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆