纯代码布局实现九宫格

    xiaoxiao2021-03-25  142

    安卓老师布置了作业 坐下笔记 还望指正

    import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private int a=(int)(Math.random()*255); private int b=(int)(Math.random()*255); private int c=(int)(Math.random()*255); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.java); sss(); } private void sss() { /* 加入一个总线性布局 */ LinearLayout main=new LinearLayout(this); LinearLayout.LayoutParams mainsize= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); main.setOrientation(LinearLayout.VERTICAL); /* 设置textView的大小和linearLayout的大小 */ LinearLayout.LayoutParams textViewsize=new LinearLayout.LayoutParams(250,LinearLayout.LayoutParams.MATCH_PARENT,1); LinearLayout.LayoutParams linearLayoutsize= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1); /* 分别设置三个子线性布局,水平排布 */ LinearLayout linearLayout2 = new LinearLayout(this); linearLayout2.setLayoutParams(linearLayoutsize); linearLayout2.setOrientation(LinearLayout.HORIZONTAL); main.addView(linearLayout2); LinearLayout linearLayout3 = new LinearLayout(this); linearLayout3.setLayoutParams(linearLayoutsize); linearLayout3.setOrientation(LinearLayout.HORIZONTAL); main.addView(linearLayout3); LinearLayout linearLayout4 = new LinearLayout(this); linearLayout4.setLayoutParams(linearLayoutsize); linearLayout4.setOrientation(LinearLayout.HORIZONTAL); main.addView(linearLayout4); /* 第一个线性子布局,水平排布 加入三个textView */ TextView t1=new TextView(this); TextView t2=new TextView(this); TextView t3=new TextView(this); color(t1); color(t2); color(t3); t1.setLayoutParams(textViewsize); t2.setLayoutParams(textViewsize); t3.setLayoutParams(textViewsize); linearLayout2.addView(t1); linearLayout2.addView(t2); linearLayout2.addView(t3); /* 第二个线性子布局,水平排布 加入三个textView */ TextView t4=new TextView(this); TextView t5=new TextView(this); TextView t6=new TextView(this); color(t4); color(t5); color(t6); t4.setLayoutParams(textViewsize); t5.setLayoutParams(textViewsize); t6.setLayoutParams(textViewsize); linearLayout3.addView(t4); linearLayout3.addView(t5); linearLayout3.addView(t6); /* 第三个线性子布局,水平排布 加入三个textView */ TextView t7=new TextView(this); TextView t8=new TextView(this); TextView t9=new TextView(this); color(t7); color(t8); color(t9); t7.setLayoutParams(textViewsize); t8.setLayoutParams(textViewsize); t9.setLayoutParams(textViewsize); linearLayout4.addView(t7); linearLayout4.addView(t8); linearLayout4.addView(t9); this.addContentView(main, mainsize); } public void color(View x){ int a=(int)(Math.random()*255); int b=(int)(Math.random()*255); int c=(int)(Math.random()*255); x.setBackgroundColor(Color.rgb(a,b,c)); } }

    转载请注明原文地址: https://ju.6miu.com/read-20119.html

    最新回复(0)