java - How to copy layout of View which add child programatically? -


my mainactivity set orientation horizontal, includes 2 framelayout.

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="horizontal"     android:weightsum="1"     tools:context="com.example.helloandroid.mainactivity" >      <framelayout         android:id="@+id/framelayout1"         android:layout_width="wrap_content"         android:layout_height="match_parent"         android:layout_weight="0.5" >     </framelayout>      <framelayout         android:id="@+id/framelayout2"         android:layout_width="wrap_content"         android:layout_height="match_parent"         android:layout_weight="0.5" >     </framelayout>  </linearlayout> 

from mainactivity.java, framelayout1 add buttons programmatically , these buttons set position random code belows:

private void addnumbers(){ // numbers set random position      (int = 1; < 3; i++) {         button btn = new button(this);         btn.settext("" + i);         btn.setid(i);          framelayout.layoutparams lp = new framelayout.layoutparams(50, 50);         int leftmargin = new random().nextint(widthscreen/2 - btnsize );          int topmargin = new random().nextint(heightscreen - btnsize);             lp.leftmargin = leftmargin;         lp.topmargin = topmargin;          btn.setlayoutparams(lp);         framelayout1.addview(btn);         //framelayout2.addview(btn);      } } 

and want make framelayout2 has same layout framelayout1 image enter image description here

so, how can copy layout of framelayout1 framelayout2? , how can recognize onclick event of buttons on framelayout1 or framelayout2?

try following sample code,

private void addnumbers(){ // numbers set random position     (int = 1; < 3; i++) {         button btn1 = new button(this);         button btn2 = new button(this);         btn1.setonclicklistener(listener1);         btn2.setonclicklistener(listener2); // here using different listener         btn1.settext("" + i);         btn2.settext("" + i); //same text          framelayout.layoutparams lp = new framelayout.layoutparams(50, 50);         int leftmargin = new random().nextint(widthscreen/2 - btnsize );          int topmargin = new random().nextint(heightscreen - btnsize);             lp.leftmargin = leftmargin;         lp.topmargin = topmargin;          btn1.setlayoutparams(lp);         btn2.setlayoutparams(lp); //same lp           framelayout1.addview(btn1);         framelayout2.addview(btn2);     } } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -