Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. LinearLayout btnsContainer = new LinearLayout(getApplicationContext());
  2. btnsContainer.setLayoutParams(new LinearLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
  3. btnsContainer.setOrientation(LinearLayout.VERTICAL);
  4. btnsContainer.setGravity(Gravity.CENTER);
  5. //Crea botons dinamicamente.
  6. for (int i = 0; i < 100; i++){
  7. final LinearLayout buttonContainer = (LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_button,null);
  8. ImageView btnImg = (ImageView) buttonContainer.findViewById(R.id.btn_image);
  9. TextView btnTxt = (TextView) buttonContainer.findViewById(R.id.btn_text);
  10. btnTxt.setText("mi Botón no." + i);
  11. btnTxt.setBackgroundColor(getRandomColor());
  12. btnImg.setImageResource(R.mipmap.ic_launcher);
  13. buttonContainer.setTag(i);
  14.  
  15. buttonContainer.setOnClickListener(new View.OnClickListener() {
  16. @Override
  17. public void onClick(View v) {
  18. Toast.makeText(getApplicationContext(), " Listener botón " + v.getTag(), Toast.LENGTH_SHORT).show();
  19. }
  20. });
  21. //Va agregegando botones al contenedor.
  22. btnsContainer.addView(buttonContainer);
  23. }
  24. //Crea contenedor para agregar contenedor de botones.
  25. FrameLayout.LayoutParams paramsContainer = new FrameLayout.LayoutParams(400, 1500, Gravity.CENTER);
  26. //Agrega contenedor con botones.
  27. addContentView(btnsContainer, paramsContainer);
  28.  
  29. public int getRandomColor(){
  30. Random rnd = new Random();
  31. return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
  32. }
  33.  
  34. <?xml version="1.0" encoding="utf-8"?>
  35. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:gravity="center"
  39. android:padding="10px"
  40. android:orientation="vertical" >
  41.  
  42. <ImageView
  43. android:id="@+id/btn_image"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:contentDescription="@null"/>
  47.  
  48. <TextView
  49. android:id="@+id/btn_text"
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:textSize="12sp"
  53. android:textColor="#F0F0F0" />
  54.  
  55. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement