【发布时间】:2011-12-03 02:52:10
【问题描述】:
我正在尝试在我的一个 Android 活动中动态创建一些 CheckBox,但它没有呈现文本。
这是我的简化代码...
-
布局 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> -
活动代码:
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);
我的“真实”代码比这更复杂(任何动态),这就是为什么我没有简单地将复选框包含在布局本身中的原因。但是,即使简化我的代码仍然无法正确呈现复选框文本。
这是一个演示屏幕截图(请参阅“吸引到”部分),还有一些额外的内容可以证明我的垂直布局似乎可以正常工作:
【问题讨论】:
-
我不会动态添加 UI 组件,因为在开发时审查它们有困难。开发人员需要运行整个应用程序才能看到添加的组件。我谦虚地建议在可见性消失的情况下添加它们。它非常轻量级,因为它们从不被绘制,但仍然可以找到(findViewById)。
-
复选框是通过 HttpRequest 生成的,这意味着您的建议是不可能的。
标签: android android-layout checkbox