【问题标题】:Android Studio layout way off on different devicesAndroid Studio 布局在不同设备上的差异
【发布时间】:2016-03-11 13:54:03
【问题描述】:

我正在尝试制作一个迷宫应用。只有我注意到一个问题。

整个迷宫是用 XML 设计的,仅使用 dp 作为尺寸,但在某些手机上看起来仍然很遥远。我做错了什么?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="xander.woutzijnemptyactivity.MainActivity"
>

    <!-- Power ups -->
    <ImageView
        android:layout_width="@dimen/ball_dimension"
        android:layout_height="@dimen/ball_dimension"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:id="@+id/power_up_0"
        android:src="@drawable/power_up_pink"
        android:layout_marginTop="60dp"
        android:layout_marginLeft="200dp" />

    <ImageView
        android:layout_width="@dimen/ball_dimension"
        android:layout_height="@dimen/ball_dimension"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:id="@+id/power_up_1"
        android:src="@drawable/power_up_red"
        android:layout_marginTop="200dp"
        android:layout_marginLeft="100dp" />

    <ImageView
        android:layout_width="@dimen/ball_dimension"
        android:layout_height="@dimen/ball_dimension"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:id="@+id/power_up_2"
        android:src="@drawable/power_up_blue"
        android:layout_marginTop="150dp"
        android:layout_marginLeft="200dp" />

    <!-- Ball -->
    <ImageView
        android:layout_width="@dimen/ball_dimension"
        android:layout_height="@dimen/ball_dimension"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:id="@+id/ball"
        android:src="@drawable/ball_128"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="100dp" />

    <!-- Four border walls -->

    <ImageView
        android:layout_width="10dp"
        android:layout_height="270dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:id="@+id/wall0"
        android:background="#000000"
        android:alpha="1"
        android:layout_marginTop="0dp"
        android:layout_marginLeft="75dp" />

    <ImageView
        android:layout_width="10dp"
        android:layout_height="270dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:id="@+id/wall1"
        android:background="#000000"
        android:alpha="1"
        android:layout_marginTop="0dp"
        android:layout_marginLeft="566dp" />

    <ImageView
        android:layout_width="500dp"
        android:layout_height="10dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:id="@+id/wall2"
        android:background="#000000"
        android:alpha="1"
        android:layout_marginTop="260dp"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="500dp"
        android:layout_height="10dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:id="@+id/wall3"
        android:background="#000000"
        android:alpha="1"
        android:layout_marginTop="0dp"
        android:layout_centerHorizontal="true" />

    <!-- Maze walls -->

    <ImageView
        android:layout_width="@dimen/wall_width"
        android:layout_height="200dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:id="@+id/wall4"
        android:background="#000000"
        android:alpha="1"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="120dp" />

    <ImageView
        android:layout_width="60dp"
        android:layout_height="@dimen/wall_width"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:id="@+id/wall5"
        android:background="#000000"
        android:alpha="1"
        android:layout_marginTop="205dp"
        android:layout_marginLeft="121dp" />

    <ImageView
        android:layout_width="150dp"
        android:layout_height="@dimen/wall_width"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:id="@+id/wall6"
        android:background="#000000"
        android:alpha="1"
        android:layout_marginTop="205dp"
        android:layout_marginLeft="225dp" />

    <ImageView
        android:layout_width="@dimen/wall_width"
        android:layout_height="34dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:id="@+id/wall7"
        android:background="#000000"
        android:alpha="1"
        android:layout_marginTop="175dp"
        android:layout_marginLeft="225dp" />

    <ImageView
        android:layout_width="40dp"
        android:layout_height="@dimen/wall_width"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:id="@+id/wall8"
        android:background="#000000"
        android:alpha="1"
        android:layout_marginTop="175dp"
        android:layout_marginLeft="189dp" />

    <ImageView
        android:layout_width="@dimen/wall_width"
        android:layout_height="140dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:id="@+id/wall9"
        android:background="#000000"
        android:alpha="1"
        android:layout_marginTop="39dp"
        android:layout_marginLeft="189dp" />

    <TextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="time: "
        android:id="@+id/timeText"
        android:layout_marginTop="280dp"
        android:textSize="20sp"
        android:gravity="center" />

</RelativeLayout>

【问题讨论】:

  • 请附上你如何绘制它的代码。
  • @DavidMedenjak 这是 xml 代码
  • 另外请附上您设置边距的代码。似乎您在运行时对某些部分执行此操作
  • 我不相信我曾经设置过任何边距。这是整个 xml 文件。在一般代码中,我只使用此 xml 文件中的代码。

标签: java android xml layout


【解决方案1】:

您正在使边界墙居中。

<ImageView
  android:layout_width="500dp"
  android:layout_height="10dp"
  android:adjustViewBounds="true"
  android:scaleType="centerCrop"
  android:id="@+id/wall2"
  android:background="#000000"
  android:alpha="1"
  android:layout_marginTop="260dp"
  android:layout_centerHorizontal="true"/> <--- THIS ONE

删除android:layout_centerHorizontal="true" 并在左侧应用一个边距,就像在左墙上一样,那么它应该可以工作:

android:layout_marginLeft="75dp"

【讨论】:

  • 是的,我知道这有帮助。但我一直遇到的问题是,在某些屏幕上迷宫非常适合,而在其他屏幕上它仍然会脱落。就像我问题中的图片一样。我认为通过使用 dp 这不应该发生。但显然确实如此。
  • @sateeprikker dp 适用于独立的尺寸。如果你在屏幕上放一把尺子,每个尺子的尺寸都差不多。您需要相对尺寸,与屏幕宽度相比,这有点复杂
  • 啊。所以你的意思是,如果我在 5 英寸屏幕上使用相同的 dp(并且它适合)它会落在 4 英寸屏幕上?你有什么建议我应该如何解决这个问题?
  • @sateeprikker 没错。但正如我所说,我不知道任何简单的解决方案,因为这需要测量尺寸并相应地调整所有尺寸:/
【解决方案2】:

很遗憾,仅使用 dp 作为维度对您没有多大帮助。

为不同的屏幕尺寸创建不同的布局文件是解决方案。

请阅读Supporting Different Screen Sizes。它包含您需要的所有信息。

【讨论】:

    【解决方案3】:

    我想让任何寻找此问题答案的人都知道我的解决方案。 我制作了自己的单位系统。一个所谓的块,在我的例子中,这是屏幕宽度除以 80。这为您提供了一个适用于所有分辨率的单位。使用此解决方案,您的屏幕始终保持相同的比例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多