我希望通过扩展 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);
}