Android 在另一个布局中以编程方式膨胀布局

Android 在另一个布局中以编程方式膨胀布局,android,xml,android-layout,android-inflate,Android,Xml,Android Layout,Android Inflate,我需要android应用程序的帮助。我需要在另一个布局中膨胀一个布局,我不知道怎么做。我的xml代码如下: item.xml-我需要膨胀多个xml(取决于变量编号) 这是我的主要xml: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wr

我需要android应用程序的帮助。我需要在另一个布局中膨胀一个布局,我不知道怎么做。我的xml代码如下:

  • item.xml-我需要膨胀多个xml(取决于变量编号)

    
    
  • 这是我的主要xml:

    <ScrollView
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
    
      <LinearLayout
           android:id="@+id/container_destacado"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:orientation="vertical" >
    
               <!-- Inflate multiple xml file here -->
    
          </LinearLayout>
    </ScrollView>
    
    
    

您有一个线性布局,您想在该布局上为其他孩子充气:

LinearLayout container = (LinearLayout)parentView.findViewById(R.id.container_destacado);
一旦用充气器加载item.xml,就可以使用

container.addView(itemView);

你可以用像这样的东西

LayoutInflater inflater = LayoutInflater.from(context);

//to get the MainLayout
View view = inflater.inflate(container_destacado, null);
...
//Avoid pass null in the root it ignores spaces in the child layout

View inflatedLayout= inflater.inflate(R.layout.yourLayout, (ViewGroup) view, false);
containerDestacado.addView(inflatedLayout);

在某些情况下,您应该能够访问活动中的充气机,您可以使用以下代码调用它:

LayoutInflater li = LayoutInflater.from(context);
其中,上下文是this或this.getActivity(),如果它是一个片段。然后用以下方法为您的假期充气:

View layout = li.inflate(R.layout.your_layout, null, false);
然后将addView(布局)用于您的容器\u destacado:

View containerDestacado = li.inflate(R.layout.container_destacado, null, false);
containerDestacado.addView(layout);
希望有帮助。

ll=(LinearLayout)findViewById(R.id.container\u destacado);//ll是将添加充气布局的布局
ll = (LinearLayout) findViewById(R.id.container_destacado);  // ll is the layout where your inflated layout will be added 
linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int pos = 0;
    while (pos < noOfTimes) 
    {
        View myView = linflater.inflate(R.layout.item, null); //here item is the the layout you want to inflate 
        myView.setId(pos);
        /*
           You can change TextView text and ImageView images here e.g.
           TextView tv = (TextView)myView.findViewById(R.id.title_N1);
           tv.setText(pos);

        */
        pos++;
        ll.addView(myView);
    }
linflater=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE); int pos=0; 而(位置<中午) { View myView=linflater.inflate(R.layout.item,null);//此处item是要充气的布局 myView.setId(pos); /* 您可以在此处更改文本视图文本和图像视图图像,例如:。 TextView tv=(TextView)myView.findViewById(R.id.title_N1); tv.setText(pos); */ pos++; ll.addView(myView); }
您可以像下面这样实现:

LayoutInflater linf;
LinearLayout rr;

linf = (LayoutInflater) getApplicationContext().getSystemService(
        Context.LAYOUT_INFLATER_SERVICE);
linf = LayoutInflater.from(activity.this);

rr = (LinearLayout) findViewById(R.id.container_destacado);

for (int i = 1; i < NoOfTimes; i++) {

    final View v = linf.inflate(R.layout.item, null);
    rr.addView(v);
}
layoutiner-linf;
线性布局rr;
linf=(LayoutInflater)getApplicationContext().getSystemService(
上下文。布局(充气机和服务);
linf=LayoutInflater.from(activity.this);
rr=(线性布局)findViewById(R.id.container\u destacado);
for(int i=1;i
Kotlin代码执行此操作:

 val layoutToInflate = 
 this.layoutInflater.inflate(R.layout.ly_custom_layout, 
  null)
container_destacado.addView(layoutToInflate )

下面是我的一个项目的工作代码:

// The parent container
mNotificationOverlay = (LinearLayout) findViewById(R.id.container_destacado);   
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

for (Message message : m) {

    // Inflate the child layout on the fly
    final View notificationContainer = inflater.inflate(R.layout.notification_overlay_linear_layout, null);
    notificationContainer.setTag(message.getNotificationId());

    // Access children of child container
    TextView notificationOverlayTitle = (TextView) notificationContainer.findViewById(R.id.notification_title_overlay);
    TextView notificationOverlayBody = (TextView) notificationContainer.findViewById(R.id.notification_body_overlay);
    ImageButton notificationOverlayCancelButton = (ImageButton) notificationContainer.findViewById(R.id.notification_cancel_overlay);

    // Perform desired operations
    notificationOverlayCancelButton.setTag(message.getNotificationId());
    notificationOverlayTitle.setText(message.getTitle());
    notificationOverlayBody.setText(message.getNotificationBody());
    mNotificationOverlay.setVisibility(View.VISIBLE);

    // Attach any listeners
    attachListenersToCancelView(notificationOverlayCancelButton);

    // Add view to parent container
    mNotificationOverlay.addView(notificationContainer);
}

希望有帮助

在Kotlin中,item.xml上有可编辑的textview元素

  val item = LayoutInflater.from(this).inflate(R.layout.item,null)
  val distance_N1_holder = item.findViewById<TextView>(R.id.distance_N1)
  distance_N1_holder.text = //dynamically generated value

  main.addView(item)
val item=LayoutInflater.from(this).充气(R.layout.item,null)
val distance_N1_holder=item.findViewById(R.id.distance_N1)
距离\u N1\u holder.text=//动态生成的值
main.addView(项目)

如果有人无法获取
添加视图()
这是因为该视图不是布局。

视图视图=充气机。充气(容器\u destacado,空);在这一行中,我得到了一个错误,它没有将我的主布局作为参数@Adam
  val item = LayoutInflater.from(this).inflate(R.layout.item,null)
  val distance_N1_holder = item.findViewById<TextView>(R.id.distance_N1)
  distance_N1_holder.text = //dynamically generated value

  main.addView(item)