Android开发 UI控件-----TextView文本框

 常用属性 

  • id:Java或使用RelativeLayout时,依靠id选择该组件
  • layout_width:组件的宽度,一般有以下可选项:

                wrap_content:控件显示的内容多大,控件就多大,
                match_parent(fill_parent):填满该控件所在的父容器;
                特定的大小,可选单位dp、px、pt......

  • layout_height:组件的高度,内容同上。
  • gravity:控件中文字的对齐方向,常用可选值有:

                top、bottom、left、right、center、fill、center_vertical.........

  • text:设置显示的文本内容,一般通过@String/xxx引用string.xml文件中的资源
  • textColor:设置字体颜色,一般通过@Colors/xxx引用colors.xml文件中的资源
  • textStyle:设置字体风格,可选值:normal(无效果),bold(加粗),italic(斜体)
  • textSize:字体大小,单位一般是用sp!
  • background:控件的背景颜色,可以理解为填充整个控件的颜色,可以是图片!

示例1

在LinearLayout容器中实现TextView控件常用属性的测试

XML中设置属性

新建一个空项目,修改activity_main.xml,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:gravity="center_vertical"
        android:text="Hello world!!!"
        android:textColor="@color/purple_200"
        android:textStyle="italic"
        android:textSize="30sp"
        android:background="#FFFF000000"
        />

</LinearLayout>

效果呈现

 Java代码中设置属性

新建一个空项目,修改activity_main.xml,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text1"
        android:layout_width="200dp"
        android:layout_height="200dp"/>

</LinearLayout>

修改MainActivity.java的onCreate方法,如下:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView text1 = findViewById(R.id.text1);
        text1.setGravity(Gravity.CENTER_VERTICAL);
        text1.setText("Hello world!!!");
        text1.setTextColor(getResources().getColor(R.color.purple_200));
        text1.setTypeface(null, Typeface.ITALIC);;
        text1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        text1.setBackgroundColor(Color.rgb(255,0,0));
    }

最终呈现效果同上!!!

文字阴影相关属性

  • shadowColor:设置阴影颜色,需要与shadowRadius一起使用!
  • shadowRadius:设置阴影的模糊程度,设为0.1就变成字体颜色了,建议使用3.0
  • shadowDx:设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置
  • shadowDy:设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置 

文字滚动相关属性

  • singleLine:文字内容超过屏幕不换行
  • focusable:可否获取焦点
  • focusablelnTouchMode:可否在触摸模式下获取焦点
  • ellipsize:在哪里省略文本,设置为marquee时滚动
  • marqueeRepeatLimit:文字滚动重复次数

示例2

在LinearLayout容器中使用TextView实现带阴影的文字滚动

XML实现

 新建一个空项目,修改res->values->strings.xml文件,如下:

<resources>
    <string name="app_name">Demo_TextView</string>
    <string name="feihuas">啊吧啊吧,咕噜咕噜,呼哧呼哧,嘎巴嘎巴,嘿呦嘿呦,亚哈呀哈</string>
</resources>

修改activity_main.xml文件,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:orientation="vertical">

    <TextView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:textSize="30sp"
        android:text="@string/feihuas"
        android:gravity="center"
        android:background="@color/white"
        android:shadowColor="#FF00FFFF"
        android:shadowRadius="3.0"
        android:shadowDx="10.0"
        android:shadowDy="10.0"
        android:singleLine="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        >
        <requestFocus/>
    </TextView>

</LinearLayout>

运行项目即可呈现文字滚动效果,最终效果因设备而异,若文字无滚动,可以增加strings.xml中feihuas的内容后重试!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

全村姑娘我头最秃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值