android基础-view的属性(XML通用属性和方法)和布局管理器(常用布局)

来源:互联网 发布:2016淘宝店赚钱吗 编辑:程序博客网 时间:2024/05/18 03:06

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

简单的自定义VIEW

这里写图片描述
这里写图片描述
实例:

public class DrawView extends View{    public float currentX = 40;    public float currentY = 50;    // 定义、并创建画笔    Paint p = new Paint();    public DrawView(Context context)    {        super(context);    }    public DrawView(Context context , AttributeSet set)    {        super(context,set);    }    @Override    public void onDraw(Canvas canvas)    {        super.onDraw(canvas);        // 设置画笔的颜色        p.setColor(Color.RED);        // 绘制一个小圆(作为小球)        canvas.drawCircle(currentX, currentY, 15, p);    }    // 为该组件的触碰事件重写事件处理方法    @Override    public boolean onTouchEvent(MotionEvent event)    {        // 修改currentX、currentY两个属性        currentX = event.getX();        currentY = event.getY();        // 通知当前组件重绘自己        invalidate();        // 返回true表明该处理方法已经处理该事件        return true;    }}
public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        LinearLayout root = (LinearLayout) findViewById(R.id.root);        // 创建DrawView组件        final DrawView draw = new DrawView(this);        // 设置自定义组件的最小宽度、高度        draw.setMinimumWidth(300);        draw.setMinimumHeight(500);        root.addView(draw);    }}

代码下载地址,其实我不想用积分的,但是现在好像变了,最少一积分

**

布局管理器

**
1.线性布局
LinearLayout的常用XML;属性及相关方法
这里写图片描述
这里写图片描述

LinearLayout子元素支持的常用XML;属性及相关方法
这里写图片描述

2.表格布局
LinearLayout的常用XML;属性及相关方法、
这里写图片描述

3.帧布局
这里写图片描述
小案例:霓虹灯效果

public class MainActivity extends AppCompatActivity {    private int currentColor = 0;    // 定义一个颜色数组    final int[] colors = new int[] {            R.color.color1,            R.color.color2,            R.color.color3,            R.color.color4,            R.color.color5,            R.color.color6    };    final int[] names = new int[] {            R.id.view01,            R.id.view02,            R.id.view03,            R.id.view04,            R.id.view05,            R.id.view06 };    TextView[] views = new TextView[names.length];    Handler handler = new Handler()    {        @Override        public void handleMessage(Message msg)        {            // 表明消息来自本程序所发送            if (msg.what == 0x123)            {                for (int i = 0; i < names.length; i++)                {                    views[i].setBackgroundResource(colors[(i                            + currentColor) % names.length]);                }                currentColor++;            }            super.handleMessage(msg);        }    };    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        for (int i = 0; i < names.length; i++)        {            views[i] = (TextView) findViewById(names[i]);        }        // 定义一个线程周期性地改变currentColor变量值        new Timer().schedule(new TimerTask()        {            @Override            public void run()            {                // 发送一条空消息通知系统改变6个TextView组件的背景色                handler.sendEmptyMessage(0x123);            }        }, 0, 200);    }}

XML布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <!-- 依次定义6个TextView,先定义的TextView位于底层        后定义的TextView位于上层 -->    <TextView        android:id="@+id/view01"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:width="160pt"        android:height="160pt"        android:background="#f00"/>    <TextView        android:id="@+id/view02"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:width="140pt"        android:height="140pt"        android:background="#0f0"/>    <TextView        android:id="@+id/view03"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:width="120pt"        android:height="120pt"        android:background="#00f"/>    <TextView        android:id="@+id/view04"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:width="100pt"        android:height="100pt"        android:background="#ff0"/>    <TextView        android:id="@+id/view05"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:width="80pt"        android:height="80pt"        android:background="#f0f"/>    <TextView        android:id="@+id/view06"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:width="60pt"        android:height="60pt"        android:background="#0ff"/></FrameLayout>

3.相对布局
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

4.网络布局
这里写图片描述
这里写图片描述

5.绝对布局(已废弃)
不做介绍