0

メイン ビューに (ループから) 何度か含めたい XML RelativeLayout スニペットがあります。問題は次のように思われます - RelativeLayout スニペットを含めるたびに要素に異なる ID が必要になるため、RatingBar の親のハードコーディングを回避する方法はありますか?

私が知る限り、推奨される方法は、レイアウト スニペットを取得し、各要素の android:id をオーバーライドして一意にし、相対位置を持つ各要素の android:layout_below を手動でオーバーライドすることです。これは少しぎこちないようです -- これらのバインディングを自動的に行う方法はありますか?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp" 
    android:id="@+id/relativeView">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:text="Label"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView1" />

</RelativeLayout>
4

1 に答える 1

1

RelativeLayout などのIDを変更するだけです

int BASEID=200;

View v = mLayoutInflator.inflate(R.layout.myRelativeLayout, null);

for (int i;i<10;i++){
    v.findViewById(R.id.relativeView).setId(i+BASEID);
}

mRootView.addView(v,...);

RatingBar次に、追加した 4 番目の forを取得する必要がある場合は、RelativeLayout呼び出すことができます

RatingBar mRatingBar = (RatingBar)mRootView.findViewById(BASEID+3).findViewById(R.id.ratingBar1);
于 2013-03-20T05:44:57.703 に答える