以编程方式将布局加载到另一个布局

问题描述:

我有一个布局images.xml,我想要加载/膨胀到另一个布局main.xml以编程方式将布局加载到另一个布局

这里是主要的布局,main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 

    // Load into placeholder 
    <LinearLayout 
     android:id="@+id/placeholder" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     /> 

    <TextView 
     android:id="@+id/desc" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 

</LinearLayout> 

这里是images.xml,我要加载到上述布局的布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     /> 

</LinearLayout> 

我用,我用一个RecyclerView适配器将数据加载到主布局中,并且这是我想要加载/扩展子布局的位置:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> { 

    private static Context context; 
    private List<Message> mDataset; 

    public RecyclerAdapter(Context context, List<Message> myDataset) { 
     this.context = context; 
     this.mDataset = myDataset; 
    } 

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener, View.OnClickListener { 
     public TextView title; 
     public LinearLayout placeholder; 

     public ViewHolder(View view) { 
      super(view); 
      view.setOnCreateContextMenuListener(this); 

      title = (TextView) view.findViewById(R.id.title); 
      placeholder = (LinearLayout) view.findViewById(R.id.placeholder); 
     } 
    } 

    @Override 
    public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_layout, parent, false); 
     ViewHolder vh = new ViewHolder((LinearLayout) view); 

     return vh; 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     Message item = mDataset.get(position); 

     holder.title.setText(item.getTitle()); 

     int numImages = item.getImages().size(); 

     if (numImages > 0) { 
      View test = LayoutInflater.from(holder.placeholder.getContext()).inflate(R.layout.images, holder.placeholder, false); 
      ImageView image = (ImageView) test.findViewById(R.id.image); 
      Glide.with(context) 
       .load("http://www.website.com/test.png") 
       .fitCenter() 
       .into(image); 
      holder.placeholder.addView(test); 
     } 
    } 

    @Override 
    public int getItemCount() { 
     return mDataset.size(); 
    } 

} 

我该怎么做?

要包含其他布局到当前布局文件:

<include layout="@layout/titlebar"/> 

其中titlebar.xml是另一个布局文件。欲了解更多请访问:http://developer.android.com/training/improving-layouts/reusing-layouts.html

您的main.xml改成这样:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 

    // Load sub-layout here 
<include 
     android:id="@+id/imageLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     layout="@layout/images" /> 
</LinearLayout> 

包括一个布局视图到另一个<include layout="@layout/your_layout"/>是xml文件来实现这一目标的标签。

要添加视图动态下面的代码将有助于:

改变你的main.xml到下面的代码:

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

     <TextView 
      android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      /> 

     // Load sub-layout here 

    </LinearLayout> 

写下面的代码动态添加视图:

// get your outer relative layout 
LinearLayout ll_mainLayout= (LinearLayout) findById(R.id.ll_mainLayout); 


// inflate content layout and add it to the relative layout as second child 
// add as second child, therefore pass index 1 (0,1,...) 

LayoutInflater layoutInflater = (LayoutInflater) 
     this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
rl.addView(1, layoutInflater.inflate(R.layout.images, this, false)); 
+0

我需要做的是编程,在RecyclerView适配器内。 – user5578746

1.如果您想以编程方式使用 ,请使用ViewStub

<ViewStub 
    android:id="@+id/layout_stub" 
    android:inflatedId="@+id/message_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="0.75" /> 

然后在你的java类来完成此

ViewStub stub = (ViewStub) findViewById(R.id.layout_stub); 
stub.setLayoutResource(R.layout.whatever_layout_you_want); 
View inflated = stub.inflate(); 
  1. 使用include
  2. 您的main.xml

    <?xml version="1.0" encoding="utf-8"?> 
    

    <TextView 
        android:id="@+id/title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        /> 
    
    // Load sub-layout here 
    
    <include 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/some_id_if_needed" 
        layout="@layout/images"/> 
    

    你的子布局

    图像。XML

    <?xml version="1.0" encoding="utf-8"?> 
    

    <ImageView 
        android:id="@+id/image" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        /> 
    

开始=>
+0

虽然ViewStub可以在RecyclerView中工作吗?由于ViewStub被替换为布局,是不是会导致应用程序崩溃,因为如果我滚动它将无法找到ViewStub? – user5578746

+0

@ user5578746我个人从未尝试过使用recyclerView,但我确定可以使用getItemViewType()方法通过适配器完成它。 –

+0

看到这,http://*.com/questions/31609215/is-there-a-way-to-use-a-viewstub-inside-a-recyclerview –

试试这个

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 

    // Load sub-layout here 
<include layout = "@layout/images"/> 

</LinearLayout> 

您应该使用include标签包含另一个布局为主要布局

编辑

以编程方式添加视图试试这个

LayoutInflater inflater = LayoutInflater.from(context); 
View inflatedLayout= inflater.inflate(R.layout.images, null, false); 
mainView.addView(inflatedLayout); 
+0

我需要以编程方式,在RecyclerView适配器内。 – user5578746

+0

@ user5578746试试这个LayoutInflater inflater = LayoutInflater.from(context); 查看inflatedLayout = inflater.inflate(R.layout.yourLayout,null,false); containerDestacado.addView(inflatedLayout); –

+0

@ user5578746请参阅我的编辑 –

包括主要布局内的第二次布局。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
    /> 

    <include layout="@layout/images.xml" /> 

</LinearLayout> 
+0

我需要以编程方式在RecyclerView适配器中执行此操作。 – user5578746

我有一个布局,images.xml,我想加载/膨胀到另一个 布局,main.xml中

做得一样:

1。首先添加orientationLinearLayoutmain.xmlvertical

android:orientation="vertical" 

2.使用holder.titleonBindViewHolderholder得到其中要添加images.xml父布局:

LinearLayout linearLayout=(LinearLayout)holder.title.getParent(); 
    View view = LayoutInflater.from(parent.getContext()).inflate(
              R.layout.images, parent, false); 
    linearLayout.addView(view); 
+0

'(ViewGroup)'强制转换是多余的。 –

+0

例如,如果我在'main.xml'中添加了一个'LinearLayout',我希望布局被加载,我可以使用您的方法将布局加载到'LinearLayout'中? – user5578746

+0

@MikeM。因为Layout包含LinearLayout作为根容器,但'getParent'返回'ViewGroup',所以即时将其转换为LinearLayout。当我们想要使用findViewById访问扩展布局的视图时,这很有用。 –