2つのビューを順番に追加したいのですが、私はこの方法を使用しましたが、エラーが発生しています。 これは私のXMLです。動的に2つのビューを1つ下に追加する
<?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の2つの相対レイアウトでは、動的に描画されるカスタムビューを追加します。
私のコード:
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は、私は2つの相対レイアウトで設定したい2つのビュータイプです。 ViewWidthとViewHeightは、実行中の画面の幅と高さです。
問題: mViewまたはmView2というビューが1つだけ追加されている場合は表示されますが、両方のビューが追加されている場合(上のような場合)、相対レイアウトは1つだけ表示されます。 私の意見はどちらも他のものよりも低く設定したいと思っています。
私の質問ではっきりしています。これを行う適切な方法を教えてください。
相対を使用しないでくださいここにレイアウト。 scrollviewをxmlのルートにし、カスタムビューを使用するときにpackagename.classnameを使用してxmlで参照できるようにします。スクロールビュー内の線形レイアウトを垂直方向に使用してください。 – L7ColWinters
解決策を見つけました。追加するのを忘れてしまいました。 rlstat2.setLayoutParams(para2);とにかく、ありがとう、私のコードで – droid