• Android studio TextView的 用法详情


    TextView

    简介 :向用户显示文本,并可选择允许他们编辑文本。TextView是一个完整的文本编辑器,但是基类为不允许编辑;其子类EditText允许文本编辑。

    点击参考属性

    先上代码

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
      <TextView
          android:id="@+id/textView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="我是测试文字"
          android:textSize="30sp"
          />
    
    </LinearLayout>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    布局设置一个控件id,根据id,我们可以在Java代码中通过findViewById()的方法获取到该对象,然后进行相关属性的设置 (所有的方法都有id)

    public class MainActivity extends AppCompatActivity {
    
        private TextView text;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //初始化 通过自己定义的id 获取到id所在的控件,对其进行修改
            text=findViewById(R.id.textView);
            //把字体换成登录页面
            text.setText("登录页面");
            //给字体修改个颜色 Color.RED 这种写法是取系统默认存在的颜色值
            text.setTextColor(Color.RED);
            //修改字体的大小
            text.setTextSize(50);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 再次运行到手机上,效果图
      在这里插入图片描述
    • text.setTextColor() 也可以使用自定义的
    text.setTextColor(getResources().getColor(R.color.black));
    
    • 1

    这个时候你就可以用自定义的任何颜色值了(通过name ,找color对应的颜色值的)
    在这里插入图片描述

    • 以上是平常最常用的,当然也有更多的属性,这里没有一一列举,感兴趣的话可以自己试试,如有疑问欢迎留言谈论
  • 相关阅读:
    转置矩阵的性质
    RabbitMQ学习笔记之Work Queues
    sqlServer实现像Oracle 的listagg的效果
    CentOS MySQL安装及问题解决
    Spring系列十:Spring MVC深度学习
    ElasticSearch-数据查询
    C语言:一级指针访问二维数组
    操作系统—死锁
    js金额格式化,千分符,(好家伙!面试直接被问四次)
    重学设计模式(三、设计模式-解释器模式)
  • 原文地址:https://blog.csdn.net/afufufufu/article/details/126050339