我是靠谱客的博主 淡淡铃铛,这篇文章主要介绍Android 中 TextView 的基本使用,现在分享给大家,希望可以做个参考。

Android 中 TextView 的基本使用

    • 1. 文字大小, 颜色
    • 2. 显示不下的使用
    • 3. 文字 + icon
    • 4. 中划线, 下划线
    • 5. 跑马灯

设置一个 Button 跳转另一个页面.

在这里插入图片描述

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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:padding="15dp" tools:context=".MainActivity"> <Button android:id="@+id/but_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/textView" /> </LinearLayout>

新建立一个新的 Activity.

在这里插入图片描述在这里插入图片描述

获取按钮实现跳转. 在 MainActivity 文件内添加内容.

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.example.hello; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { // 生命按钮 private Button but1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 得到按钮 but1 = findViewById(R.id.but_1); // 设置点击监听 but1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 跳转到 TextView 演示界面 Intent intent = new Intent(MainActivity.this, TextViewActivity.class); startActivity(intent); } }); } }

1. 文字大小, 颜色

在这里插入图片描述

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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" android:padding="15dp" tools:context=".TextViewActivity"> <TextView android:id="@+id/tv_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/textView" android:textColor="#f0f" android:textSize="45sp" /> </LinearLayout>

手机显示

2. 显示不下的使用

在这里插入图片描述

复制代码
1
2
3
4
5
6
7
8
9
10
<TextView android:id="@+id/tv_2" android:layout_width="160dp" android:layout_height="wrap_content" android:maxLines="1" android:ellipsize="end" android:text="@string/textView" android:textColor="#f00" android:textSize="45sp" />

3. 文字 + icon

在这里插入图片描述

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<TextView android:id="@+id/tv_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines="1" android:ellipsize="end" android:text="@string/textViewLogo" android:textColor="#000" android:drawableEnd="@drawable/ic_launcher" android:drawableStart="@drawable/ic_launcher" android:drawableBottom="@drawable/ic_launcher" android:drawableTop="@drawable/ic_launcher" android:textSize="45sp" />

4. 中划线, 下划线

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<TextView android:id="@+id/tv_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000" android:textSize="45sp" /> <TextView android:id="@+id/tv_5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000" android:textSize="45sp" />

在 TextViewActivity 文件中设置.

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.example.hello; import androidx.appcompat.app.AppCompatActivity; import android.graphics.Paint; import android.os.Bundle; import android.widget.TextView; public class TextViewActivity extends AppCompatActivity { // 声明 private TextView tv4; private TextView tv5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_text_view); // 获取 TextView tv4 = findViewById(R.id.tv_4); // 添加内容 tv4.setText("中划线"); // 设置中划线 tv4.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); // 去除中划线锯齿 tv4.getPaint().setAntiAlias(true); // 获取 TextView tv5 = findViewById(R.id.tv_5); // 添加内容 tv4.setText("下划线"); // 设置下划线 tv5.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); } }

在这里插入图片描述

5. 跑马灯

复制代码
1
2
3
4
5
6
7
8
9
10
<TextView android:id="@+id/tv_6" android:layout_width="200dp" android:layout_height="wrap_content" android:textColor="#000" android:textSize="45sp" android:singleLine="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever"/>
复制代码
1
2
3
4
5
6
7
// 获取 TextView tv6 = findViewById(R.id.tv_6); // 添加内容 tv6.setText("走马观花走马观花走马观花走马观花走马观花走马观花走马观花走马观花走马观花走马观花"); // 设置聚焦 tv6.setSelected(true);

在这里插入图片描述

最后

以上就是淡淡铃铛最近收集整理的关于Android 中 TextView 的基本使用的全部内容,更多相关Android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部