我需要动态创建代表这个xml代码的LinearLayout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ddd"
android:orientation="vertical"
android:padding="1dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="horizontal"
android:paddingBottom="26dp"
android:paddingTop="26dp" >
<LinearLayout
android:layout_width="160dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff" >
<ImageView
android:id="@+id/ImageView04"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="top"
android:layout_marginLeft="32dp"
android:layout_marginTop="4dp"
android:background="#eee" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:gravity="top"
android:paddingBottom="4dp"
android:paddingLeft="16dp"
android:text="Name of event"
android:textSize="22sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="16dp"
android:text="Group of this event"
android:textColor="#aaa"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
但问题是我写了一些代码,但我不知道为什么在调试我的应用程序后,我看到的唯一一件事就是事件的名称。
以下是代码:
public class Event extends LinearLayout{
private LinearLayout globalContener;
private LinearLayout contener;
private LinearLayout avatarBox;
private LinearLayout contentBox;
private TextView eventName;
private TextView eventGroup;
private ImageView avatar;
private LinearLayout.LayoutParams globalContenerParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
private LinearLayout.LayoutParams contenerParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
private LinearLayout.LayoutParams avatarBoxParams = new LinearLayout.LayoutParams(160, LayoutParams.MATCH_PARENT, 1.0f);
private LinearLayout.LayoutParams contentBoxParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
private LinearLayout.LayoutParams eventNameParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
private LinearLayout.LayoutParams eventGroupParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
private LinearLayout.LayoutParams avatarParams = new LinearLayout.LayoutParams(48, 48);
public Event(Context context){
super(context);
// Design of global contener
globalContenerParams.setMargins(0, 0, 0, 32);
globalContener = new LinearLayout(context);
globalContener.setBackgroundColor(0xDDDDDD);
globalContener.setPadding(1, 1, 1, 1);
globalContener.setOrientation(LinearLayout.VERTICAL);
globalContener.setLayoutParams(globalContenerParams);
// Design of contener
contener = new LinearLayout(context);
contener.setBackgroundColor(0xFFFFFF);
contener.setPadding(0, 26, 0, 26);
contener.setOrientation(LinearLayout.HORIZONTAL);
contener.setLayoutParams(contenerParams);
// Design of avatar box
avatarBox = new LinearLayout(context);
avatarBox.setBackgroundColor(0xFFFFFF);
avatarBox.setLayoutParams(avatarBoxParams);
// Design of contet box
contentBox = new LinearLayout(context);
contentBox.setOrientation(LinearLayout.VERTICAL);
contentBox.setBackgroundColor(0xFFFFFF);
contentBox.setLayoutParams(contentBoxParams);
// Design of avatar
avatarParams.gravity = Gravity.TOP;
avatarParams.setMargins(32, 4, 0, 0);
avatar = new ImageView(context);
avatar.setBackgroundDrawable(Const.avatarImg);
avatar.setLayoutParams(avatarParams);
// Design of event name
eventNameParams.gravity = Gravity.TOP;
eventName = new TextView(context);
eventName.setGravity(Gravity.TOP);
eventName.setPadding(16, 0, 0, 4);
eventName.setTextSize(22);
eventName.setText("Sprawdzian z języka polskiego");
eventName.setLayoutParams(eventNameParams);
// Design of event group
eventGroup = new TextView(context);
eventGroup.setGravity(Gravity.CENTER);
eventGroup.setPadding(16, 0, 0, 0);
eventGroup.setTextSize(16);
eventGroup.setTextColor(0xAAAAAA);
eventGroup.setText("4ia Technikum Łączności w Krakowie");
eventGroup.setLayoutParams(eventGroupParams);
// Packing boxes
avatarBox.addView(avatar);
contentBox.addView(eventGroup);
contentBox.addView(eventName);
contener.addView(avatarBox);
contener.addView(contentBox);
globalContener.addView(contener);
// Adding to view
addView(globalContener);
}
}
有谁在这里可以帮助我吗?
答案 0 :(得分:0)
将主要LinearLayout方向设置为垂直
然后
将contentBoxParams权重设置为1f
private LinearLayout.LayoutParams contentBoxParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f);
然后 把这个
contentBox.addView(eventName);
之前
contentBox.addView(eventGroup);
然后替换所有
0xFFFFFF
与0xFFFFFFFF