Android sutdio 开发学习笔记——第二篇:简单开发

本文介绍了如何在Android应用中使用TextView组件,包括设置文本样式、颜色,以及如何通过XML和Java代码动态改变属性。重点讲述了如何实现文本跑马灯效果并自定义一个无需点击即可循环的组件。
摘要由CSDN通过智能技术生成

开发

TextView

使用TextView可以在容器中创建一个文本,通过设置不同的属性改变文本的样式,包括颜色,尺寸,排版等等。

<?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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/tv_one"
        android:text="hello world"
        android:textColor="@color/black"
        android:textStyle="italic"
        android:background="#FFFF0000"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </TextView>
</LinearLayout>

在上述代码中,有一些属性的值是@开头,这是使用了规定好的样式,在正式开发中,不会将属性值直接写在代码中,而是在values文件夹里定义好不同的String,在组件里引用即可。

比如

android:id="@+id/tv_one"

实际上是为了让MainActivity.java里的findViewById方法使用

setContentView(R.layout.activity_main);
TextView tv_one = findViewById(R.id.tv_one);
tv_one.setText("Hello Lain");

通过ID寻找到组件后,使用Set方法改变组件的其他属性值,这里将TextView的Text修改为Hello Lain,有趣的是,我已经在xml的组件里也定义了Text的属性值为 Hello World

 android:text="hello world"

这种情况会优先Java代码,所以Hello World被修改为Hello Lain。

另一个情况是使用已经定义好的属性值,比如

 android:textColor="@color/black"

实际上这个值被存储在app\src\main\res\values\colors.xml里,如下

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
</resources>

在这个目录下,还有其他的xml文件,对应着不同的使用场景,默认有三个分别是,colors.xml负责定义好的颜色样式,strings.xml负责项目中的所有显示文字,themes.xml负责主题。

比如我希望TextView的Text属性值为Hello Lain,我可以直接在strings.xml里添加一个值,这样每次使用相同的值,我直接调用这里的名字即可。

<resources>
    <string name="app_name">financing</string>
    <string name="lain">Hello Lain</string>
</resources>

调用时

android:text="@string/lain"

属性

属性 (Attribute)描述 (Description)
text设置文本内容 (Sets the text content)
textColor设置文本颜色 (Sets the text color)
textSize设置文本大小 (Sets the text size)
textStyle设置文本样式,如粗体、斜体 (Sets text style such as bold, italic)
gravity设置文本的对齐方式 (Sets the text alignment)
fontFamily设置文本字体 (Sets the text font family)
padding设置文本的内边距 (Sets the text padding)
background设置文本的背景 (Sets the text background)
maxLines设置最大行数 (Sets the maximum number of lines)
singleLine设置是否单行显示 (Sets whether to display in a single line)
ellipsize设置超出范围时的省略方式 (Sets the ellipsis mode for overflow)

阴影

属性 (Attribute)描述 (Description)
shadowColor设置阴影的颜色 (Sets the color of the shadow)
shadowDx设置阴影在水平方向的偏移量 (Sets the horizontal offset of the shadow)
shadowDy设置阴影在垂直方向的偏移量 (Sets the vertical offset of the shadow)
shadowRadius设置阴影的半径 (Sets the radius of the shadow)

跑马灯

想要实现跑马灯需要设置5个属性分别为:

android:singleLine:内容单行显示

android:focusable:是否可以获取焦点

android:focusablelnTouchMode:用于控制视图在触摸模式下是否可以聚焦

android:ellipsize:在哪里省略文本

android:marqueeRepeatLimit:字幕动画重复的次数

首先修改文字内容,让其无法一行显示完全:

<resources>
    <string name="app_name">financing</string>
    <string name="lain">Hello Lain,Hello Lain,Hello Lain,Hello Lain,Hello Lain,Hello Lain,Hello Lain,Hello Lain,Hello Lain</string>
</resources>

通过添加这五个属性实现文字跑马灯效果

android:singleLine="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:clickable="true"

除了上述五个属性外,还需要添加一个获取点击焦点的属性,这样当点击后返回为true,开始循环跑马灯。

这样实现的跑马灯效果必须点击后才能使用,这样并不够高效,所以接下来通过自定义一个组件来实现无需点击自动获取焦点的功能。

通过在com.example里创建一个类,并且继承原组件,此时需要添加构造函数后才能正常使用,最后在重写判断焦点的方法返回为true,这样不论点击,都会循环重复。

//当前类:com.example.TextViewLain
package com.example.financing;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

import androidx.annotation.Nullable;

public class TextViewLain extends TextView {
    public TextViewLain(Context context) {
        super(context);
    }

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

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

//    重写
    @Override
    public boolean isFocused() {
        return super.isFocused();
    }
}

这段代码是Java中的一个方法重写(override)。它重写了一个名为isFocused()的方法,该方法原本是从父类(superclass)继承而来的。在重写中,它调用了super.isFocused(),意味着它调用了父类中的isFocused()方法。这个方法的作用是判断当前对象是否具有焦点(focus)。

更改返回值:

//    重写
    @Override
    public boolean isFocused() {
        return true;
    }

由于我们重写了TextView,在xml中使用它也应该调用新的类,所以变成了这样:

<com.example.financing.TextViewLain
    android:id="@+id/tv_one"
    android:text="@string/lain"
    android:textColor="@color/black"
    android:textStyle="italic"
    android:background="#FFFF0000"
    android:gravity="center"
    android:singleLine="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</com.example.financing.TextViewLain>

再次运行,会发现自动实现了跑马灯效果。

  • 37
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值