我在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);
}
}
我得到Null点异常。请告诉我该怎么做。
最好的问候
答案 0 :(得分:0)
嘿你正在使用findViewById获取在当前布局show_all.xml中没有的relativelayout实例。为什么你得到空指针异常。为那个特定的relativelayout创建单独的布局并将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);
}
}
答案 1 :(得分:0)
Hello Umar我不知道如何使用xml布局动态添加但是你可以使用Below Code来获取你需要的东西添加到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);
}