安卓开发从0到1(基础知识5-设置文本的大小)

安卓开发从0到1(基础知识5-设置文本的大小)

  1. 在java代码中调用setTextSize方法,即指定文本大小
  2. 在xml文件中通过属性android:textSize指定文本大小,需要指定文本字号单位。
  • 在Android开发中,“px”(Pixels)是屏幕上的物理像素点。它表示屏幕的实际显示单元。每个px对应着设备屏幕上的一个物理像素点。
  • “dp”(Density-independent Pixels)被称为密度无关像素或设备独立像素。它是一种抽象的单位,用于在不同密度的设备上以一致的方式定义尺寸。Android系统会根据设备的屏幕密度自动缩放dp值,以确保在不同设备上获得一致的视觉效果。
  • “dpi”(Dots Per Inch)表示每英寸的点数。它指的是屏幕上每英寸的像素密度。较高的dpi意味着更多的像素被压缩在相同的物理空间上,从而使图像和文本显示更加清晰和细致。
  • “density dpi” 表示屏幕的像素密度,即每英寸的像素数。它用于确定设备的屏幕密度级别,如低密度 (ldpi)、中密度 (mdpi)、高密度 (hdpi)、很高密度 (xhdpi)、超高密度 (xxhdpi)和超超高密度 (xxxhdpi) 等。这些密度级别决定了Android系统如何将dp单位转换为实际像素值。
  • “dpi/dp” 是dpi与dp之间的换算关系,具体转换公式为:px = dp * (dpi / 160)。其中160是基准密度(mdpi)的屏幕密度,这个比例可以将dp值转换为具体设备上的像素值。
  • 在Android开发中,建议使用dp作为尺寸单位,以确保在不同分辨率和屏幕密度的设备上获得一致的布局和用户体验。由于dp是与密度无关的抽象单位,使得应用程序更适应不同尺寸的设备屏幕。通过考虑dpi和density dpi,可以在不同设备上实现尺寸的灵活调整与适配。

第一步创建Activity:SetSizeActivity.java

package com.example.module1;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;
//@author:lcy
//@date:2023.6.24
//@description:set different textSize
public class SetSizeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_size);
//          TextView set_text= findViewById(R.id.text_px);
//        set_text.setTextSize(30);
    }
}

第二步在activity_set_size.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:orientation="vertical">
    <TextView
        android:id="@+id/text_dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textSize="30px"
        />

    <TextView
        android:id="@+id/text_sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textSize="30dp"
        />
    <TextView
        android:id="@+id/text_px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textSize="30sp"
        />

</LinearLayout>

第三步在AndroidManifest.xml中:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication">
        <activity
            android:name=".SetSizeActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

运行后的结果

在这里插入图片描述

说明:当手机的font-size发生变化时,dp和px是不变的,java代码中setTextsize属性默认的单位是sp,所以官方推荐sp。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Lihua总是问我为什么

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

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

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

打赏作者

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

抵扣说明:

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

余额充值