在android按钮监听器中以编程方式添加布局

时间:2014-12-04 15:33:01

标签: android android-imageview android-relativelayout

我有一些布局文件,我尝试以编程方式添加新的relativelayout 这是我的xml布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainlayout"
android:layout_width="wrap_content"
android:layout_height="match_parent" >

<RelativeLayout
    android:id="@+id/rot"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="#ff0000" >

    <ImageView
        android:id="@+id/btn_categorry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="12dp"
        android:background="@drawable/ic_launcher" />
</RelativeLayout>

这是java代码

public class MainActivity extends Activity {
RelativeLayout myImage, mainlayout;
private ImageView img;
RelativeLayout staticlayout;
RelativeLayout.LayoutParams parms;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myImage = (RelativeLayout) findViewById(R.id.rot);
    mainlayout = (RelativeLayout) findViewById(R.id.mainlayout);

    img = (ImageView) findViewById(R.id.btn_categorry);

    parms = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.FILL_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    img.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            staticlayout = new RelativeLayout(getApplicationContext());
            staticlayout.setBackgroundColor(Color.parseColor("#000000"));

            ImageView add_btn = new ImageView(getApplicationContext());
            add_btn.setBackgroundResource(R.drawable.ic_launcher);

            RelativeLayout.LayoutParams parms2 = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            parms2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            parms2.addRule(RelativeLayout.CENTER_HORIZONTAL);
            add_btn.setLayoutParams(parms2);
            add_btn.setOnClickListener(new listener());
            staticlayout.setId(10);
            staticlayout.addView(add_btn);

            parms.addRule(RelativeLayout.BELOW, R.id.rot);

            staticlayout.setLayoutParams(parms);
            mainlayout.addView(staticlayout);

        }
    });

}

class listener implements OnClickListener {

    @Override
    public void onClick(View v) {
        RelativeLayout.LayoutParams costomparam = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout relativelayout = new RelativeLayout(
                getApplicationContext());
        relativelayout.setBackgroundColor(Color.parseColor("#AB3232"));

        ImageView add_btn = new ImageView(getApplicationContext());
        add_btn.setBackgroundResource(R.drawable.ic_launcher);

        RelativeLayout.LayoutParams imagelayoutparam = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        imagelayoutparam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        imagelayoutparam.addRule(RelativeLayout.CENTER_HORIZONTAL);
        add_btn.setOnClickListener(new listener());
        add_btn.setLayoutParams(imagelayoutparam);
        relativelayout.addView(add_btn);

        costomparam.addRule(RelativeLayout.BELOW, staticlayout.getId());

        relativelayout.setLayoutParams(costomparam);
        mainlayout.addView(relativelayout);

    }
}

}

我试着解释我的问题。我可以在新的布局中添加新的布局和imageview(我创建了它 以编程方式)我也可以在按钮点击中添加新的布局我也是以编程方式创建的按钮。 现在我想编写代码来每次创建新的布局。我想要递归函数。我可以解决我的问题吗? 如果有人知道解决方案问题,请帮帮我

1 个答案:

答案 0 :(得分:0)

检查此解决方案是否满足您的需求:

活动代码:

package com.example.abc;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

    private RelativeLayout myImage, mainlayout;
    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        myImage = (RelativeLayout) findViewById(R.id.rot);
        mainlayout = (RelativeLayout) findViewById(R.id.mainlayout);

        img = (ImageView) findViewById(R.id.btn_categorry);
        img.setOnClickListener(new RecursionOnClickListener());
    }

    private int layoutId = 1;

    class RecursionOnClickListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            RelativeLayout staticlayout = new RelativeLayout(getApplicationContext());
            staticlayout.setId(layoutId++);
            if(layoutId % 2 == 0) {
                staticlayout.setBackgroundColor(Color.parseColor("#000000"));
            }
            else {
                staticlayout.setBackgroundColor(Color.parseColor("#FF0000"));
            }

            ImageView add_btn = new ImageView(getApplicationContext());
            add_btn.setBackgroundResource(R.drawable.ic_launcher);

            RelativeLayout.LayoutParams parms2 = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            parms2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            parms2.addRule(RelativeLayout.CENTER_HORIZONTAL);
            add_btn.setLayoutParams(parms2);
            add_btn.setOnClickListener(new RecursionOnClickListener());
            staticlayout.addView(add_btn);

            RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            parms.addRule(RelativeLayout.BELOW, mainlayout.getChildAt(mainlayout.getChildCount()-1).getId());

            staticlayout.setLayoutParams(parms);
            mainlayout.addView(staticlayout);
        }

    }

}

布局XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00" >

    <RelativeLayout
        android:id="@+id/rot"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="#ff0000" >

        <ImageView
            android:id="@+id/btn_categorry"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="12dp"
            android:background="@drawable/ic_launcher" />
    </RelativeLayout>

</RelativeLayout>