Android 2-2 TextView文本框

TextView文本

🔧常用属性

名称解释格式
android:text要显示的文本字符串或@value
android:textSize文字大小sp
android:textColor文字颜色16进制或@color
android:maxLine最大行数数字
android:ellipse省略显示(固定宽度时)start/end/middle

🌲例子:实现以下效果:

  • 文字大小、颜色
  • 显示不下使用何种方式
  • 文字、图标
  • 下划线、删除线等风格
  • 轮播文字🔥

🏠主要代码(activity_helloworld…xml)
tv_1 默认
tv_2 溢出的文字自动隐藏
tv_3 溢出的文字以…表示
tv_4 删除线
tv_5 下划线
tv_6 文字轮播效果
tv_new 使用代码控制文字显示(优先级高)

<?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"
    tools:context=".HelloWorldActivity"
    android:orientation="vertical"
    >
    <TextView
        android:id="@+id/tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/txt_1"
        android:textColor="#000000"
        android:textSize="30sp"
        />
    <TextView
        android:id="@+id/tv_2"
        android:layout_width="170dp"
        android:layout_height="wrap_content"
        android:text="@string/txt_1"
        android:textColor="#38f"
        android:textSize="30sp"
        android:maxLines="1"
        />
    <TextView
        android:id="@+id/tv_3"
        android:layout_width="170dp"
        android:ellipsize="end"
        android:layout_height="wrap_content"
        android:text="@string/txt_1"
        android:textColor="#003300"
        android:textSize="30sp"
        android:maxLines="1"
        />
    <TextView
        android:id="@+id/tv_4"
        android:layout_width="170dp"
        android:ellipsize="end"
        android:layout_height="wrap_content"
        android:text="@string/txt_1"
        android:textColor="#000000"
        android:textSize="30sp"
        android:maxLines="1"
        />
    <TextView
        android:id="@+id/tv_5"
        android:layout_width="170dp"
        android:ellipsize="end"
        android:layout_height="wrap_content"
        android:text="@string/txt_1"
        android:textColor="#000000"
        android:textSize="30sp"
        android:maxLines="1"
        />
    <TextView
        android:id="@+id/tv_6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/txt_1_1"
        android:textColor="#000000"
        android:textSize="30sp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        />
    <TextView
        android:id="@+id/tv_new"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/txt_1"
        android:textColor="#000000"
        android:textSize="30sp"
        android:maxLines="1"
        />
    <TextView
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="28sp"
        android:text="查看详情"
        android:textColor="#000000"
        android:drawableRight="@drawable/icon_arrow_off_32"
        android:drawablePadding="5dp"
        />
</LinearLayout>

其中,下划线和删除线使用java代码控制,在该xml对应的helloworld_activity.java中

package pers.yuri.androidfundamental;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
public class HelloWorldActivity extends AppCompatActivity {
    private TextView mTv4, mTv5, mTvNew;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello_world);
        mTv4 = findViewById(R.id.tv_4);
        mTv4.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);    //设置删除线
        mTv4.getPaint().setAntiAlias(true); //消除锯齿
        mTv5 = findViewById(R.id.tv_5);
        mTv5.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //设置下划线
        mTvNew = findViewById(R.id.tv_new);
        mTvNew.setText(Html.fromHtml("<u>月下门推,心细如你脚步碎</u>"));
    }
}

效果:

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值