【问题标题】:layout_marginTop in Android StudioAndroid Studio 中的 layout_marginTop
【发布时间】:2021-03-10 13:57:24
【问题描述】:

我正在尝试为 Android Studio 中的应用创建登录页面。该页面的 XML 代码是 -

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@color/colorPrimary"
    tools:context=".LoginChoice">


    <Button
        android:id="@+id/userbtn"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="450dp"
        android:onClick="userloginIntent"
        android:adjustViewBounds="true"
        android:background="@drawable/user_login_button_small"
        app:strokeColor="@android:color/white" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/driverbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textColor="@android:color/white"
        android:onClick="driverloginIntent"
        android:paddingHorizontal="10dp"
        android:text="@string/driver_login"
        android:background="@drawable/buttonbg1"
        android:layout_marginVertical="20dp"
        android:layout_below="@+id/userbtn"/>

</RelativeLayout>

这里的问题是,我正在使用的Buttons 在我的手机中运行应用程序时看起来比在 Android Studio 中的Design 区域中运行的要低得多。 这是它在 android studio 中的外观-

这是我在手机上运行应用程序时的屏幕截图 -

为什么会发生这种情况,我可以做些什么来解决它?

【问题讨论】:

  • 你想达到什么目的?您将上边距设置为 userBtn = 450dp。该值会因设备的屏幕尺寸而异。

标签: android android-studio button layoutmargins


【解决方案1】:

在 Android Studio 中,您可以选择您正在使用的设备来可视化您的layout.xml,默认设备可能没有您的物理设备的尺寸。您可以更改它或添加设备定义以匹配您所需的设备。此外,您可以使用Layout Validation 来查看更准确的设计预览。

请记住,您使用的是dp,而不是标准的pixels

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 2013-02-09
    • 2016-01-31
    相关资源
    最近更新 更多