2012-03-01 140 views
0

我曾在一個XML文件中相對佈局下面讓我們說add_relative_layout.xml添加自定義相對佈局到一個單排佈局

<?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="vertical" 
    android:id="@+id/addAccountLinearLayout"> 



</LinearLayout> 

以上是我想添加的代碼文件拷貝以下主要佈局。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/UIContainer" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

<RelativeLayout 

    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/white" > 

    <TextView 
     android:id="@+id/amountLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="5dp" 
     android:text="Amount" 
     android:textColor="@android:color/black" 
     android:textStyle="bold" /> 

    <EditText 
     android:id="@+id/amount" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="5dp" > 

    </EditText> 

我命名爲Show_all.xml另一個Android XML文件。這是一個線性佈局XML

我想,因爲我在這個SHOW_ALL佈局

目前想我使用這個代碼上方添加儘可能多的時候,這相對佈局

private void callOnCreate() 
     { 
      linear = (LinearLayout) findViewById(R.id.addAccountLinearLayout); // the layout in which i want to make dynamic copies of this layout. 
      layout = (RelativeLayout) findViewById(R.layout.ui_relative_layout_style); // name of xml File of above code. 

      for (int i=0; i < 4; i++) 
      { 
       Account account = accountArray.get(i); 
       linear.addView(layout, i); 
      } 
     } 

我得到空點例外。請告訴我應該怎麼做 。

問候

+0

,你得到空指針異常。請發佈logcat,以便它可以幫助我們回答您的問題。 – deepa 2012-03-01 04:23:41

+0

@umar將您的完整logcat和NullPointerexception提供給您。您的活動檢查哪一行提供空值 – 2012-03-01 04:29:19

回答

0

您好奧馬爾我不知道你如何使用xml配置動態添加,但你可以用下面的代碼來獲得您所需的東西添加到LinearLayout

public RelativeLayout createViewTOAdd(){ 
    lp=new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.FILL_PARENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT); 
    RelativeLayout mRelativeLayout=new RelativeLayout(this); 
    mRelativeLayout.setBackgroundColor(Color.WHITE); 
    TextView mTextView=new TextView(this); 
    RelativeLayout.LayoutParams Textview_lp=new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT); 
    mTextView.setText("Amout"); 
    Textview_lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
    Textview_lp.addRule(RelativeLayout.CENTER_VERTICAL); 
    Textview_lp.leftMargin=10; 
    mTextView.setTextColor(Color.BLACK); 
    mTextView.setTextAppearance(this, R.style.TextStyle); 
    //mTextView.setLayoutParams(Textview_lp); 
    EditText mEditText=new EditText(this); 
    RelativeLayout.LayoutParams EditText_param=new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT); 
    EditText_param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    EditText_param.addRule(RelativeLayout.CENTER_VERTICAL); 
    EditText_param.rightMargin=10; 
    //mEditText.setLayoutParams(EditText_param); 
    //mRelativeLayout.addView(mTextView, 0); 
    //mRelativeLayout.addView(mEditText, 1); 
    //mRelativeLayout.addView(mTextView); 
    //mRelativeLayout.addView(mEditText); 
    mRelativeLayout.addView(mTextView, Textview_lp); 
    mRelativeLayout.addView(mEditText, EditText_param); 
    return mRelativeLayout; 
} 

現在如何添加視圖到的LinearLayout低於

mLinearLayout=(LinearLayout)findViewById(R.id.mainLinearView); 

    mLinearLayout.removeAllViews(); 

    for(int i=0;i<4;i++){ 
     mLinearLayout.addView(createViewTOAdd(), i); 
    } 
+0

你知道如何使用drawable/abc.xml設置relativelayout的背景嗎? – 2012-03-01 08:05:52

+0

以及我添加列表視圖並製作了一個自定義適配器。但是,無論如何感謝您的幫助:) – 2012-03-02 06:25:07

0

嘿您使用findViewById獲得的RelativeLayout的情況下,爲什麼你得到空指針exception.So針對特定的RelativeLayout創建單獨的佈局,是不是在你的當前佈局show_all.xml .thats可用和名稱XML佈局文件UIContianer然後嘗試使用下面的代碼

private void callOnCreate() 
     { 
      linear = (LinearLayout) findViewById(R.id.addAccountLinearLayout); // the layout in which i want to make dynamic copies of this layout. 

LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); 
View vi = inflater.inflate(R.layout.ui_relative_layout_style, null); 
      for (int i=0; i < 4; i++) 
      { 
       Account account = accountArray.get(i); 
       linear.addView(vi, i); 
      } 
     } 
+0

檢查新代碼。錯誤代碼 – 2012-03-01 06:39:23

+0

請告訴您現在哪條線路出現錯誤,並且同時發佈了兩個佈局文件的xml代碼 – Maneesh 2012-03-01 06:44:43

+0

錯誤位於linear.addView(layout,i); – 2012-03-01 06:56:28