首页 > 解决方案 > ConstraintLayout 视图大小响应

问题描述

我正在尝试使用ConstraintLayoutas进行布局,parent其中有三个buttons如下图所示。

在此处输入图像描述

xml 布局文件如下所示:

<androidx.constraintlayout.widget.ConstraintLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">

<Button
    android:id="@+id/splash_facebook_btn"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_margin="15dp"/>

<Button
    android:id="@+id/splash_sign_in_btn"
    android:layout_width="180dp"
    android:layout_height="45dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="12dp"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/splash_facebook_btn" />

<Button
    android:id="@+id/splash_sign_up_btn"
    android:layout_width="180dp"
    android:layout_height="45dp"
    android:layout_marginEnd="15dp"
    app:layout_constraintBottom_toBottomOf="@id/splash_sign_in_btn"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.995"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintStart_toEndOf="@id/splash_sign_in_btn"
    app:layout_constraintTop_toTopOf="@id/splash_sign_in_btn"
    app:layout_constraintVertical_bias="0.0" />

我想这样做而不为底部的两个按钮设置固定值。

我知道我可以通过更改为LinearLayout和使用来实现这一点,layout_weight但我想在有ConstraintLayout父母的情况下做到这一点。

有没有办法做到这一点?

标签: androidxmluser-interfacelayoutandroid-constraintlayout

解决方案


对于左键设置android:layout_marginEnd="8dp"为右android:layout_marginStart="8dp",然后以编程方式设置它们的宽度:

public int getScreenWidth() {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);    
    return size.x/2;
}

这样你会得到screen width/2,然后你可以用这个值设置按钮的宽度。

如果你只喜欢 XML,试试这个:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <Button
        android:id="@+id/splash_facebook_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_margin="15dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:orientation="horizontal"
        android:weightSum="2"
        android:layout_marginTop="8dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintTop_toBottomOf="@+id/splash_facebook_btn"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        >

        <Button
            android:id="@+id/splash_sign_in_btn"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginEnd="8dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/splash_sign_up_btn"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginStart="8dp"
            android:layout_weight="1" />

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

推荐阅读