【问题标题】:Programmatically load layout into another layout以编程方式将布局加载到另一个布局中
【发布时间】:2015-11-20 04:40:40
【问题描述】:

我有一个布局,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();
    }

}

我该怎么做?

【问题讨论】:

    标签: android android-layout android-activity android-xml android-recyclerview


    【解决方案1】:

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

    这样做:

    1.首先在main.xml中为LinearLayout添加orientation为vertical:

    android:orientation="vertical"
    

    2. 在onBindViewHolder 中使用holder.title 或holder 获取要添加images.xml 的父布局:

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

    【讨论】:

    • (ViewGroup) 演员表是多余的。
    • 如果我例如在main.xml 中添加了一个LinearLayout 我希望加载布局,我可以使用您的方法将布局加载到LinearLayout 中吗?
    • @user5578746:是的布局加载为 LinearLayout 的子视图,可能在 TextView 之后。如果遇到任何问题,则根据需要使用 RelativeLayout 而不是 LinerLayout 来定位添加的布局
    • @ρяσѕρєяK 我想我理解你。但是那你为什么不能直接投给LinearLayout呢?
    • @MikeM.:是的,非常感谢你是对的。更新了我的答案
    【解决方案2】:

    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

    你的 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"/>
    

    你的子布局

    images.xml

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

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

    【讨论】:

    • ViewStub 可以在 RecyclerView 中工作吗?由于 ViewStub 被布局替换,如果我滚动它将无法找到 ViewStub,它不会导致应用程序崩溃吗?
    • @user5578746 我个人从未尝试过使用 recyclerView,但我确信它可以通过您的适配器使用 getItemViewType() 方法完成。
    【解决方案3】:

    试试这个

    <?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);
    

    【讨论】:

    • 我需要在 RecyclerView 适配器中以编程方式进行。
    • @user5578746 试试这个 LayoutInflater inflater = LayoutInflater.from(context);查看 inflatedLayout=inflater.inflate(R.layout.yourLayout, null, false); containerDestacado.addView(inflatedLayout);
    【解决方案4】:

    在主布局中包含第二个布局。

    <?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>
    

    【讨论】:

    • 我需要在 RecyclerView 适配器中以编程方式进行。
    【解决方案5】:

    把你的 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>
    

    将一个布局视图包含到另一个 &lt;include layout="@layout/your_layout"/&gt; 是 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) ); 
    

    【讨论】:

    • 我需要在 RecyclerView 适配器中以编程方式进行。
    【解决方案6】:

    要在当前布局文件中包含另一个布局:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多