2013-03-25 219 views
2

我想我通过扩展的LinearLayout做一个复合控件,这里是我的代码:如何在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

我尝试添加一个垂直的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

尝试使用RelativeLayout的,而不是线性 – 2013-03-25 05:50:24

+0

@No_Rulz在使用RelativeLayout的我必须使用RelativeLayout.LayoutParams和.addRule,这需要控件的ID我累.getId一个组件,但是没有用。我在XML中使用RelativeLayout,它是我的问题的完美解决方案:) – malhobayyeb 2013-03-25 06:32:43

+0

@No_Rulz没关系,明白了。我必须设置ID才能使用对象的getId。 – malhobayyeb 2013-03-25 06:42:45

回答

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

你是对的,但是在代码中我无法设置相对定位,因为如果我使用了LEFT_OF,我需要一个小部件的id。我累getId但没用! – malhobayyeb 2013-03-25 06:34:49

+0

所以你根本不使用xml? – 2013-03-25 06:41:10

+0

是的。但我明白了,我必须在每个元素使用getId之前使用setId。 – malhobayyeb 2013-03-25 06:41:49

2

不要把空间,因为它是不正确的方法。

使用android:layout_width

  1. 设置儿童 “0dp”
  2. android:layout_width设置父的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>