首页 > 解决方案 > 我无法均匀间隔按钮

问题描述

我在android studio中使用约束布局,我想在主要活动上均匀间隔按钮,它在移动视图上工作正常,但是当我尝试切换到平板电脑视图时它变得一团糟,有没有办法在两者上均匀间隔按钮意见?这是图像中的问题

标签: android-layoutandroid-constraintlayoutandroid-layout-weight

解决方案


如果您想了解如何使用 chainStyle 的示例,可以查看我们的一个公共示例。或者这是链接中的粘贴:

<androidx.cardview.widget.CardView
    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="wrap_content"
    android:layout_margin="@dimen/margin_std_half"
    android:clickable="true"
    app:cardBackgroundColor="@color/colorAccentLighter"
    app:cardCornerRadius="@dimen/cardview_corner_radius"
    app:contentPadding="@dimen/cardview_padding"
>
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    >
        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/sku_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@id/guideline"
            app:layout_constraintEnd_toStartOf="@id/sku_price"
            app:layout_constraintHorizontal_chainStyle="spread_inside"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="This is a Title placeholder"/>
        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/sku_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="@id/guideline"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/sku_title"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="$4.99"/>
        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.25"/>

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/sku_image"
            android:layout_width="wrap_content"
            android:layout_height="68dp"
            android:adjustViewBounds="true"
            android:maxWidth="126dp"
            android:scaleType="centerInside"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/sku_description"
            app:layout_constraintHorizontal_chainStyle="packed"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@id/guideline"
        />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/sku_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/margin_std_half"
            android:layout_marginRight="@dimen/margin_std_half"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/sku_image"
            app:layout_constraintTop_toTopOf="@id/guideline"
            tools:text="This is a description placeholder, telling users how cool this item is"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

推荐阅读