2012-03-01 143 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