Android CheckBox text not displaying

后端 未结 4 1384
悲哀的现实
悲哀的现实 2021-01-11 20:50

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. L

相关标签:
4条回答
  • 2021-01-11 21:34

    I just did the same and found that I was using setText("") in the initialisation code rather than setChecked(false). Dur!

    0 讨论(0)
  • 2021-01-11 21:42

    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);
    
    0 讨论(0)
  • 2021-01-11 21:51

    you are not setting the Layout parameters, Layout parameter says how the control will be shown

    final CheckBox female = new CheckBox(this);
    female.setText("Female");
    female .setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
    attractedTo.addView(female);
    
    0 讨论(0)
  • 2021-01-11 21:54

    Maybe it's due to the simplification of your real code, but did you set width and height of your checkbox ?

    0 讨论(0)
提交回复
热议问题