我进行了很多搜索,但没有找到解决我问题的方法。当我创建多个视图并尝试将它们添加到 LinearLayout 时,仅显示第一个视图(蛋糕)。
这是我创建和添加视图的地方。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.image_View);
PlayAreaView cake = new PlayAreaView(SecondTestActivity.this, R.drawable.cake);
views.add(cake);
PlayAreaView bomb = new PlayAreaView(SecondTestActivity.this, R.drawable.bomb);
views.add(bomb);
PlayAreaView crown = new PlayAreaView(SecondTestActivity.this, R.drawable.crown);
views.add(crown);
PlayAreaView scissors = new PlayAreaView(SecondTestActivity.this, R.drawable.cut);
views.add(scissors);
PlayAreaView trash = new PlayAreaView(SecondTestActivity.this, R.drawable.bin_closed);
views.add(trash);
PlayAreaView key = new PlayAreaView(SecondTestActivity.this, R.drawable.bullet_key);
views.add(key);
LayoutParams params
= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for(View v : views){
Log.v("created", "view created");
v.setLayoutParams(params);
linearLayout.addView(v);
}
}
这是我的 main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_View"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/image_View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</FrameLayout>
我可以创建一个视图并且没问题,但我无法在 LinearLayout 中添加多个视图。为什么是这样?