2014-12-04 72 views
0

我有一些布局文件编程方式添加布局,我尝试以编程 添加新的RelativeLayout这是我的XML布局文件在Android的按钮侦听

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

    } 
} 

}

我试着解释我的问题。我可以在新的布局(我创建了
以编程方式)添加新的布局和图像视图,并且我还可以添加新的布局按钮点击一个按钮我也以编程方式创建。 现在我想编写代码来创建新的布局每次。我想递归函数。我可以解决我的问题? 如果有人知道解决问题,请帮我

回答

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>