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

android-按钮的大小取决于屏幕分辨率,第1张

概述我有6个按钮的LinearLayout.分辨率为4.7英寸时,按钮几乎是完美的选择.但是在分辨率较高的设备上,它们不会占据整个屏幕,而在分辨率较低的设备上,并非所有屏幕都可见.您能否解释一下如何在屏幕分辨率不同的设备上使按钮显示相同?<?xmlversion="1.0"encoding="utf-8"?><LinearLa

我有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的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的布局参数的重复),但这超出了原始问题的范围.

编辑:要在注释中回答您的问题,以了解如何使用不同的资源为不同的密度提供不同的文本大小…

进入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-按钮的大小取决于屏幕分辨率全部内容,希望文章能够帮你解决android-按钮的大小取决于屏幕分辨率所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1089608.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存