在android按钮单击中以编程方式添加布局

时间:2014-11-12 10:06:30

标签: android android-activity android-relativelayout programmatically-created

我有一个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>

我编写了一些可以编程方式添加新布局的代码,并且我还以编程方式在布局中添加(创建)了图像视图。我想写代码巫婆会以编程方式添加新的布局。欲获得更多信息。我想创建新的布局每个图像视图点击我以编程方式创建的。

这是我的来源

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

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

        }
    });

}

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

    }
}

}

我的代码可以创建新的布局,也可以在图像视图点击中创建新的布局。每次单击图像视图时,如何编写代码以自动创建新布局? 如果有人知道解决方案,请帮助我。

0 个答案:

没有答案