android - 如何使文本大小适应 Android 中不同大小的设备/线性布局

标签 android screen-size text-size

我目前正在研究一个布局文件,它可以适应不同尺寸的安卓设备。到目前为止,我已经取得了一些成功。使用“layout_weight”我已经能够制作一个线性布局来占据布局中一定比例的空间。这意味着在 2'7 英寸屏幕或 10'1 英寸屏幕上,显示器可以拉伸(stretch)以覆盖整个屏幕。 但是,我在顶部的线性布局中遇到文本大小问题。如何使 TextSize 适应大型或小型设备? 这是我的代码。

 <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:baselineAligned="false"
          android:weightSum="1.0"
          android:background="#ff0000"
          >
      <LinearLayout
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="0dip"
          android:layout_weight="0.25"
          android:weightSum="1.0">

     <TextView
        android:id="@+id/textView1"

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="medium"
        android:textSize="30sp"
        android:layout_weight="1"

        android:textAppearance="?android:attr/textAppearanceLarge" />

     </LinearLayout>

     <LinearLayout
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="0dip"
          android:layout_weight="0.25"
          android:weightSum="1.0">        
         <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_1"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
        />

        <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_2"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
        />

        <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_3"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
        />

        <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_4"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
        />
     </LinearLayout>

     <LinearLayout
         android:orientation="horizontal"
         android:layout_width="fill_parent"
         android:layout_height="0dip"
         android:layout_weight="0.25"
         android:weightSum="1.0">        
           <ImageView 
               android:layout_width="0dip"
               android:layout_height="fill_parent"
               android:src="@drawable/sample_1"
               android:layout_gravity="center_horizontal"
               android:layout_weight="0.25"
           />

        <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_2"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
        />

        <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_3"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
        />

        <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_4"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
        />
     </LinearLayout>
     <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="0dip"
      android:layout_weight="0.25"
      android:weightSum="1.0">        
        <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_1"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
        />

        <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_2"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
        />

        <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_3"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
        />

        <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_4"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
               />
      </LinearLayout>
      </LinearLayout>

最佳答案

在 res 文件夹中创建不同的文件夹,例如 values-ldpi、values-mdpi、values-hdpi、values-xhdpi

在这些文件夹里面创建 dimens.xml 文件

应该是这样的

res/values-ldpi/dimens.xml
res/values-mdpi/dimens.xml
res/values-hdpi/dimens.xml

为您的 TextView inside dimens.xml 定义不同的大小

关于android - 如何使文本大小适应 Android 中不同大小的设备/线性布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17831721/

相关文章:

android - 谷歌应用内购买取消用户订阅

android - 为所有屏幕提供图像?

text-size - 使用 asciidoc 时,有什么方法可以更改特定 block 的文本大小(字体大小)?

java - Android 中的外部 JAR 错误

android - Firebase session 失效

Android onClick 函数用于编辑文本,如何调用编辑文本?

ios - 如何让 iOS 应用程序在 iPhone 5、iPhone 6 和 iPhone 6+ 上看起来一样

ios - 纵横比错误的 iTunes Connect 屏幕截图

android - 在不同设备上制作相同大小的文本

ios - 有没有更快的方法来更改iOS模拟器上的辅助功能(动态类型)文本大小?