Android 文本视图TextView

墨蓝 2022-06-05 02:22 344阅读 0赞

TextView想必大家都很熟悉了,不管在哪门语言中,文本显示是最为基础的。TextView的基本属性有很多,我就不一一介绍了。这里我们直接通过几个例子来看下TextView的使用。

1.TextView跑马灯效果,这个示例是实现文字滚动效果。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context="com.easygoing.androidtextview.MainActivity">
  9. <com.easygoing.androidtextview.ScrollingTextView
  10. android:id="@+id/tv_marquee"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:layout_marginTop="30dp"
  14. android:singleLine="true"
  15. android:ellipsize="marquee"
  16. android:focusable="true"
  17. android:clickable="true"
  18. android:focusableInTouchMode="true"
  19. android:textSize="18sp"
  20. android:marqueeRepeatLimit="10"
  21. android:text="静静绽放 吐露芬芳 无论什么境况 静静绽放吐露芬芳"
  22. />
  23. </LinearLayout>

这里我简单自定义了一个TextView,让TextView一直拥有焦点,从而使跑马灯效果不停止。

  1. package com.easygoing.androidtextview;
  2. import android.content.Context;
  3. import android.graphics.Rect;
  4. import android.util.AttributeSet;
  5. import android.widget.TextView;
  6. import org.jetbrains.annotations.Nullable;
  7. /**
  8. * Created by Lenovo on 2017/11/15.
  9. */
  10. public class ScrollingTextView extends TextView {
  11. private Context context;
  12. public ScrollingTextView(Context context) {
  13. super(context);
  14. }
  15. public ScrollingTextView(Context context, @Nullable AttributeSet attrs) {
  16. super(context, attrs);
  17. }
  18. public ScrollingTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  19. super(context, attrs, defStyleAttr);
  20. this.context = context;
  21. }
  22. @Override
  23. protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
  24. if(focused)
  25. super.onFocusChanged(focused, direction, previouslyFocusedRect);
  26. }
  27. @Override
  28. public void onWindowFocusChanged(boolean hasWindowFocus) {
  29. if(hasWindowFocus)
  30. super.onWindowFocusChanged(hasWindowFocus);
  31. }
  32. @Override
  33. public boolean isFocused() {
  34. return true;
  35. }
  36. }

让我们看下效果图:

Center

其中这几个属性是必须要设置的:

  1. android:singleLine="true" //指定文本单行显示 android:ellipsize="marquee"//指定文本超出范围后的省略方式--start:省略号在开头 middle:省略号在中间 end:省略号在末尾 marquee:跑马灯显示 android:focusable="true"//指定是否获得焦点,跑马灯效果必须为true android:focusableInTouchMode="true"//指定在触摸时获得焦点,跑马灯效果必须为true
  2. 2.TextView搜索框
  3. 这里我们简单实现一个搜索框,它有默认的背景色,以及按下的背景色。实例如下图:

Center 1

Center 2

代码也很简单:

Center 3

当然光有布局是不行的,下面我将selector的内容放出来。

Center 4

另外两个就比较简单了,只是设置下按下时候的背景或文字颜色。

Center 5

Center 6

好了,TextView就到这里了。

发表评论

表情:
评论列表 (有 0 条评论,344人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android 文本视图TextView

    TextView想必大家都很熟悉了,不管在哪门语言中,文本显示是最为基础的。TextView的基本属性有很多,我就不一一介绍了。这里我们直接通过几个例子来看下TextView的