【问题标题】:ConstraintLayout: Center two buttons horizontallyConstraintLayout:水平居中两个按钮
【发布时间】:2020-03-14 17:32:23
【问题描述】:

虽然看起来很简单,但我做不到,但我希望两个按钮从侧面相互接触并水平居中,如下所示:

我在这个帖子中尝试了答案:Center two buttons horizontally,但它只与RelativeLayout有关,与ContrainstLayout无关

我也试过玩

app:layout_constraintHorizontal_chainStyle="spread"

但没有成功。我的无用 xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    android:background="@color/colorBackground"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        style="@style/btnStyle"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:background="@color/btnTrue"
        android:text="Button"
        android:textColor="#ffffff"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        style="@style/btnStyle"
        android:layout_height="wrap_content"
        android:layout_marginEnd="56dp"
        android:background="@color/btnFalse"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        tools:layout_editor_absoluteY="0dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

这可以通过 ConstraintLayout 实现吗?

【问题讨论】:

  • 在水平线性布局中添加按钮并将容器在屏幕上居中。

标签: android android-constraintlayout


【解决方案1】:

这应该做的工作。

<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintEnd_toStartOf="@+id/button2"
    app:layout_constraintHorizontal_chainStyle="packed"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_chainStyle="packed" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@id/button"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

关键是删除不需要的参数并使用

 app:layout_constraintHorizontal_chainStyle="packed"

【讨论】:

    【解决方案2】:

    您必须为两个按钮创建一个水平链,并以链样式打包

    这是一个示例

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.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"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#000"
            android:text="Button"
            android:textColor="#ffffff"
            app:layout_constraintEnd_toStartOf="@+id/button2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintHorizontal_chainStyle="packed"/>
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#ad11"
            android:text="Button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/button"
            app:layout_constraintTop_toTopOf="parent"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    输出:

    【讨论】:

      【解决方案3】:

      您可以将按钮放在水平线性布局中

      <androidx.constraintlayout.widget.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"
          tools:context=".MainActivity">
      
          <LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent">
      
              <Button
                  android:id="@+id/button"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:backgroundTint="@color/colorPrimary"
                  android:text="Button"
                  android:textColor="#ffffff" />
      
              <Button
                  android:id="@+id/button2"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:backgroundTint="@color/colorAccent"
                  android:text="Button" />
          </LinearLayout>
      
      
      </androidx.constraintlayout.widget.ConstraintLayout>
      

      【讨论】:

      • 这要简单得多。如何将组件之间的间距宽度设置为 0?
      • @August 在这种情况下,我使用的是按钮的默认样式,因此,有一些您无法避免的填充(或者我认为是这样)。最好的方法是创建一个新的按钮样式,您可以在其中定义填充。在这里看看如何创建新样式:stackoverflow.com/questions/18507351/…
      【解决方案4】:

      尝试使用“指南”:

      <?xml version="1.0" encoding="utf-8"?>
      
      <androidx.constraintlayout.widget.ConstraintLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      
      <androidx.constraintlayout.widget.Guideline
          android:id="@+id/guideline"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          app:layout_constraintGuide_percent="0.5" />
      
      <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@color/green"
          android:text="Button"
          app:layout_constraintEnd_toStartOf="@+id/guideline"
          app:layout_constraintTop_toTopOf="parent" />
      
      <Button
          android:id="@+id/button2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@android:color/holo_red_dark"
          android:text="Button"
          app:layout_constraintStart_toStartOf="@+id/guideline"
          app:layout_constraintTop_toTopOf="parent" />
      
      </androidx.constraintlayout.widget.ConstraintLayout>
      

      【讨论】:

        【解决方案5】:

        对于这样的构造,您的视图宽度需要为 0dp。同样对于按钮 2,请确保它从按钮 1 到水平方向的父级结束。你还没有表现出你的风格,所以可能还有其他问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-07-29
          • 2013-03-02
          • 1970-01-01
          • 2017-08-24
          • 2014-05-22
          • 2016-08-15
          • 1970-01-01
          相关资源
          最近更新 更多