简体   繁体   English

独立于设备分辨率的按钮网格

[英]craeate grid of buttons independent from resolution of device

I want to create a grid of Buttons in my android app. 我想在我的Android应用程序中创建一个Buttons网格。 The problem is, when I test this on low resolution devices, some Buttons are displayed off the screen. 问题是,当我在低分辨率设备上进行测试时,屏幕上会显示一些Buttons How can I fix that? 我该如何解决?

This is the layout I am using: 这是我正在使用的布局:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:columnCount="4"
    android:orientation="horizontal">

    <Button
        android:text=""
        android:id="@+id/b0"
        android:padding="20dp"
        android:layout_margin="10dp" />

    <Button
        android:text=""
        android:id="@+id/b1"
        android:padding="20dp"
        android:layout_margin="10dp" />

    <Button
        android:text=""
        android:id="@+id/b2"
        android:padding="20dp"
        android:layout_margin="10dp" />

    <Button
        android:text=""
        android:id="@+id/b3"
        android:padding="20dp"
        android:layout_margin="10dp" />

    <Button
        android:text=""
        android:id="@+id/b4"
        android:padding="20dp"
        android:layout_margin="10dp" />

    <Button
        android:text=""
        android:id="@+id/b5"
        android:padding="20dp"
        android:layout_margin="10dp" />

    <Button
        android:text=""
        android:id="@+id/b6"
        android:padding="20dp"
        android:layout_margin="10dp" />

    <Button
        android:text=""
        android:id="@+id/b7"
        android:padding="20dp"
        android:layout_margin="10dp" />

    <Button
        android:text=""
        android:id="@+id/b8"
        android:padding="20dp"
        android:layout_margin="10dp" />

    <Button
        android:text=""
        android:id="@+id/b9"
        android:padding="20dp"
        android:layout_margin="10dp" />

    <Button
        android:text=""
        android:id="@+id/b10"
        android:padding="20dp"
        android:layout_margin="10dp" />

    <Button
        android:text=""
        android:id="@+id/b11"
        android:padding="20dp"
        android:layout_margin="10dp" />

    <Button
        android:text=""
        android:id="@+id/b12"
        android:padding="20dp"
        android:layout_margin="10dp" />

    <Button
        android:text=""
        android:id="@+id/b13"
        android:padding="20dp"
        android:layout_margin="10dp" />

    <Button
        android:text=""
        android:id="@+id/b14"
        android:padding="20dp"
        android:layout_margin="10dp" />

    <Button
        android:text=""
        android:id="@+id/b15"
        android:padding="20dp"
        android:layout_margin="10dp" />

    <TextView
        android:id="@+id/text1"
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:textSize="30sp"
        android:text="  Taget:-" />

    <TextView
        android:id="@+id/tar"
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:textSize="30sp"
        android:text="" />

    <TextView
        android:id="@+id/text2"
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:textSize="30sp"
        android:text="  Current:-" />

    <TextView
        android:id="@+id/cur"
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:textSize="30sp"
        android:text="" />

    <TextView
        android:id="@+id/text3"
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:textSize="20sp"
        android:text="  TimeLeft:-" />

    <TextView
        android:id="@+id/tim"
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:textSize="20sp"
        android:text="" />


</GridLayout>

When using a GridLayout you need to specify where exactly you want the Views to be positioned and how big they are supposed to be. 使用GridLayout ,需要指定要在何处准确放置Views ,以及它们应该多大。 Try something like this: 尝试这样的事情:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
    android:rowCount="2"
    android:orientation="horizontal">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="0"
        android:layout_columnSpan="2"
        android:layout_rowSpan="2" />

    ...

</GridLayout>

To define how many rows and columns there are use these two attributes on the GridLayout: 要定义有多少行和列,请在GridLayout上使用以下两个属性:

  • android:columnCount
  • android:rowCount

You can define the position of a View inside the GridLayout with these two attributes: 您可以使用以下两个属性来定义View在GridLayout中的位置:

  • android:layout_column
  • android:layout_row

