【问题标题】:Android - Button text size for all screen sizesAndroid - 所有屏幕尺寸的按钮文本大小
【发布时间】:2014-01-19 21:35:01
【问题描述】:

我正在开发一个 android 应用程序,我在屏幕顶部有 3 个按钮,我使用 layout_weight 属性定义了它们。按钮可适应所有屏幕尺寸,包括平板电脑,但其文本不会。

这是 nexus 5 上的样子:

这是 nexus 10 上的样子:

看到区别了吗?
在 nexus 5 上,它非常清晰,但在 nexus 10 上,您几乎看不到任何东西!

这是我的 XML:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center_vertical"
    android:layout_weight="1"
    android:background="#000000"
    android:orientation="horizontal"
    android:weightSum="3" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="Button 1"
        android:layout_weight="1"
        android:id="@+id/button" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="Button 2"
        android:layout_weight="1"
        android:id="@+id/button2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="Button 3"
        android:layout_weight="1"
        android:id="@+id/button3" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center_vertical"
    android:background="#000000"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:weightSum="3" >

</LinearLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center_vertical"
    android:layout_weight="1"
    android:background="#000000"
    android:orientation="horizontal"
    android:weightSum="3" >

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center_vertical"
    android:layout_weight="1"
    android:background="#000000"
    android:orientation="horizontal"
    android:weightSum="3" >

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center_vertical"
    android:layout_weight="1"
    android:background="#000000"
    android:orientation="horizontal"
    android:weightSum="3" >

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center_vertical"
    android:layout_weight="1"
    android:background="#000000"
    android:orientation="horizontal"
    android:weightSum="3" >

</LinearLayout>

那么如何让按钮文本大小在不同的屏幕尺寸上自动调整,而不必为不同的屏幕尺寸制作不同的布局?

请帮我写代码,因为我几乎是个初学者!

提前致谢!

【问题讨论】:

  • @StarsSky 我想避免制作不同的文件夹,并正在寻找一种解决方案来制作支持所有屏幕尺寸的单一布局。如果您有解决方案,请帮助我。谢谢!
  • 引入了不同的布局文件夹以满足您的要求
  • 是的,我知道,但这就是我想要避免的。我只想要一种可以自动适应所有屏幕尺寸的布局。我通过使用“layout_weight”完成了一半,但我被按钮的文本大小困住了。如果您有解决方案,请帮助我。谢谢!
  • 尝试在你的按钮中使用 textsite in sp。

标签: android android-button


【解决方案1】:

您目前正在尝试扩展整个应用程序,而不是构建合适的平板电脑 UI。您将按钮的宽度设置为屏幕的 1/3,这是按预期工作的。您没有更改按钮的 textSize,因此 Android 选择了默认的 textSize。默认的 textSizes 是一个特定的大小,与 View 的尺寸无关。您不能(也不想)在 4 英寸手机和 10 英寸平板电脑上扩展完整的 UI。

您无法在一个布局中同时为手机和平板电脑构建(正确的)用户界面,因为您应该在平板电脑上显示比在手机上更多的信息。尽管您可以重复使用大部分布局。 您可能想阅读 Android 开发者网站上的 Supporting Different Screen Sizes 培训。

【讨论】:

  • 我以前读过。基本上我想做的是让按钮的文本大小自动适应大屏幕。
【解决方案2】:

您可以在 sp 中设置按钮 textSize(即:24sp)。

请记住,此值是指 mdpi 屏幕,它将根据屏幕分辨率进行放大或缩小。

因此,在 ldpi 屏幕上它会缩小到 18sp,而在 xhdpi 屏幕上它会放大到 48sp。

除此之外,您可以在您的 values 文件夹中的 dimens.xml 文件中定义一个维度值(但您必须创建多个值文件夹,一个用于您将支持的每种分辨率)。

【讨论】:

  • 实际上我的布局中有 119 个按钮,而 24sp 对它们来说太大了。我的意思是在 xhdpi 屏幕上,您的解决方案运行良好,但在小型设备上,它太大了。请帮我解决这个问题。谢谢!
  • 要在 xhdpi 屏幕上获得 24 sp,您的初始值应该是 12sp。但这很小,在 ldpi 屏幕上它会变成 10sp。无论如何,我怀疑你能在这么小的屏幕上安装 119 个按钮。无论如何,您可以使用dimens 解决方案来设置一个自定义维度,该维度因每个分辨率而异。
  • OK 将使用尺寸。你能指导我吗,因为我是初学者?谢谢!
  • 尺寸比例是多少?
  • 嗯,尺寸以 px 为单位,分辨率以 dpi 为单位。相应地设置这些值(即:具有 mdpi 屏幕的普通手机屏幕通常是 320*480 @ 160 dpi 纵向和 480*320 @ 160 dpi 横向)。对于 ldpi 手机屏幕,通常具有以下尺寸:纵向:240*320 @ 120dpi 和横向:320*240 @ 120dpi。然后,尺寸在 res/values-resolution/dimens.xml 文件中设置,如参考链接。设置后,在您的 xml 或代码中引用这些尺寸,您就完成了。相当直截了当。我会说 12sp 的字体大小适合 ldpi 屏幕
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-20
  • 1970-01-01
  • 2014-02-08
  • 2021-05-10
相关资源
最近更新 更多