Java 安卓:不同屏幕大小的问题不显示相同的内容
我对Android/Java/编程基本上是新手,如果这是一个愚蠢的问题,那么很抱歉 我一直在为我的LG G3制作一个小游戏,网格的布局看起来不错(9x9),可以在这里找到: 然而,我最近在Nexus 5模拟器上测试了它,结果显示它的布局是这样的:(它现在是一个8x9网格,在屏幕边距上流动。) 我已将所有按钮的宽度和高度设置为“dp” 下面是布局XMLJava 安卓:不同屏幕大小的问题不显示相同的内容,java,android,button,layout,Java,Android,Button,Layout,我对Android/Java/编程基本上是新手,如果这是一个愚蠢的问题,那么很抱歉 我一直在为我的LG G3制作一个小游戏,网格的布局看起来不错(9x9),可以在这里找到: 然而,我最近在Nexus 5模拟器上测试了它,结果显示它的布局是这样的:(它现在是一个8x9网格,在屏幕边距上流动。) 我已将所有按钮的宽度和高度设置为“dp” 下面是布局XML <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro
<?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网格在所有设备上都可见呢
感谢所有帮助阅读以下有关屏幕大小的信息: 要获得更好的“响应性”设计,请使用“权重”属性 文件在此: 编辑:示例如下:
祝你好运;) 您可以使用
LinearLayout
的setWeight
属性。加权布局将测量显示,每个布局将根据其相对权重填充屏幕。这是一个简单的方法,一旦你熟悉了它。但是,如果考虑边距/填充等,则可以产生大量的“嵌套”布局和权重。
您可以使用GridView
设置列/行。这与上面的方法具有类似的优点,但通常在需要滚动的情况下非常有用。但是,适配器可能会为您提供使其成为更好选项的工具
不过,控制最多的选项是使用DisplayMetrics
测量屏幕大小,然后根据测量的屏幕大小设置所有布局。在布局中设置dp
,通常会出现这样的问题
这也将给你完全控制的利润,居中等,如果你想或需要。您还可以确保它在较小的屏幕和较大的屏幕上看起来都是正确的
<>最后,您可以考虑使用不同的布局,用于<代码> HDPI <代码> MPDPI 和<代码> XHDPI 屏幕。甚至使用dimens
属性