2

LinearLayoutを拡張して複合コントロールを作成したいのですが、コードは次のとおりです。

public class MemberView extends LinearLayout {
    private TextView contactName;
    private ImageView removeContact;
    private ImageView contactPicture;

    public MemberView(Context context) {
    super(context);
    //Context thisContext = getContext();
    onCreate(context);


    }

    private void onCreate(Context context) {
    contactName = new TextView(context);
    contactName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    contactName.setText("Mohammad");

    removeContact = new ImageView(context);
    removeContact.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    removeContact.setImageResource(R.drawable.ic_menu_delete);

    contactPicture = new ImageView(context);
    contactPicture.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    contactPicture.setImageResource(R.drawable.ic_contact_picture);

    setClickable(true);
    setOrientation(LinearLayout.HORIZONTAL);
    addView(contactName);
    addView(removeContact);
    addView(contactPicture);
    }
}

次のUIプロトタイプのように見せたいです。

ここに画像の説明を入力してください

しかし、私のコードの結果は次のとおりです。

ここに画像の説明を入力してください

VERTICAL LinearLayoutを追加してスペースを追加しようとしましたが、使用できません。編集したonCreateコードは次のとおりです。

private void onCreate(Context context) {
    contactName = new TextView(context);
    contactName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    contactName.setText("Mohammad");

    removeContact = new ImageView(context);
    removeContact.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    removeContact.setImageResource(R.drawable.ic_menu_delete);

    contactPicture = new ImageView(context);
    contactPicture.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    contactPicture.setImageResource(R.drawable.ic_contact_picture);

    LinearLayout layout2 = new LinearLayout(context);
    layout2.setOrientation(LinearLayout.VERTICAL);
    layout2.addView(new Space(context));
    layout2.addView(contactName);
    layout2.addView(new Space(context));


    setClickable(true);
    setOrientation(LinearLayout.HORIZONTAL);
    addView(layout2);
    addView(removeContact);
    addView(contactPicture);
    }
4

2 に答える 2

2

適切な方法ではないため、スペースを入れないでください。

android:layout_widthを使用します。

  1. android:layout_width子のを「0dp」に設定します
  2. android:weightSum親のを設定します
  3. android:layout_weight各子のを比例的に設定します。つまり
    weightSum="5"、3人の子を使用します。
    • layout_weight="1"
    • layout_weight="3"
    • layout_weight="1"

たとえば(これは静的であり、動的に試す必要があります):

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="5">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="1" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="2" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="3" />

</LinearLayout>
于 2013-03-25T05:41:27.963 に答える
0

RelativeLayoutは、ここでの目的に最適です。構造は次のようになります。

<RelativeLayout>

    // image layout, aligned to the right.
    <RelativeLayout horizontal alignParentRight >
    <ImageView contact icon>
    <ImageView deleteIcon alignBottom right of contactIcon>
    </RelativeLayout>

    // contact name
    <RelativeLayout fillparent leftof abovelayout>
    <Textview centerparent true  this will display contact name />
    </Relativelayout>

</RelativeLayout>
于 2013-03-25T05:56:10.287 に答える