Android开发笔记01-TextView01

【2021 最新版】Android studio全套教程+Android(安卓)开发入门到精通(项目实战篇)_哔哩哔哩_bilibili

1、TextView

android:id        组件ID(demo中的id值为:textView_name)

android:layout_width        组件宽度

android:layout_height        组件高度

对于组件宽度/高度:
方式一、值为:wrap_content(自动分配宽度或者高度)
方式二、指定dp值例如 200dp
方式三、match_parent,和父级结构保持相同大小

android:text        设置显示的文本内容,文本值

android:textColor    设置文本的颜色,#00颜色透明度+00红色+00绿色+00蓝色。     #FF00FF00

android:textSize        字体大小,与组件大小dp不同的是单位一般是sp。

android:textStyle        字体样式,默认为normal

android:background        控件的背景色,可以理解为填充整个控件的颜色,可以是北京图片

android:gravity        设置控件中内容的对齐方向,TextView中是文字,ImageView中是图片等等。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">
    
    <TextView
        android:id="@+id/textView_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是文本"
        tools:layout_editor_absoluteX="149dp"
        tools:layout_editor_absoluteY="211dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

在MainActivity.class中

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView_name=findViewBy(R.id.textView_name);
        textView_name.setText("我是TextView,我现在被赋值了");

    }
}

假如在res/values/strings.xml中有预先定义好的字符串,比如定义常用的颜色等等。

我预先定义了一个name为key0字符串为value0的<string></string>标签

<resources>
    <string name="app_name">LDS</string>
    <string name="title_home">Home</string>
    <string name="title_dashboard">Dashboard</string>
    <string name="title_notifications">Notifications</string>
    <string name="key0">value0</string>
</resources>

这时候在TextView中可以去引用这个name为key0的值

 android:text="@string/key0"

2、带阴影的TextView

1.android:shadowColor:设置阴影颜色,需要与shadowRadius一起使用

2.andorid:shadowRadius:设置阴影的模糊程度,设为0.1就变成字体颜色了,建议使用3.0

3.andorid:shadowDx:设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置

4.android:shadowDy:设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="123456我是modelID"
        android:shadowRadius="3.0"
        android:shadowDx="10.0"
        android:shadowDy="10.0"
        android:shadowColor="#302626"
        tools:layout_editor_absoluteX="149dp"
        tools:layout_editor_absoluteY="211dp" />

3、跑马灯效果的TextView

1.android:singleLine:内容单行显示

2.android:focusable:是否可以获取焦点,赋值true

3.android:focusableTouchMode:用于控制视图在触摸模式下是否可以聚焦,赋值true

4.android:ellipsize:在哪里省略文本,如果值为start则三个省略号在前,如果值为end省略号在后,marquee则会跑马灯显示。

5.android:marqueeRepeatLimit:字幕动画重复次数,设置成marquee_forever无限次流动 

6.android:clickable="true"

 在res/values/strings.xml中

<resources>
    <string name="app_name">LDS</string>
    <string name="title_home">Home</string>
    <string name="title_dashboard">Dashboard</string>
    <string name="title_notifications">Notifications</string>
    <string name="key0">同舍生皆被绮绣,戴朱缨宝饰之帽,腰白玉之环,左佩刀,右备容臭,烨然若神人;余则缊袍敝衣处其间,略无慕艳意。</string>
</resources>

在activity_main.xml中

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/key0"
        android:textColor="#00FFE8"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:clickable="true"

        android:shadowRadius="3.0"
        android:shadowDx="10.0"
        android:shadowDy="10.0"
        android:shadowColor="#3EFAAD"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

方式1.activity_main.xml中引用自己继承的TextView

    <com.example.app.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/key0"
        android:textColor="#00FFE8"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:clickable="true"

        android:shadowRadius="3.0"
        android:shadowDx="10.0"
        android:shadowDy="10.0"
        android:shadowColor="#3EFAAD"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

        在package com.example.app;目录下,新建MyTextView 继承TextView

   重写它的构造方法,最终要的是重写它的isFocused方法并return true;

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

package com.example.app;

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

import androidx.annotation.Nullable;

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

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

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

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

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

方式2.activity_main.xml中用TextView,不过在末尾需要加上<requestFocus/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/key0"
        android:textColor="#00FFE8"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:clickable="true"

        android:shadowRadius="3.0"
        android:shadowDx="10.0"
        android:shadowDy="10.0"
        android:shadowColor="#3EFAAD"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <requestFocus/>
    </TextView>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值