0
点赞
收藏
分享

微信扫一扫

Android 基础开发组件 TextView

kolibreath 2022-02-06 阅读 90

layout_width

TextView宽度
wrap_content:自动
match_parent:充满父容器

<TextView
	android:layout_width="match_parent">
</TextView>

或者使用dp单位来表示

<TextView
	android:ayout_width="200dp">
</TextView>

layout_height

TextView高度
wrap_content:自动
match_parent:充满父容器

<TextView
	android:layout_height="wrap_content">
</TextView>

或者使用dp单位来表示

<TextView
	android:layout_height="200dp">
</TextView>

id

TextView组件id

<TextView
	android:id="@+id/tv_hello">
</TextView>

text

文本内容

<TextView
	android:text="Hello World">
</TextView>

获取id改变文本内容

TextView tv_hello = findViewById(R.id.tv_hello);
tv_hello.setText("Hello World!");

strings.xml

自定义字符串集

<resources>
  <string name="app_name">App</string>
  <string name="tv_hello">Hello World!</string>
  <string name="login">登录</string>
  <string name="et_username">用户名</string>
  <string name="et_password">密码</string>
</resources>

textColor

文本颜色
1-2位表示透明度,3-4位表示红,5-6位表示绿,7-8位表示蓝

<TextView
	android:textColor="#FF00FF00">
</TextView>

colors.xml

自定义颜色集

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <color name="black">#FF000000</color>
  <color name="white">#FFFFFFFF</color>
  <color name="red">#FFFF0000</color>
  <color name="green">#FF00FF00</color>
  <color name="blue">#FF0000FF</color>
</resources>

根据标准最好是这样写

<TextView
	android:textColor="@color/red">
</TextView>

textStyle

文本字体风格
normal:普通
bold:加粗
italic:斜体

<TextView
	android:textStyle="bold">
</TextView>

textSize

文本字体大小
单位通常使用sp用来适配更多机型

<TextView
	android:textSize="20sp">
</TextView>

background

TextView背景颜色

<TextView
	android:background="@color/black">
</TextView>

gravity

内容布局
left:左上
right:右上
start:左上
end:右上
top:左上
fill:左中
bottom:左下
clip_vertical:左上
fill_vertical:左中
center_vertical:左中,垂直居中
clip_horizontal:左上
fill_horizontal:左上
center_horizontal:上中,水平居中
center:中中,水平垂直居中
缺少:右中右下中下

<TextView
	android:gravity="center">
</TextView>

shadow

字体阴影
shadowColor:阴影颜色
shadowRadius:模糊度
shadowDx:x轴漂移
shadowDy:y轴漂移

<TextView
	android:shadowColor="@color/black"
    android:shadowRadius="3"
    android:shadowDx="10"
    android:shadowDy="-5">
</TextView>

跑马灯效果

singleLine

文本内容单行显示
一行显示不出来的文字替换成"…"显示

ellipsize

文字省略号位置
marquee:跑马灯效果
start:"…“在开头
middle:”…“在中间
end:”…“在结尾
none:没有”…"

marqueeRepeatLimit

字幕动画重复的次数
marquee_forever:永远

focusable

focusableInTouchMode

获取焦点
text_view.xml

<?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:layout_width="match_parent"
                                                   android:layout_height="match_parent">

  <TextView
    android:text="@string/tv_one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_one"
    android:textColor="@color/red"
    android:textStyle="normal"
    android:textSize="20sp"
    android:background="@color/white"
    android:gravity="left"

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

    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:ignore="RtlHardcoded">
  </TextView>

</androidx.constraintlayout.widget.ConstraintLayout>
举报

相关推荐

0 条评论