首页 > 其他分享 >Android Studio 设置文字大小和颜色

Android Studio 设置文字大小和颜色

时间:2023-02-25 15:23:56浏览次数:33  
标签:code 文字大小 tv Studio 设置 import Android id TextView

设置文字大小

分为两种方式

1、在xml中进行文字的设置,再在java文件中进行获取显示

<?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:orientation="vertical"

    >


    <TextView
        android:id="@+id/tv_px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text = "@string/hello"
        android:textSize="30px"/>

        />
    <TextView
        android:id="@+id/tv_dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text = "@string/hello"
        android:textSize="30dp"/>

    />
    <TextView
        android:id="@+id/tv_sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text = "@string/hello"
        android:textSize="30sp"/>

    />
</LinearLayout>

这里用了不同的单位

2、直接在java中进行设置

package com.example.chapter03;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class TextSizeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_size);
       TextView tv_hello = findViewById(R.id.tv_px);
        tv_hello.setTextSize(30);
    }
}

 

文字颜色的设置

<?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:orientation="vertical">


    <TextView
        android:id="@+id/tv_code_system"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text = "@string/color"
        android:textSize="17sp"/>
    <TextView
        android:id="@+id/tv_code_eight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text = "@string/color8"
        android:textSize="17sp"/>
    <TextView
        android:id="@+id/tv_code_six"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text = "@string/color6"
        android:textSize="17sp"/>
    <TextView
        android:id="@+id/tv_xml"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text = "@string/colorx"
        android:textSize="17sp"
        android:textColor="#00ff00"/>
    <TextView
        android:id="@+id/tv_values"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text = "@string/colorv"
        android:textSize="17sp"
        android:textColor="@color/green"/>
    <TextView
        android:id="@+id/tv_code_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text = "@string/background"
        android:textSize="17sp"/>
        <!--android:background="@color/green:-->
</LinearLayout>
package com.example.chapter03;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

public class TextColorActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_color);
        TextView tv_code_system = findViewById(R.id.tv_code_system);
        tv_code_system.setTextColor(Color.GREEN);

        TextView tv_code_system2 = findViewById(R.id.tv_code_eight);
        tv_code_system2.setTextColor(0xff00ff00);

        TextView tv_code_system3 = findViewById(R.id.tv_code_six);
        tv_code_system3.setTextColor(0x00ff00);

        TextView tv_code_background=findViewById(R.id.tv_code_background);
        //tv_code_background.setBackgroundColor(Color.GREEN);


        tv_code_background.setBackgroundResource(R.color.green);

    }
}

用了很多不同的方式

从上到下依次是

<resources>
    <string name="app_name">chapter03</string>
    <string name="hello">你好,世界</string>
    <string name="color">代码设置系统自带的颜色</string>
    <string name="color8">代码设置八位文字颜色</string>
    <string name="color6">代码设置六位文字颜色</string>
    <string name="colorx">布局文件设置六位文字颜色</string>
    <string name="colorv">资源文件引用六位文字颜色</string>
    <string name="background">背景设置为绿色</string>
</resources>

 

标签:code,文字大小,tv,Studio,设置,import,Android,id,TextView
From: https://www.cnblogs.com/Arkiya/p/17154470.html

相关文章

  • Ubuntu 16.04上安装openjdk7--编译Android6.0使用
    问题背景:需要在编译服务器上编译android6.0代码。这个版本需要使用openjdk7。参考来自官网的信息:https://source.android.com/docs/setup/start/older-versions?hl=zh-c......
  • Android Studio上运行Flutter,报错could not find io.flutter:flutter_embedding_debug
    AndroidStudio4.2.0,Flutter2.2.3Dart2.13.4下载​​io.flutter.zip​​......
  • Android 基础知识4-2.10 GridLayout(网格布局)详解
    一、GridLayout(网格布局)概述        GridLayout布局是Android4.0以后引入的新布局,和TableLayout(表格布局)有点类似,不过它功能更多,也更加好用,最大的特点是放......
  • Android 基础知识4-2.9 FrameLayout(帧布局)详解
    一、FrameLayout(帧布局)概述        FrameLayout又称作帧布局,它相比于LinearLayout和RelativeLayout要简单很多,因为它的应用场景也少了很多。这种布局没有方便的定位......
  • Android 基础知识4-3.1 TextView(文本框)详解
    一、前言    TextView就是一个显示文本标签的控件,就是用来显示文本。可以在代码或者XML中设置字体,字体大小,字体颜色,字体样式(加粗级斜体),文字截断(比如:只显示10个字,......
  • 上班第一天 Android 环境配置
    其实是昨天把大概回归Android开发第一天学会查然后等待反正我是不希望以后再查了写出来吧去谷歌那边把androidstudio下载下来更新jdk版本(与传统的java开发不同......
  • Android笔记--FileProvider
    FileProvider介绍继承于ContentProvider,本质上依旧是用于跨境通信,对第三方应用暴露文件,并授予文件读写地权限具体内容1、在Strings.xml里面配置一个常量2、在Manifest......
  • Android笔记--通过MediaStore查询图片
    相关描述已经完成发送彩信功能之后,就来继续向前走一步,来到MediaStore查询图片界面啦!具体步骤实现1、简简单单地一个界面<?xmlversion="1.0"encoding="utf-8"?><Line......
  • Android Studio 设置文本内容
    首先在Androidstudio中设置文本内容有两种方式1、在XML文件中通过属性android:text设置文本2、在java代码中调用文本视图的对象的setText方法设置文本具体如下1、......
  • Android 观察App运行日志和面对安装工程中需要在
    Android采用Log工具打印日志,它将各类日志划分为五个等级;log.e表示错误信息,比如可能导致程序崩溃的异常log.w表示警告信息log.i表示一般消息log.d表示调试信息。可把程......