2011-06-16 64 views
0

我有自定義編碼的佈局來容納所有的孩子。下面是代碼:對齊自定義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中間,我的按鈕放在最右邊。考慮到這種層次觀點,我該怎麼做?

感謝

+1

我不明白爲什麼您沒有XML文件中的佈局?沒有什麼是你正在做的,你現在無法用XML做。 – dymmeh 2011-06-16 15:23:21

+0

我在選擇中創建了我的佈局和內部視圖,其中有很多,我需要在運行時設置所有內容。此外,我還要以分層方式設置這些佈局。我認爲這會給我帶來很大的安慰,編碼 – ikbal 2011-06-16 15:30:38

回答

1

@dymmeh是完全正確的,你正在做的可能是你的main.xml如下每一件事情:

@Steve我又增加了TextView的建議,你會想選擇要麼TextViewOR

<?xml version="1.0" encoding="utf-8" ?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal"> 

    <TextView 
     android:drawableLeft="@drawable/star" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="plain view text" 
     android:weight="1" 
     /> 

    <<--OR-->> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:drawableLeft="@drawable/star" 
     android:weight="1" 
     /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="plain view text" 
     android:weight="1" 
     /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="accessor" 
     android:weight="1" 
     />  
</LinearLayout> 

再經過ORTextView & ImageView之前您的java課程只需在您的onCreate之後撥打setContentView(R.layout.main);super.onCreate();

比您擁有的要容易得多。您可以以任何您想要的方式輕鬆安排任何這些物品。請參閱LinearLayout您也可以使用TableLayout但我認爲這些對於大多數情況來說太複雜了。

+0

對不起,我沒有看到你的評論,你仍然可以做的一切都從XML開始,它會讓事情變得更容易,你到底想要做什麼? – 2011-06-16 15:40:59

+0

我的線性佈局將是一個行的容器,它包含的內容將在運行時完全確定。我沒有看到任何xml的好處。我無法解釋我需要什麼,但我可以告訴你,我需要寫在代碼中,謝謝 – ikbal 2011-06-17 06:30:29

+0

好吧,我會採取你的話,希望我可以幫助更多,但最後一件事,但如果唯一改變在運行時是內容而不是格式或佈局,這仍然可以幫助您以編程方式完成所有這些工作,正如開發人員教程[ListView]中所做的那樣(http://developer.android.com/resources/tutorials/ views/hello-listview.html)以獲取每個列表項的格式。 – 2011-06-17 12:53:24

0

使用XML佈局更容易。我會建議使用它。但是,如果您確實需要在代碼中執行此操作,則可以將中間TextView設置爲layout_weight = 1的佈局參數。