gpt4 book ai didi

android - 按钮的大小取决于屏幕分辨率

转载 作者:太空宇宙 更新时间:2023-11-03 13:19:32 26 4
gpt4 key购买 nike

我有带有 6 个按钮的 LinearLayout。在 4.7 英寸的分辨率下,按钮几乎是完美的。但在分辨率较高的设备上,它们不会占据整个屏幕,而在分辨率较低的设备上,并非所有按钮都可见。您能否解释一下如何使按钮在屏幕分辨率不同的设备上显示相同?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/mainbackground">

<TextView
android:id="@+id/tvBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_company"
android:layout_marginLeft="30dp"
android:textColor="#002060"
android:layout_marginTop="20dp"
android:textSize="15sp"
>
</TextView>
<Button
android:id="@+id/button_operations"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/gradient_blue"
android:drawableLeft="@drawable/ic_purchase"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:gravity="left|center"
android:text=" oper1"
android:layout_marginTop="15dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:textColor="#001B51"
android:textSize="30sp"
android:textStyle="bold"
/>

....... //4 more buttons

<Button
android:id="@+id/button_exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/gradient_blue"
android:paddingLeft="10dp"
android:drawableLeft="@drawable/ic_exit"
android:gravity="left|center"
android:text=" Exit"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:textColor="#001B51"
android:textSize="30sp"
android:textStyle="bold"/>
</LinearLayout>

最佳答案

如果你想让每个人在第一个TextView之后占据剩余高度的相等部分, 尝试使用 LinearLayout's layout_weight属性并设置每个 Button 的高度至 fill_parent像这样:

<Button
android:id="@+id/button_exit"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
... />

如果每个Button具有相等的 layout_weight,它们将各自填充可用空间的相同部分。自 TextView未加权,它将首先占用所需的空间,加权按钮将一起填充剩余空间。

您可能还想根据屏幕的密度指定不同的文本大小(并且可能还使用样式来减少所有 Button's 布局参数的重复),但这有点超出了您最初问题的范围。

编辑:在关于如何使用不同资源为不同密度提供不同文本大小的评论中回答您的问题......

进入values/dimens.xml并添加 <dimen name="main_button_text_size">30sp</dimen>在新的一行上。

现在进入资源文件夹以获得更大的屏幕尺寸,比如 values-sw720dp/dimens.xml (注意:如果不存在,请在 res 目录的根目录中创建文件夹/文件),然后添加 <dimen name="main_button_text_size">40sp</dimen>在新的一行上。

现在,在您的主布局文件中,设置 textSize按钮上的属性为 @dimen/main_button_text_size .

您现在已经有效地为相同的 TextView 设置了不同的值基于显示它的屏幕的大小。

还有其他值文件夹,https://stackoverflow.com/a/21324209/1426565如果您想了解更多,给出了很好的解释。

关于android - 按钮的大小取决于屏幕分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30669523/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com