我想逐個添加兩個視圖,我用這種方式,但我得到一個錯誤。 這是我的XML。動態添加兩個視圖一個在下面其他
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/rel1"
android:layout_alignParentTop="true"
></RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/rel2"
android:layout_below="@+id/rel1"
></RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
在兩個相對佈局rel1和rel2中,我將添加我將要動態繪製的自定義視圖。
我的代碼:
setContentView(R.layout.main);
RelativeLayout rlstat1=(RelativeLayout)findViewById(R.id.rel1);
RelativeLayout rlstat2=(RelativeLayout)findViewById(R.id.rel2);
RelativeLayout.LayoutParams para1 = new RelativeLayout.LayoutParams(
viewWidth, viewHeight);
RelativeLayout.LayoutParams para2 = new RelativeLayout.LayoutParams(
viewWidth, viewHeight);
rlstat1.setLayoutParams(para1);
rlstat1.addView(mView);
para2.addRule(RelativeLayout.BELOW, R.id.rel1);
rlstat2.addView(mView2);
這裏MVIEW和mView2是,我想在這兩個相對的佈局設置兩個視圖類型。 ViewWidth和ViewHeight是它正在運行的屏幕的寬度和高度。
問題: 如果只添加一個視圖,即顯示mView或mView2,但是如果兩個視圖都被添加(如上所述),則只顯示一個相對佈局。 我希望我的兩個視圖都可以設置爲一個。
希望我在我的問題中很清楚,可否請告訴我適當的方式來做到這一點。
請不要使用相對佈局在這裏。使scrollview成爲xml的根,並且在使用自定義視圖時,可以使用packagename.classname在xml中引用它們。使用滾動視圖內的線性佈局,以垂直方向... – L7ColWinters 2012-02-24 05:53:26
得到了解決方案。這是我的錯,只是忘了添加 rlstat2.setLayoutParams(para2); 在我的代碼中,無論如何非常感謝。 – droid 2012-02-24 06:10:40