Android的复选框文字不显示 [英] Android CheckBox text not displaying

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

问题描述

我想要动态地创建在我的Andr​​oid的活动之一,一些复选框,但它并不是渲染文本。

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

下面是我的简化code ...

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>

  • 活动code:

  • 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);
    

  • 我的真正的code是一个有点比这更复杂的(任何动态),这就是为什么我没有简单地包含在布局本身的复选框。然而,即使是弱智化我的code仍然没有呈现复选框文字正确。

    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的复选框文字不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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