我有自定義編碼的佈局來容納所有的孩子。下面是代碼:對齊自定義LinearLayout中的對象?
public abstract class ItemView extends LinearLayout{
private ImageView icon;//cell icon
public static final int iconID=12345;
private Button accessorButton;//this will not be button ?
public static final int accessorID=54321;
public ItemView(Context context) {
super(context);
}
public void arrange(){
}
public void setView(Context context){
int l=super.getLeft();
int r=super.getRight();
int t=super.getTop();
int b=super.getBottom();
icon=new ImageView(context);
icon.setImageResource(R.drawable.star);
addView(icon);
accessorButton=new Button(context);
accessorButton.setText("accessor");
addView(accessorButton);
// icon.layout(l+5, t+5, l+100, b-5);
//accessorButton.layout(r-100, t+5, r-10, b-5);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
super.onLayout(changed, l, t, r, b);
}
}
,我有孩子的佈局,其中之一是:
public class ItemPlainView extends ItemView{
private TextView text;
public ItemPlainView(Context context) {
super(context);
setView(context);
}
public void setView(Context context){
super.setView(context);
text=new TextView(context);
text.setText("plain view text");
addView(text);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
super.onLayout(changed, l, t, r, b);
}
public TextView getText() {
return text;
}
public void setText(TextView text) {
this.text = text;
}
@Override
public void arrange() {
// TODO Auto-generated method stub
}
}
當我運行它自然給了我一個星形圖標,按鈕,之後一個TextView代碼。我需要替換和調整我的對象。在這個例子中,我需要將星形圖標放在最左邊的TextView中間,我的按鈕放在最右邊。考慮到這種層次觀點,我該怎麼做?
感謝
我不明白爲什麼您沒有XML文件中的佈局?沒有什麼是你正在做的,你現在無法用XML做。 – dymmeh 2011-06-16 15:23:21
我在選擇中創建了我的佈局和內部視圖,其中有很多,我需要在運行時設置所有內容。此外,我還要以分層方式設置這些佈局。我認爲這會給我帶來很大的安慰,編碼 – ikbal 2011-06-16 15:30:38