android - 是否可以包含一个相对于底部而不是顶部元素的按钮?

标签 android android-layout android-support-library android-constraintlayout android-relativelayout

所以我目前正在为我的注册屏幕使用约束布局。但是,注册按钮应该距父元素底部 100dp,而不是距顶部元素 200dp。每当我尝试删除上边距并尝试使其相对于父底部时,它几乎都位于屏幕中间而不是底部。我想知道是否有办法让它与屏幕底部对齐? 这是我的代码:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/content_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    android:fitsSystemWindows="true">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:foreground="@drawable/image_gradient"
        android:scaleType="centerCrop"
        android:src="@drawable/hero_image"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:contentDescription="@null" />

    <ImageView
        android:id="@+id/logo"
        android:layout_width="207dp"
        android:layout_height="77dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="180dp"
        android:contentDescription="@string/my_logo"
        android:src="@drawable/ic_my_white_logo"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginTop="200dp"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:background="@color/white"
        android:fontFamily="sans-serif-medium"
        android:letterSpacing="0.07"
        android:lineSpacingExtra="0sp"
        android:text="@string/sign_in"
        android:textColor="@color/reddish"
        android:textSize="16sp"
        android:textStyle="normal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintBottom_toTopOf="@id/link_sign_up"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/logo"
        app:layout_constraintVertical_chainStyle="packed" />

    <TextView
        android:id="@+id/link_sign_up"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginTop="15dp"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:fontFamily="sans-serif-medium"
        android:gravity="center_horizontal"
        android:letterSpacing="0.07"
        android:lineSpacingExtra="0sp"
        android:text="@string/no_account"
        android:textColor="@color/white"
        android:textSize="16sp"
        android:textStyle="normal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_login" />

</android.support.constraint.ConstraintLayout>

从上面的 XML 中,按钮 login_in 和 sign_up 链接应该在一起(按预期工作),登录按钮,在该注册链接下方。但我手动设置 "android:layout_marginTop="200dp" 从它顶部的 Logo 开始,这不是一个好习惯,对于某些设备,它最终不会按预期对齐底部(并且是硬编码的)。相反,我希望它从边缘说 100dp,没有联盟到顶部,因此对于任何设备,它都是从底部开始的 100dp。关于如何解决此问题以实现目标的任何想法?

提前致谢!

最佳答案

您可以使用如下所示的 RelativeLayout 实现相同的行为。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@android:drawable/btn_default" />

    <ImageView
        android:id="@+id/logo"
        android:layout_width="207dp"
        android:layout_height="77dp"
        android:layout_centerInParent="true"
        android:layout_marginTop="180dp"
        android:src="@drawable/ic_launcher_background" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="100dp"
        android:orientation="vertical">

        <android.support.v7.widget.AppCompatButton
            android:id="@+id/btn_login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="200dp"
            android:layout_marginRight="16dp"
            android:background="@android:color/white"
            android:fontFamily="sans-serif-medium"
            android:letterSpacing="0.07"
            android:lineSpacingExtra="0sp"
            android:text="Sign in"
            android:textColor="@android:color/holo_red_dark"
            android:textSize="16sp"
            android:textStyle="normal" />

        <TextView
            android:id="@+id/link_sign_up"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginRight="16dp"
            android:fontFamily="sans-serif-medium"
            android:gravity="center_horizontal"
            android:letterSpacing="0.07"
            android:lineSpacingExtra="0sp"
            android:text="No Account"
            android:textColor="@android:color/white"
            android:textSize="16sp"
            android:textStyle="normal" />
    </LinearLayout>
</RelativeLayout>

请注意,为了在我的 IDE 中对其进行测试,我省略了一些可绘制对象和颜色。请根据需要进行修改。

关于android - 是否可以包含一个相对于底部而不是顶部元素的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55927759/

相关文章:

android - 为什么不在 Android 开发中只使用最高分辨率的图像?

android - 如何覆盖 android 样式的私有(private)属性

android - 如何创建完整的Round LinearLayout或RelativeLayout

Android 文本布局 Spacingmult 和 Spacingadd?

android - 我可以将最新版本的 appcompat 库用于针对 sdk 21 的项目吗

android - 如何以编程方式通过 tcp 启动 adb 服务器

c# - Facebook (OAuthException - #100) 尝试访问节点类型 (AnonymousUser) 上不存在的字段 (first_name)

android - 滑动刷新仅在用户滑动时调用,而不是在用户到达列表顶部时调用

Android/AOSP Android.mk 构建和支持-v4 AAR 等

java - 无法观察空数据 - 带有 Livedata 的 MVVM