And you can define how many rows or columns a View spans with these two attributes: 您可以使用以下两个属性定义视图跨多少行或几列:

  • android:layout_rowSpan
  • android:layout_columnSpan

Please note that the GridLayout was only added with API level 14! 请注意,GridLayout仅在API级别14中添加! If you want to use the GridLayout in earlier versions you need to use the GridLayout from the support library: 如果要在早期版本中使用GridLayout,则需要使用支持库中的GridLayout:

<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    ...    

</android.support.v7.widget.GridLayout>

Also note that you ARE NOT supposed to hardcode a fixed size for each Button . 还要注意,您应该为每个Button硬编码固定大小。 Set height and width to wrap_content , the GridLayout will take care of setting the size of the Buttons . 将高度和宽度设置为wrap_contentGridLayout将负责设置Buttons的大小。

You can decrease the amount of columns you display by using a resource selector: 您可以使用资源选择器减少显示的列数:

For example define this in res/values/dimens.xml so smaller devices use only two columns: 例如,在res / values / dimens.xml中定义此名称,以便较小的设备仅使用两列:

<dimen name="column_count">2</dimen>

And in res/values-w350dp/dimens.xml you define the same dimen resource with a bigger value: res / values-w350dp / dimens.xml中,您可以定义具有更大值的相同dimen资源:

<dimen name="column_count">3</dimen>

The w350dp means that all device which have a width bigger than 350dp use 3 columns instead of the default 2. w350dp表示所有宽度大于350dp的设备都使用3列,而不是默认的2列。

To make this work you of course need to set this dimen resource as the columnCount on your GridLayout : 为了完成这项工作,您当然需要将这个dimen资源设置为GridLayout上的columnCount

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="@dimen/column_count"
    android:rowCount="2"
    android:orientation="horizontal">

    ...

</GridLayout>

First and foremost, the Android developer's page on supporting multiple screens is a pretty comprehensive guide on this subject. 首先,有关支持多屏幕的Android开发人员页面是有关此主题的非常全面的指南。

In short, you're on the right track using dp instead of straight pixels, as Android will scale most things on its own pretty well. 简而言之,您将使用dp而不是直像素进入正确的轨道,因为Android可以很好地自行缩放大多数内容。 However, this system is not always perfect in practice. 但是,该系统在实践中并不总是完美的。 One way to get around this is to create different layouts for different screen sizes. 解决此问题的一种方法是为不同的屏幕尺寸创建不同的布局。

By default, Android resizes your application layout to fit the current device screen. 默认情况下,Android会调整应用程序布局的大小以适合当前设备屏幕。 In most cases, this works fine. 在大多数情况下,这可以正常工作。 In other cases, your UI might not look as good and might need adjustments for different screen sizes. 在其他情况下,您的UI可能看起来不那么好,可能需要针对不同的屏幕尺寸进行调整。 For example, on a larger screen, you might want to adjust the position and size of some elements to take advantage of the additional screen space, or on a smaller screen, you might need to adjust sizes so that everything can fit on the screen. 例如,在较大的屏幕上,您可能需要调整某些元素的位置和大小以利用额外的屏幕空间,或者在较小的屏幕上,可能需要调整大小以使所有内容都适合屏幕。

The configuration qualifiers you can use to provide size-specific resources are small, normal, large, and xlarge. 可用于提供特定于大小的资源的配置限定符为small,normal,large和xlarge。 For example, layouts for an extra large screen should go in layout-xlarge/. 例如,超大屏幕的布局应放在layout-xlarge /中。

For example, you could use a similar layout with only 3 columns instead of 4 for small-medium screens (if that would work with your game), or make the size of each button smaller. 例如,对于中小屏幕,您可以使用只有3列而不是4列的类似布局(如果这适用于您的游戏),或者使每个按钮的尺寸更小。

Additionally, you could declare in your application that small screen sizes are not supported. 此外,您可以在应用程序中声明不支持小屏幕尺寸。 This is pretty universally frowned upon if you can avoid it, since it does exclude some users from your app. 如果您可以避免的话,这是普遍不赞成的,因为它确实从您的应用程序中排除了一些用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM