Android CheckBox文字不显示 [英] Android CheckBox text not displaying
问题描述
我试图在我的一个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...
-
版式XML:
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屋!