我的代码编译得很好,当我启动Activity时,会显示其关联的布局,但是 我之后在onCreate()中动态创建的小部件不显示。
为什么不呢?
Eclipse不会进入Debug Perspective,它们根本就不显示。
以下是相关代码:
public class Authorize_Activity_DynamicControls extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ondemandandautomatic_dynamicauthorize);
LinearLayout llay = new LinearLayout(this);
llay.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams llp = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
llp.weight = 1.0f;
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText("1");
cb.setLayoutParams(llp);
llay.addView(cb);
ScrollView svh = (ScrollView) findViewById(R.id.scrollViewHost);
svh.addView(llay);
}
...这里是布局xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dip"
android:text="@string/demand"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dip"
android:text="@string/time"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dip"
android:text="@string/space"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dip"
android:text="@string/contact"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ScrollView
android:id="@+id/scrollViewHost"
android:fillViewport="true"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</ScrollView>
</LinearLayout>
答案 0 :(得分:1)
您可能需要手动使整个视图无效以获得所需的结果,因此请尝试将此行添加到结尾:
svh.invalidate();
答案 1 :(得分:1)
您不需要使您在onCreate()中添加的视图无效。没有XML布局文件,尝试自信地回答帖子也是一些猜测。您是否尝试使用TextView而不是复选框?