纯代码布局实现九宫格

这是一篇关于安卓开发的笔记,详细记录了如何通过纯代码方式实现一个功能完善的九宫格布局,适用于安卓应用开发的学习和实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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


    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值