【问题标题】:The size of the buttons depending on the screen resolution按钮的大小取决于屏幕分辨率
【发布时间】:2015-08-20 13:56:15
【问题描述】:

我有 LinearLayout 有 6 个按钮。在 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>

【问题讨论】:

  • 使用不同的dimens.xml并添加到各自的分辨率资源文件夹中
  • 你可以尝试使用weightSum

标签: android


【解决方案1】:

如果您希望每个人在第一个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 布局参数的重复),但这有点超出了您最初问题的范围。

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

进入values/dimens.xml 并在新行中添加&lt;dimen name="main_button_text_size"&gt;30sp&lt;/dimen&gt;。

现在进入更大屏幕尺寸的资源文件夹,比如values-sw720dp/dimens.xml(注意:如果不存在,请在res 目录的根目录中创建文件夹/文件),然后添加&lt;dimen name="main_button_text_size"&gt;40sp&lt;/dimen&gt; on换行。

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

您现在只是根据所显示的屏幕大小有效地为同一个TextView 设置了不同的值。

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

【讨论】:

  • 您可能还想根据屏幕的密度指定不同的文本大小 - 我该怎么做?
  • 回答为编辑,因为我认为它不适合这里
【解决方案2】:

如果你使用Android Studio,有一个选项叫做:

Preview All Screen Sizes Android Studio的预览 工具栏预览的顶部。

所以,这就是你需要的:

http://developer.android.com/guide/practices/screens_support.html

支持的屏幕范围:

从 Android 1.6(API 级别 4)开始,Android 支持 多种屏幕尺寸和密度,反映了许多不同的 设备可能具有的屏幕配置。您可以使用以下功能 Android 系统优化您的应用程序的用户界面 每个屏幕配置并确保您的应用程序不仅 渲染正确,但提供了可能的最佳用户体验 每个屏幕。

  • ldpi(低)~120dpi
  • mdpi(中)~160dpi
  • hdpi(高)~240dpi
  • xhdpi(超高)~320dpi
  • xxhdpi (extra-extra-high) ~480dpi
  • xxxhdpi (extra-extra-extra-high) ~640dpi

最终答案: http://developer.android.com/guide/practices/screens_support.html#support

您应该为每个 API 创建一个与下图完全相同的布局文件:

  • res/layout/layout.xml
  • res/layout-small/layout.xml
  • res/layout-large/layout.xml
  • res/layout-xlarge/layout.xml
  • res/layout-land/layout.xml
  • res/layout-small-land/layout.xml
  • res/layout-large-land/layout.xml
  • res/layout-xlarge-land/layout.xml

您需要为每个屏幕进行设计。

【讨论】:

    【解决方案3】:

    将所有按钮更改为零高度

    android:layout_height=0dp"
    

    给它们增加重量

    android:layout_weight="1"
    

    这将使所有视图(按钮)均等地缩放以占据整个 LinearLayout。

    ps.:忽略所有关于使用不同布局或不同尺寸的讨论。

    【讨论】:

    • ps.:忽略所有关于使用不同布局或不同尺寸的讨论。 - 不能忽视文本大小的不同尺寸
    • @AndrewEfremov 这些对于 android 开发人员来说是重要的工具,但与您的主要问题无关。
    【解决方案4】:

    如果你有不同的维度,你可以通过调用维度属性来切换match_parent和layout_weight

    ==> layout_width= -1 (match_parent)
    ==> layout_width= 100dp ( fixed width )
    
    ==> layout_weight= 1 (setting weight)
    ==> layout_weight= 0 (Ignore weight)
    

    示例: 假设,我们有三种类型的屏幕(小、中、大),而您想要 保持 match_parent 和 layout_weight 为 small 和 medium 自动调整 设备尺寸,但对于大型设备,我们需要固定宽度。你可以这样做...

    这里三种屏幕类型

    1. values ( dimens.xml )
    <dimen name="layout_width_value">-1dp</dimen> (android:layout_width=match_parent)
    <dimen name="layout_weight_value">1</dimen> (layout_weight=1)
    
    2. values-sw375dp ( dimens.xml )
    <dimen name="layout_width_value">-1dp</dimen> (android:layout_width=match_parent)
    <dimen name="layout_weight_value">1</dimen> (layout_weight=1)
    
    3. values-sw600dp ( dimens.xml )
    <dimen name="layout_width_value">370dp</dimen> (android:layout_width=370dp)
    <dimen name="layout_weight_value">0</dimen> (layout_weight=0)
    

    No 3 与 No1 和 No2 不同,因为 No 3 是固定宽度且没有 layout_weight。 No1 和 No2 是灵活的宽度,根据屏幕大小自动调整。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-26
      • 2013-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 2014-12-27
      相关资源
      最近更新 更多