Android学习记录(四)


今天的学习内容为Android中的常用布局,在前几天的任务中,我已经使用过了线性布局以及网格布局。所以今天我首先会进行两种布局方法的进一步了解,然后在进行新内容的学习。

线性布局

在这里插入图片描述

网格布局

在这里插入图片描述

帧式布局

帧式布局常用属性

android:foreground:*设置改帧布局容器的前景图像
android:foregroundGravity:设置前景图像显示的位置
android:scrollbars:滚动条(none、horizontal、vertical)
android:layout_marginTop:上边距
android:layout_marginBottom:下边距
android:layout_marginLeft:左边距
android:layout_marginRight:右边距
android:paddingLeft:左内边距
android:paddingRight:右内边距
android:paddingTop:上内边距
android:paddingBottom:下内边距
android:background:背景

案例演示

效果展示

在这里插入图片描述

代码展示

1.activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tvBottom"
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:layout_gravity="center"
            android:background="#ff0000"
            android:text="@string/bottom"
            android:textColor="#ffff00"
            android:textSize="30sp"/>
        <TextView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:id="@+id/tvMiddle"
            android:layout_gravity="center"
            android:background="#0000ff"
            android:text="@string/middle"
            android:textColor="#ffff00"
            android:textSize="30sp"/>
        <TextView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/tvTop"
            android:layout_gravity="center"
            android:background="#00ff00"
            android:text="@string/top"
            android:textColor="#ffff00"
            android:textSize="30sp"/>

    </FrameLayout>

    <Button
        android:id="@+id/btnSwitchColor"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:onClick="doSwitchColor"
        android:text="@string/switch_color"
        android:textSize="20sp" />

</LinearLayout>

2.strings.xml文件

<resources>
    <string name="app_name">SwitchColor</string>
    <string name="bottom">底层</string>
    <string name="middle">中层</string>
    <string name="top">顶层</string>
    <string name="switch_color">切换颜色</string>
</resources>

3.MainActivity.java文件

package net.nell.switchcolor;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    
    
    private TextView tvBottom;
    private TextView tvMiddle;
    private TextView tvTop;
    private int clickCount;
    private int[] colors;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //通过资源标识获得控件实例
        tvBottom = findViewById(R.id.tvBottom);
        tvMiddle = findViewById(R.id.tvMiddle);
        tvTop = findViewById(R.id.tvTop);
    }
    /**
     * 切换颜色单击事件处理方法
     *
     * @param view
     */
    public void doSwitchColor(View view){
    
    
        //累计按钮单击次数
        clickCount++;
        //单击次数对3求余
        clickCount = clickCount % 3;
        //判断次数是0、1、2
        switch (clickCount){
    
    
            case 0 :
                //红——蓝——绿
                colors = new int[]{
    
    Color.RED, Color.BLUE,Color.GREEN};
                break;
            case 1:
                //蓝——绿——红
                colors = new int[]{
    
    Color.BLUE,Color.GREEN,Color.RED};
                break;
            case 2:
                //绿——红——蓝
                colors = new int[]{
    
    Color.GREEN,Color.RED,Color.BLUE};
                break;
        }
        //根据切换后的颜色数组来设置三层标签颜色
        tvBottom.setBackgroundColor(colors[0]);
        tvMiddle.setBackgroundColor(colors[1]);
        tvTop.setBackgroundColor(colors[2]);
    }
}

为达到同样的效果,我们可以进行代码的优化,具体如下所示:

package net.nell.switchcolor;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    
    
    private TextView tvBottom;
    private TextView tvMiddle;
    private TextView tvTop;
    //private int clickCount;
    private int[] colors;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //通过资源标识获得控件实例
        tvBottom = findViewById(R.id.tvBottom);
        tvMiddle = findViewById(R.id.tvMiddle);
        tvTop = findViewById(R.id.tvTop);

        //初始化颜色数组
        colors = new int[]{
    
    Color.RED,Color.BLUE,Color.GREEN};
    }
    /**
     * 切换颜色单击事件处理方法
     *
     * @param view
     */
    public void doSwitchColor(View view){
    
    
        /**
        //累计按钮单击次数
        clickCount++;
        //单击次数对3求余
        clickCount = clickCount % 3;
        //判断次数是0、1、2
        switch (clickCount){
            case 0 :
                //红——蓝——绿
                colors = new int[]{Color.RED, Color.BLUE,Color.GREEN};
                break;
            case 1:
                //蓝——绿——红
                colors = new int[]{Color.BLUE,Color.GREEN,Color.RED};
                break;
            case 2:
                //绿——红——蓝
                colors = new int[]{Color.GREEN,Color.RED,Color.BLUE};
                break;*/
        //切换颜色
        int temp = colors[0];
        colors[0] = colors[1];
        colors[1] = colors[2];
        colors[2] = temp;

        /**
         * int temp = colors[0];
         * for(int i = 0;i < colors.length - 1;i++){
         *     colors[i] = colors[i + 1];
         * }
         * colors[colors.length -1] = temp;
         */
        //设置三层标签的颜色
        tvBottom.setBackgroundColor(colors[0]);
        tvMiddle.setBackgroundColor(colors[1]);
        tvTop.setBackgroundColor(colors[2]);

    }

    /**

//根据切换后的颜色数组来设置三层标签颜色
        tvBottom.setBackgroundColor(colors[0]);
        tvMiddle.setBackgroundColor(colors[1]);
        tvTop.setBackgroundColor(colors[2]); *
            */
    }

其中注释部分代表不同的方法。
以上就是我今天的学习内容,虽然不是很多,但是我的收获很大。

猜你喜欢

转载自blog.csdn.net/weixin_46705517/article/details/112429726