【问题标题】:ConstraintLayout view size responsiveConstraintLayout 视图大小响应
【发布时间】:2020-06-12 08:35:01
【问题描述】:

我正在尝试使用ConstraintLayout 作为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 作为父级的同时做到这一点。

有没有办法做到这一点?

【问题讨论】:

    标签: android xml user-interface layout android-constraintlayout


    【解决方案1】:

    使用Guideline 喜欢:

    <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="0dp"
        android:layout_height="45dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="15dp"
        android:layout_marginTop="12dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/guideline_vertical"
        app:layout_constraintTop_toBottomOf="@id/splash_facebook_btn" />
    
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline_vertical"
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5"/>
    
    <Button
        android:id="@+id/splash_sign_up_btn"
        android:layout_height="0dp"
        android:layout_width="0dp"
        android:layout_marginEnd="15dp"
        android:layout_marginStart="15dp"
        app:layout_constraintBottom_toBottomOf="@id/splash_sign_in_btn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/guideline_vertical"
        app:layout_constraintTop_toTopOf="@id/splash_sign_in_btn"
        app:layout_constraintVertical_bias="0.0"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

      【解决方案2】:

      为左键设置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>
      

      【讨论】:

      • 这可以工作,但我正在寻找只涉及 xml 的更简单的东西。感谢您的回答。
      • 我只使用 XML 版本进行了编辑。它在您的ConstraintLayout 中包含LinearLayout
      • 我知道你可以用LinearLayoutlayout_weight 做到这一点,在问题中提到了
      • 检查然后回答@Frank D. with guideline
      【解决方案3】:

      移除固定值,为这些按钮添加约束:左和右,并将它们的宽度设置为 0dp。 here's the image 结果如下:

      <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">
      
      <Button
          android:id="@+id/splash_facebook_btn"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_margin="15dp"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          tools:text="continua cu cancer frate" />
      
      <Button
          android:id="@+id/splash_sign_in_btn"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_marginTop="12dp"
          android:layout_marginEnd="16dp"
          app:layout_constraintEnd_toStartOf="@id/splash_sign_up_btn"
          app:layout_constraintStart_toStartOf="@id/splash_facebook_btn"
          app:layout_constraintTop_toBottomOf="@id/splash_facebook_btn"
          tools:text="intra aici frate" />
      
      <Button
          android:id="@+id/splash_sign_up_btn"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_marginStart="16dp"
          android:layout_marginTop="12dp"
          app:layout_constraintEnd_toEndOf="@id/splash_facebook_btn"
          app:layout_constraintStart_toEndOf="@id/splash_sign_in_btn"
          app:layout_constraintTop_toBottomOf="@id/splash_facebook_btn"
          tools:text="Creeaza cont frate" />
      

      PS:我添加了 tools:text 以便在 Android Studio 预览中出现一些文本。 PSS:祝你好运

      【讨论】:

      • Mersi omule :))
      猜你喜欢
      • 2018-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 2021-01-21
      • 2017-04-10
      • 2019-08-09
      • 1970-01-01
      相关资源
      最近更新 更多