Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将自定义相对布局添加到单个线性布局_Java_Android_Eclipse_Android Layout - Fatal编程技术网

Java 将自定义相对布局添加到单个线性布局

Java 将自定义相对布局添加到单个线性布局,java,android,eclipse,android-layout,Java,Android,Eclipse,Android Layout,我在一个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"

我在一个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>

我还有另一个android xml文件名为Show_all.xml。它是一个线性布局xml

我想在这个show\u 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);
          }   
      }
private void callOnCreate()
{
线性=(LinearLayout)findViewById(R.id.addAccountLinearLayout);//要在其中创建此布局的动态副本的布局。
layout=(RelativeLayout)findViewById(R.layout.ui_relative_layout_style);//上述代码的xml文件名。
对于(int i=0;i<4;i++)
{
Account=accountArray.get(i);
线性。添加视图(布局,i);
}   
}
我得到空点异常。请告诉我该怎么办


致以最诚挚的问候

嘿,您正在使用findViewById获取relativelayout的实例,该实例在当前布局show_all.xml中不可用。这就是您获取空指针异常的原因。因此,请为该特定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);
          }   
      }
private void callOnCreate()
{
线性=(LinearLayout)findViewById(R.id.addAccountLinearLayout);//要在其中创建此布局的动态副本的布局。
LayoutFlater充气机=(LayoutFlater)getSystemService(布局充气机服务);
视图vi=充气机。充气(R.layout.ui\u relative\u layout\u样式,空);
对于(int i=0;i<4;i++)
{
Account=accountArray.get(i);
线性。addView(vi,i);
}   
}

您好Umar我不知道如何使用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);
    }
mLinearLayout=(LinearLayout)findViewById(R.id.mainLinearView);
mLinearLayout.removeallview();

对于(int i=0;i您在哪里得到空指针异常。请发布日志,以便帮助我们回答您的问题。@umar放置完整的日志猫和空指针异常给您提供活动的行号检查哪一行给出空请告诉现在哪一行出现错误,并发布两个布局文件的xml代码error位于linear.addView(layout,i);activity.LAYOUT_INFLATER_SERVICE--未识别活动。我该怎么办?我不需要只执行一次。这就像我有帐户列表,我需要复制此布局并在每个布局中显示帐户信息,所有这些布局都在我的主帐户中膨胀。抱歉,删除活动只需使用LAYOUT_INFLATER_SERVICECEDo您知道如何使用drawable/abc.xml设置relativelayout的背景吗?我添加了列表视图并制作了一个自定义适配器。不过还是要感谢您的帮助:)