IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android——基本组件-1 -> 正文阅读

[移动开发]Android——基本组件-1

TextView

用于显示文本的一个控件
文本的字体尺寸单位为sp;
sp:scaled pixels(放大像素)主要用于字体显示

1.常用属性
在这里插入图片描述

Shpe

可以定义控件的一些展示效果,例如圆角,渐变,填充,描边,大小,边距;shape子标签就可以实现这些效果,shape子标签有下面几个属性:corners,gradient,padding,size,solid,stroke;
可以替换图片,减少app所占用的空间。

1.使用shape给TextView自定义形状和图片

1.在res/drawable下新建shape.xml文件
在这里插入图片描述
在这里插入图片描述

2.编写shape.xml文件

设置渐变

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    设置一个黑色边框
    <stroke
        android:width="10px"
        android:color="#000000"/>
    <!--渐变 -->
    <gradient
        android:angle="270"
        android:endColor="#c0c0c0"
        android:startColor="#fcd209"/>
    <!--设置一下边距,让空间大一点-->
    <padding
        android:left="5dp"
        android:top="5dp"
        android:right="5dp"
        android:bottom="5dp"/>
</shape>

设置圆角

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    设置一个黑色边框
    <stroke
        android:width="10px"
        android:color="#000000"/>
    <!--设置透明背景色-->
    <solid
        android:color="#87ceeb"/>
    <!--设置圆角-->
    <corners
        android:bottomLeftRadius="50px"
        android:bottomRightRadius="50px"
        android:topLeftRadius="50px"
        android:topRightRadius="50px"/>
    <!--设置一下边距,让空间大一点-->
    <padding
        android:left="5dp"
        android:top="5dp"
        android:right="5dp"
        android:bottom="5dp"/>
</shape>

3.使用shape.xml文件
在这里插入图片描述
3.效果展示
在这里插入图片描述

在这里插入图片描述

EditText(输入框)

EditText输入框,集成与TextView,也继承其属性。

1.EditText特有属性
在这里插入图片描述
2.运用

<?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:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text_1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:gravity="center"
        android:text="登录页面"
        android:textSize="30dp"
        android:textStyle="bold"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="20dp">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="用户名:"
            android:layout_weight="1"
            android:textColor="#000000"
            android:textSize="20dp" />
        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:selectAllOnFocus="true"
            android:hint="请输入用户名"
            android:text="可爱多一点"
            android:layout_weight="4"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="20dp">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="密    码:"
            android:layout_weight="1"
            android:textColor="#000000"
            android:textSize="20sp"/>
        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:selectAllOnFocus="true"
            android:hint="请输入密码"
            android:inputType="textPassword"
            android:layout_weight="4"/>
    </LinearLayout>
</LinearLayout>

3.效果展示
在这里插入图片描述

Button(按钮)

Button控件继承TextView,拥有TextView的属性。

ImageView(图像视图)

ImageView继承View

1. src属性与background属性的区别

在API文档中我们发现ImagView有两个可以设置图片,分别是src和background:
1.background通常指的都是背景,而src指的是内容
2.当使用src填入图片时,是按照图片大小直接填充,并不会进行拉伸,而使用background填入图片,则是会根据ImageView给定的宽度来进行拉伸

<?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:orientation="vertical"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:background="@drawable/shape"
        android:src="@drawable/a1"
        android:padding="20dp"/>
  <!--
      <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/a1"/>
  -->  
    <ImageView
        android:layout_width="400dp"
        android:layout_height="wrap_content"
        android:background="@drawable/a1"
        android:layout_marginTop="15dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/a1"
        android:layout_marginTop="15dp"/>
    <ImageView
        android:layout_width="400dp"
        android:layout_height="wrap_content"
        android:src="@drawable/a1"
        android:layout_marginTop="15dp"/>
    </LinearLayout>
</LinearLayout>

效果展示
注:最后一张图片由于我选的图片原本尺寸有点大,所以出去了。但可以看出来使用src的时候图片是没有拉伸的。
在这里插入图片描述
2.绘制圆形的ImageView

1.新建一个java文件
在这里插入图片描述
在这里插入图片描述CircleImageView.java文件

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import androidx.annotation.Nullable;

public class CircleImageView extends androidx.appcompat.widget.AppCompatImageView {

    //画笔
    private Paint mPaint;
    //圆形图片的半径
    private int mRadius;
    //图片的宿放比例
    private float mScale;

    public CircleImageView(Context context) {
        super(context);
    }

    public CircleImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CircleImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //由于是圆形,宽高应保持一致
        int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
        mRadius = size / 2;
        setMeasuredDimension(size, size);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {

        mPaint = new Paint();

        Drawable drawable = getDrawable();

        if (null != drawable) {
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();

            //初始化BitmapShader,传入bitmap对象
            BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            //计算缩放比例
            mScale = (mRadius * 2.0f) / Math.min(bitmap.getHeight(), bitmap.getWidth());

            Matrix matrix = new Matrix();
            matrix.setScale(mScale, mScale);
            bitmapShader.setLocalMatrix(matrix);
            mPaint.setShader(bitmapShader);
            //画圆形,指定好坐标,半径,画笔
            canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
        } else {
            super.onDraw(canvas);
        }
    }

}

2.引用

<?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:orientation="vertical"
    tools:context=".MainActivity">

    <com.example.day_16_3.CircleImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:padding="20dp"
        android:src="@drawable/m1" />
</LinearLayout>

3.效果展示
在这里插入图片描述

在这里插入图片描述

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-07-17 12:03:47  更:2021-07-17 12:03:57 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/16 3:23:19-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码