如何向活动添加多个布局

时间:2016-08-01 11:22:47

标签: android list layout

我是Android编程的新手,在做项目的过程中,我遇到了一个问题。我需要添加一些包含文本和按钮的线性布局。我编写了代码,但它只是将一个布局放在前一个布局上。我应该在代码中更改它以将布局放在列表中。

以下是代码:

package com.example.sanzharaubakir.fin;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sanzharaubakir on 01.08.16.
 */
public class Scanned extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scanned);
        Intent in = getIntent();
        int num = in.getIntExtra("n", 0);
        String [] s = in.getStringArrayExtra("arr");
        for (int i = 0 ; i < num; i++)
            Toast.makeText(getApplicationContext(), "" + s[i], Toast.LENGTH_LONG).show();
        List<String> str = new ArrayList<String>();
        for(int i = 0; i < num; i++)
        {
            str.add(s[i]);
        }
        final List<Integer> intList = new ArrayList<Integer>();
        for (int i = 0 ;i < num; i++)
        {
            final LinearLayout l = new LinearLayout(this);
            l.setOrientation(LinearLayout.HORIZONTAL);
            LinearLayout.LayoutParams lParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            setContentView(l, lParam);
            ViewGroup.LayoutParams lpView = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            TextView v = new TextView(this);
            v.setText(s[i]);
            v.setLayoutParams(lpView);
            l.addView(v);

            Button delete = new Button(this);
            delete.setText("Delete");
            delete.setLayoutParams(lpView);
            l.addView(delete);

            final int finalI = i;
            delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    intList.add(finalI);
                    l.setVisibility(View.GONE);
                }
            });
        }

    }
}

和XML文件如下所示:

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

</LinearLayout>

3 个答案:

答案 0 :(得分:0)

不要在forloop中创建linearlayout实例,

final List<Integer> intList = new ArrayList<Integer>();
final LinearLayout l = new LinearLayout(this);
        l.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams lParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        setContentView(l, lParam);
    for (int i = 0 ;i < num; i++)
    {

        ViewGroup.LayoutParams lpView = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        TextView v = new TextView(this);
        v.setText(s[i]);
        v.setLayoutParams(lpView);
        l.addView(v);

        Button delete = new Button(this);
        delete.setText("Delete");
        delete.setLayoutParams(lpView);
        l.addView(delete);

        final int finalI = i;
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intList.add(finalI);
                l.setVisibility(View.GONE);
            }
        });
    }

答案 1 :(得分:0)

因为您正在使用setContentView,它会将活动布局设置为每次新创建的布局。要执行您愿意执行的操作,请在R.layout.scanned xml文件中为您的linearlayout设置ID,并将其他线性布局添加到此处。另外,从上面的循环中删除setContentView。如果您遇到任何问题,请告诉我。

答案 2 :(得分:0)

使用以下代码。

  final List<Integer> intList = new ArrayList<Integer>();
    final LinearLayout l = new LinearLayout(this);
    LinearLayout.LayoutParams lParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    for (int i = 0; i < num; i++) {

        l.setOrientation(LinearLayout.HORIZONTAL);
        l.setLayoutParams(lParam);
        //setContentView(l, lParam);
        ViewGroup.LayoutParams lpView = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        TextView v = new TextView(this);
        //v.setText(s[i]);
        v.setLayoutParams(lpView);
        l.addView(v);

        Button delete = new Button(this);
        delete.setText("Delete");
        delete.setLayoutParams(lpView);
        l.addView(delete);

        final int finalI = i;
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intList.add(finalI);
                l.setVisibility(View.GONE);
            }
        });
    }
    setContentView(l,lParam);

它对我有用.. !!