【问题标题】:Android: Problems with different screen sizes not showing the same thingAndroid:不同屏幕尺寸的问题不显示相同的东西
【发布时间】:2015-05-02 04:34:01
【问题描述】:

我是 Android/Java/编程的新手,如果这是一个愚蠢的问题,我很抱歉。

我一直在为我的 LG G3 制作一个小游戏,网格的布局看起来不错 (9x9),可以在这里找到:http://i.imgur.com/DGlsYGv.png

不过,我最近在 Nexus 5 模拟器上对其进行了测试,结果是这样的布局:http://i.imgur.com/MiIsYyr.png(它现在是 8x9 网格并流过屏幕边距。)

我已将所有按钮的宽度和高度设置为“dp”。

这是布局 XML

<?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:layout_gravity="top|right"
android:background="#ff000000"
android:layout_centerHorizontal="true"
android:id="@+id/fifteen_by_fifteen">

<Button
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:textSize="23sp"
    android:background="@android:color/transparent"
    android:text="@string/back"
    android:layout_gravity="top|right"
    android:textColor="#ffff274a"
    android:id="@+id/back_button"/>


<Button
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:textSize="30sp"
    android:background="@android:color/transparent"
    android:id="@+id/show_unknown_distance_button"
    android:layout_gravity="center_horizontal"
    android:textStyle="bold"
    android:textColor="#ff59e4ff"/>

<GridLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="50dp"
    android:rowCount="9"
    android:columnCount="9"
    android:layout_gravity="center">


    <Button
        android:layout_width="44dip"
        android:layout_height="45dip"
        android:id="@+id/zero_zero"
        android:layout_margin="1dip"
        android:layout_gravity="center"
        android:paddingTop="1dip"
        android:paddingRight="2dip"
        android:paddingLeft="2dip"
        android:paddingBottom="1dip"
        android:layout_row="0"
        android:layout_column="0" />

    <Button
        android:layout_width="44dip"
        android:layout_height="45dip"
        android:id="@+id/zero_one"
        android:layout_margin="1dip"
        android:layout_gravity="center"
        android:paddingTop="1dip"
        android:paddingRight="2dip"
        android:paddingLeft="2dip"
        android:paddingBottom="1dip"
        android:layout_row="0"
        android:layout_column="1" />

    <Button
        android:layout_width="44dip"
        android:layout_height="45dip"
        android:id="@+id/zero_two"
        android:layout_margin="1dip"
        android:layout_gravity="center"
        android:paddingTop="1dip"
        android:paddingRight="2dip"
        android:paddingLeft="2dip"
        android:paddingBottom="1dip"
        android:layout_row="0"
        android:layout_column="2" />

81 个按钮以此类推。

那么我应该怎么做才能使 9x9 网格在所有设备上可见?

感谢大家的帮助

【问题讨论】:

    标签: java android button layout


    【解决方案1】:

    阅读有关屏幕尺寸的信息: https://stackoverflow.com/a/17547325/3552583

    要获得更好的“响应式”设计,请使用 weight 属性。

    文档在这里:http://developer.android.com/guide/topics/ui/layout/linear.html

    编辑:此处示例:http://www.101apps.co.za/index.php/articles/using-android-s-layout-weight-attribute.html

    祝你好运;)

    【讨论】:

      【解决方案2】:

      您可以使用LinearLayoutsetWeight 属性。加权布局将测量显示,每个布局将根据其相对权重填充屏幕。这是一种简单的方法,一旦您熟悉了它。但是,如果您考虑边距/填充等,它可能会导致大量“嵌套”布局和权重。

      您可以使用GridView 并设置列/行。这具有与上述类似的优点,但通常对于您想要滚动的情况很有用。但是,适配器可能会为您提供工具,使其成为更好的选择。

      不过,控制最多的选项是使用DisplayMetrics 测量您的屏幕尺寸,然后根据测量的屏幕尺寸设置所有布局。在布局中设置dp 通常会出现这样的问题。

      如果您想要或需要,这也可以让您完全控制边距、居中等。您还可以确保它在较小的屏幕和较大的屏幕上看起来都合适。

      How to get screen display metrics in application class

      最后,您可以考虑为hdpi mdpixhdpi 屏幕使用不同的布局。甚至使用dimens 属性。

      How to create layout-small-land folder?

      http://developer.android.com/guide/topics/resources/more-resources.html#Dimension

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-11
        • 1970-01-01
        • 2020-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多