Android中用TextView显示大量文字的方法

查了一下资料,找到了两种方法实现:

1. 只用TextView,用TextView自带的滚动条完成全部展示,在布局xml文件中,TextView的属性需要设置android:scrollbars和android:singleLine,如下:

复制代码
  <TextView
android:id="@+id/news_item_content_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:lineSpacingExtra="2dp" android:scrollbars="vertical" android:singleLine="false" android:text="this is content, blablabla..." android:textColor="@color/news_content_color" />
复制代码

主要是黄色的;然后在Activity的onCreate或者FragMent的onViewCreated方法中添加代码如下:

    TextView contentTV = (TextView) view.findViewById(R.id.news_item_content_text_view);
    contentTV.setMovementMethod(ScrollingMovementMethod.getInstance());

到这里就基本OK了,不过因为在我的Demo中,是在一个Activity中显示了两个Fragment,左边的是新闻列表,右边展示新闻详情,然后出现了一个问题:显示了一个比较长的新闻,然后把新闻内容拖到最后,在切换新闻条目后,展示新闻内容的TextView无内容显示,需要触摸一下TextView区域才能显示,处理办法:

在每次切换新闻后,都在TextView的setText方法后面添加一个TextView的滚动条滚动的方法,如下:

    TextView contentTV = (TextView) view.findViewById(R.id.news_item_content_text_view);
    contentTV.setText(content);
    contentTV.scrollTo(0, 0);//滚动条滚动到0位置

这样子就OK了。

2. 布局的时候把TextView放在一个ScrollView里面,这样子就更简单了,不需要任何代码处理。

复制代码
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <TextView
                android:id="@+id/news_item_content_text_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:lineSpacingExtra="2dp"
                android:text="this is content, blablabla..."
                android:textColor="@color/news_content_color" />
        </ScrollView>
复制代码

当TextView文字内容很长的时候,ScrollView自动会显示滚动条,不需要我们再去写代码实现了。

### 回答1: 要在Android Studio中使用TextView显示文本,可以按照以下步骤操作: 1. 在布局文件中添加TextView组件,例如: ``` <TextView android:id="@+id/my_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> ``` 2. 在Java代码中获取TextView组件,并设置文本内容,例如: ``` TextView myTextView = findViewById(R.id.my_textview); myTextView.setText("Hello Android Studio!"); ``` 这样就可以在TextView显示文本了。 ### 回答2: Android Studio 是一个非常流行的 Android 应用程序开发集成工具,可用于创建各种 Android 应用程序。TextViewAndroid 应用程序中最常用的UI 控件之一,用于在应用程序中显示文本。在 Android Studio 中使用 TextView 显示文本非常简单。以下是一些简单的步骤,可用于使用 TextView 显示文本: 1. 首先,在您的 Android 应用程序项目中的布局文件(例如 activity_main.xml)中创建 TextView 。 <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World!" /> 此代码将在您的应用程序上创建一个 TextView,ID 为 textView。您可以更改 ID 和其他属性,以满足您的要求。 2. 接下来,在您的 MainActivity.java 文件中获取对 TextView 的引用。 TextView textView = findViewById(R.id.textView); 此代码将帮助您获取 ID 为 textViewTextView 的引用。 3. 现在,您可以使用 setText() 方法TextView 中设置文本。 textView.setText("Hello World!"); 此代码将在您的 TextView 中设置文本为“Hello World!” 通过这些简单的步骤,您就可以在 Android 应用程序中使用 TextView 显示文本。您可以将这些代码用作起点,并根据您的要求进行修改以满足您的应用程序需求。此外,Android Studio 中还有许多其他的 UI 控件和功能可供使用,可帮助您创建出色的 Android 应用程序。 ### 回答3: 在Android Studio中,TextView是一个非常常用的控件,用于显示文本内容。它可以显示单行或多行文本,也可以设置文本样式、字体大小、颜色等各种属性。下面就详细讲解如何在Android Studio中使用TextView进行文本显示。 首先,在xml布局文件中添加TextView控件,可以通过拖拽的方式添加或手动编写代码,如下所示: ```xml <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World!" android:textSize="20sp" android:textStyle="bold" android:textColor="@color/colorPrimaryDark" /> ``` 上述代码定义了一个id为text_view的TextView控件,布局宽度为match_parent(占满父容器),高度为wrap_content(自适应内容),文本内容为“Hello World!”,字体大小为20sp,字体样式为粗体,字体颜色为颜色值为@color/colorPrimaryDark。可以根据需要修改其中的属性。 接下来,可以在相应的Java类中通过findViewById方法来获取到这个TextView控件,并对它进行一系列操作。例如: ```java TextView textView = findViewById(R.id.text_view); textView.setText("Hello Android Studio!"); textView.setTextSize(24); textView.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC); textView.setTextColor(getResources().getColor(android.R.color.holo_red_dark)); ``` 上述代码获取到id为text_view的TextView控件,然后将其文本内容设置为“Hello Android Studio!”、字体大小为24sp、字体样式为衬线字体的加粗斜体、字体颜色为系统主题色中的深红色。还可以根据需要设置其他属性,如对齐方式、边框等。 以上就是Android Studio中使用TextView进行文本显示的基本方法,简单易懂。可以通过不断实践和自学来掌握更多的控件使用方法开发出高质量的Android应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值