Android控件(TextView)

使用java的MainActivity

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

布局文件xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

效果

在这里插入图片描述

xml TextView 属性介绍

属性解释
android:layout_width宽度wrap_content 为自适应,match_parent 为铺满屏幕 也可以设置固定大小,如 10dp10px
android:layout_height高度同上
android:text内容任意字符串
android:drawableLeft左侧显示指定图片在这里插入图片描述
android:drawableRight右侧显示指定图片在这里插入图片描述
android:drawableTop上面显示指定图片在这里插入图片描述
android:drawableBottom下边显示指定图片在这里插入图片描述
android:fontFamily字体在res下新建font 文件夹,放入字体资源,使用"@font/jetbrainsmono_bold" 即可
android:textStyle字体类型bold 加粗 normal 正常 italic 斜体
android:textColor字体颜色颜色值 , #ffffff 为白色,也可以写为#fff,
android:idID实例化控件使用
android:visibility是否显示在界面gone 隐藏不占位,visible 显示 ,invisible 隐藏,但是占位

代码操作


import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.service.autofill.TextValueSanitizer;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView textView;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 实例化控件
        textView = findViewById(R.id.textView);
        // 设置内容
        textView.setText("Hello TextView !");
        // 设置颜色
        textView.setTextColor(Color.parseColor("#fff333"));
        // 是否显示
        // View.VISIBLE 显示
        // View.GONE 隐藏 不占位
        // View.INVISIBLE 隐藏,占位
        textView.setVisibility(View.VISIBLE);
    }
}

设置html

   textVuew.setText(Html.fromHtml("<u>Hello</u>"));
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值