Android-所有屏幕大小的按钮文本大小

Android-所有屏幕大小的按钮文本大小,android,android-button,Android,Android Button,我正在开发一个android应用程序,在屏幕顶部有3个按钮,我使用layout\u weight属性定义了它们。按钮可以调整到所有屏幕大小,包括平板电脑,但其文本不能 这就是nexus 5上的外观: <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_gravity="center_vertical" android:layo

我正在开发一个android应用程序,在屏幕顶部有3个按钮,我使用
layout\u weight
属性定义了它们。按钮可以调整到所有屏幕大小,包括平板电脑,但其文本不能

这就是nexus 5上的外观:

<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>

这就是nexus 10上的外观:

<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>

看到区别了吗?
在Nexus5上,它非常清晰,但在Nexus10上,你几乎看不到任何东西

这是我的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>


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

请帮助我的代码,因为我几乎是一个初学者


提前谢谢

您可以在sp中设置按钮文本大小(即:24sp)

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

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


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

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

你不能在一个单一的布局中为手机和平板电脑构建一个(合适的)用户界面,因为平板电脑上显示的信息应该比手机上显示的信息更多。尽管您可以重复使用大部分布局。
您可能希望阅读Android开发者网站上的培训。

可能重复@StarsSky我希望避免创建不同的文件夹,并正在寻找一种解决方案,以创建一个支持所有屏幕大小的单一布局。如果你有解决办法,请帮助我。谢谢不同的布局文件夹已经被引入,以满足您的要求是的,我知道,但这是我想要避免的。我只需要一个布局,将自动调整到所有屏幕大小。我已经用“布局权重”完成了一半,但我被按钮的文本大小卡住了。如果你有解决办法,请帮助我。谢谢尝试在按钮中使用sp中的textsite。实际上,我的布局中有119个按钮,24sp对它们来说太大了。我的意思是,在xhdpi屏幕上,您的解决方案运行良好,但在小型设备上,它太大了。请帮我解决这个问题。谢谢要在xhdpi屏幕上获得24 sp,初始值应为12 sp。但它非常小,在ldpi屏幕上会变成10sp。无论如何,我怀疑你能在这么小的屏幕上安装119个按钮。无论如何,您可以使用dimens解决方案设置自定义尺寸,该尺寸因每个分辨率而异。确定将使用尺寸。我是初学者,你能给我指点一下吗?谢谢比例大小是多少?尺寸单位是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屏幕,我以前读过。基本上,我想做的是使按钮的文本大小自动适应大屏幕。