1

私はAndroidが初めてで、プログラムでRelativeLayoutsを作成しようとしています(少なくとも、値が動的な場合に機能する唯一の方法だと思います)。基本的に、値のリストを返す Web サービスを構築し、それらの値を表示したいと考えています。1 つの値の場合もあれば、500 の場合もあります。

私のlayout.xmlでは、必要なものに比較的近いRelativeLayoutを構築できましたが、一度しか入力できませんでした。以下は私のXMLです:

注: これは XML 全体ですが、問題のノードは RelativeLayout です。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:orientation="vertical" 
    android:id="@+id/linear">

    <Spinner
        android:id="@+id/band"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000000"
        android:dropDownSelector="#000000"
        android:textColor="#F0F0F0" />

    <Spinner
        android:id="@+id/yearSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#F0F0F0" />

      <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:padding="6dip"
        android:id="@+id/showRelativeLayout">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="6dip"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/secondLine"
            android:layout_width="fill_parent"
            android:layout_height="26dip"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_toRightOf="@id/icon"
            android:ellipsize="marquee"
            android:singleLine="true"
            android:text="Simple application that shows how to use RelativeLayout" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/secondLine"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_alignWithParentIfMissing="true"
            android:layout_toRightOf="@id/icon"
            android:gravity="center_vertical"
            android:text="My Application"
            android:id="@+id/thirdLine" />
    </RelativeLayout>


</LinearLayout>

そこで、Web サービスの結果を繰り返し処理できるように、Activity で同じものを作成しようとしました。以下は、(返される json 配列ごとに) 何度も呼び出されるメソッドです。残念ながら、relativeLayout は 1 つしかなく、画面の残りの部分 (1 行で約 90%) を占有しようとします。私は間違いなく基本的なことを理解していません。

private void processRelativeLayout(int count) {
    RelativeLayout rl = new RelativeLayout(this);
    RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    rl.setLayoutParams(lay);

    //Set the Image
    //TODO:  pull from webservice to get the artist image
    RelativeLayout.LayoutParams imlay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    imlay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    imlay.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    imlay.setMargins(0, 0, 6, 0);

    ImageView iv = new ImageView(this);
    iv.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
    iv.setId(1+count);
    iv.setLayoutParams(imlay);
    rl.addView(iv);

    RelativeLayout.LayoutParams tv1lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 26);
    tv1lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    tv1lay.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    tv1lay.addRule(RelativeLayout.RIGHT_OF, 1+count);
    TextView tv = new TextView(this);
    tv.setSingleLine();
    tv.setEllipsize(TruncateAt.MARQUEE);
    tv.setTextColor(Color.YELLOW);
    tv.setText("Simple application that shows how to use RelativeLayout");
    tv.setId(2+count);
    tv.setLayoutParams(tv1lay);
    rl.addView(tv);

    RelativeLayout.LayoutParams tv2lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    tv2lay.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    tv2lay.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    tv2lay.addRule(RelativeLayout.ABOVE, 2+count);
    tv2lay.addRule(RelativeLayout.RIGHT_OF,1+count);
    TextView tv2 = new TextView(this);
    tv2.setTextColor(Color.YELLOW);
    tv2.setGravity(Gravity.CENTER_VERTICAL);
    tv2.setText("My Application");
    tv2.setId(count+3);
    tv2.setLayoutParams(tv2lay);
    rl.addView(tv2);

    rootLinear.addView(rl);


    //add the relative layoutll.addView(tv, lay);
}

出力を示すために、エミュレーターのスクリーンショットも添付しています。結果が 5 回表示されることを期待していました (processRelativeLayout を 5 回呼び出したため)。

長い投稿で申し訳ありませんが、十分な情報を提供したかったのです。

更新: 画像を投稿しようとしましたが、サイトは私が十分に味付けされていないと言っています:)

4

1 に答える 1

1

推測では、は、下部に整列する子要素があるため、指定しようとしRelativeLayoutた高さ設定を処理できません。WRAP_CONTENTこのため、代わりに親の高さに合わせることにフォールバックしているため、画面のほぼ全体を占めるのはなぜですか。レイアウトパラメータで明示的な高さを指定するか、使用rlを避けるために子を再構築してみてくださいALIGN_PARENT_BOTTOM-私が見る限り、上にスナップするように設定するのは簡単なはずimlayです(独自のデフォルトの高さを使用するか、 own)、およびIDが。のルールをtv1lay使用しています。ALIGN_BOTTOMiv

于 2012-11-12T04:47:11.737 に答える