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"三个孩子:
    • 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 回答