如何在LinearLayout中添加空格?

时间:2013-03-25 05:36:16

标签: android android-layout

我想通过扩展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原型

enter image description here

但我的代码的结果是:

enter image description here

我尝试添加一个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);
    }

2 个答案:

答案 0 :(得分: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"
  4. 例如(这是静态的,你必须尝试动态):

    <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>
    

答案 1 :(得分: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>