【问题标题】:Preview in Android Studio do not same as the one in hardwareAndroid Studio 中的预览与硬件中的预览不同
【发布时间】:2016-04-06 12:30:23
【问题描述】:

我有一个 ImageView 作为背景,想在背景前面放置一些小部件。

我尝试了相对布局和线性布局,但仍然发现 AndroidStudio 中的布局预览与我在设备中部署的不一样。设备和背景图片均为 16:9, 1920x1080

  • 背景分辨率:1080x1920
  • 设备分辨率:1080x1920
  • 活动主题:Theme.NoTitleBar.Fullscreen

这是布局 XML 文件

<?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"
    tools:context="com.chihangc.imaginecup_prel.MainActivity"
    android:layout_margin="0dp"
    android:padding="0dp"

    >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/r01_main"
    />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="500dp">

            <Button
                android:layout_width="210dp"
                android:layout_height="80dp"
                android:text="New Buttonr"
                android:id="@+id/button"
                android:layout_marginBottom="127dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <Button
                android:layout_width="210dp"
                android:layout_height="80dp"
                android:text="New Buttonr"
                android:id="@+id/button2"
                android:layout_alignTop="@+id/button"
                android:layout_toRightOf="@+id/button"
                android:layout_toEndOf="@+id/button" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

硬件截图

Android Studio 布局预览

【问题讨论】:

  • 您的布局预览中的设备是否与您的硬件设备具有相同的配置?
  • 两者是否也在同一个 Android SDK API 版本上?
  • @Pooya,是的,我自己添加的
  • @Radix,谢谢。您能否详细解释一下 Framelayout 如何实现上述解决方案?非常感谢
  • @Elye 是的。我正在使用 API23 Android 6.0

标签: android android-layout android-studio


【解决方案1】:

如我所见,显示的布局非常混乱。几个问题 - 当按钮被线性布局包裹时,按钮中有一些相对布局参数的混合。 - 有嵌套的线性布局层,这是不必要的。 - 以及各种硬编码值(例如按钮宽度。

我对代码进行了一些改进。您可以使用此代码,并根据需要重新调整上边距(我现在设置为 400dp),希望这将在预览和实际设备之间提供一致。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/r01_main"
        />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="2"
        android:layout_marginTop="400dp">

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="80dp"
            android:text="New Buttonr"
            android:id="@+id/button"/>

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="80dp"
            android:text="New Buttonr"
            android:id="@+id/button2"/>
    </LinearLayout>

</RelativeLayout>

理想情况下,我们也不应该依赖 marginTop。而是将图像分成多个图像。并将主要作为顶部视图放在底部上方。这样就可以更好地控制跨设备(具有不同高度)的视图布局。

【讨论】:

  • 非常感谢您的帮助。但是,问题仍然存在。预览和设备布局不一样
  • 你的代码中还有android:layout_marginBottom="127dp"吗?
  • 没有。我复制你所有的代码来替换它。并且添加 layout_marginBottom="127dp" 后没有任何区别
  • 你的模拟器设备的 DPI 是多少,你的硬件设备 DPI 是多少?
  • 我检查了 1080x1090 设备模拟器 Nexus 5X 和 Nexus 5,它们都有基于相同 .xml 的不同布局。它们的 DPI 不同,它们的实际设备高度也不同(Nexus 5X 更长)。你能测试一下吗。这就是使用固定边距高度不理想的原因。
【解决方案2】:

由于您为 marginBottom 提供了明确的值,因此您将在不同的屏幕尺寸下获得不同的输出。您的模拟器屏幕尺寸可能与您的硬件屏幕尺寸不同。为防止这种情况发生,请使用 RelativeLayout 并相应地对齐按钮。

【讨论】:

  • 谢谢。我已经尝试过 RelativeLayout 并将按钮与 layout_marginBottom 和 layout_alignParentBottom 对齐。但它仍然向下移动。
  • 如果您的屏幕尺寸不同,那么您在预览中看到的内容与您在硬件中看到的内容会不匹配。使用具有不同屏幕尺寸的不同设备,您将获得不同的输出。为避免这种情况,您要么必须计算屏幕高度并相应地设置边距,要么必须针对不同的屏幕尺寸设计布局。
  • 但是我已经在 AndroidStudio 中设置了一个具有相同分辨率和屏幕尺寸的硬件配置文件。
【解决方案3】:

试试这个代码

<?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:layout_margin="0dp"
    android:background="@drawable/bg"
    android:padding="0dp"
    tools:context="com.chihangc.imaginecup_prel.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:layout_weight="0.5"
            android:text="New Buttonr" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:layout_weight="0.5"
            android:text="New Buttonr" />
    </LinearLayout>
    
</RelativeLayout>

【讨论】:

  • 谢谢。但问题是按钮向下移动。
  • 谢谢,但我的按钮仍然向下移动(而不是宽度不同)
  • 感谢您的帮助。但是,问题 - 那些向下移动的按钮的垂直 (y) 位置仍然无法解决
  • 我再次更改了我的代码,并且它在我的设备上运行良好
  • 再次感谢。您的代码没有那些按钮不在中间的 layout_marginBottom 。添加 android:layout_marginBottom="150dp" 后代码仍然失败
【解决方案4】:

也许两个屏幕的尺寸不同。

android:layout_marginTop="500dp" 保证不同密度下边距大小相同,但不保证不同分辨率下的位置。

您可以删除marginTop的线并对齐到LinearLayout的底部。

编辑 垂直居中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/r01_main" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_centerVertical="true">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button2" />
</LinearLayout>
</RelativeLayout>

【讨论】:

  • 不使用marginTop,我应该使用什么来使这些按钮位于中间(我的意思是垂直的)
  • 使用 android:layout_centerVertical 作为 LinearLayout 属性。 developer.android.com/reference/android/widget/…
  • 我尝试使用具有特定高度 (150dp) 的 LinearLayout。然后对齐上面的按钮。可悲的是,仍然失败
  • 但是这些按钮不能对齐到不同的 y 位置
【解决方案5】:

为您的 XML 布局使用以下代码

<?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"
    tools:context=".MainActivity"
    android:layout_margin="0dp"
    android:padding="0dp"
    android:background="@drawable/r01_main">



<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical">

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="New Buttonr"
                android:id="@+id/button"
                android:layout_weight=".5"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="New Buttonr"
                android:id="@+id/button2"
                android:layout_weight=".5"/>

</LinearLayout>
</RelativeLayout>

如果您想将按钮放置在中心,您可以这样做。 将 android:layout_alignParentBottom="true" 替换为 android:layout_gravity="center_vertical 在按钮的父布局中(线性布局)。

【讨论】:

  • 谢谢,但按钮的 y 位置不正确。
  • 你想如何显示y位置?
  • 参考问题,我希望按钮位置在屏幕中间(y位置的一些变化)
  • 请检查更新的答案,让我知道状态。抱歉,如果我无法帮助您实现目标。
猜你喜欢
  • 2015-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多