【问题标题】:Android studio 4 Button on full screen [closed]全屏显示Android Studio 4按钮[关闭]
【发布时间】:2019-12-18 10:23:14
【问题描述】:

我是android studio的初学者,谁能告诉我如何设计4个按钮覆盖整个屏幕

【问题讨论】:

    标签: android android-layout android-constraintlayout


    【解决方案1】:

    使用 ConstraintLayout

    试试这个
    <?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"
        android:orientation="vertical">
    
        <ImageView
            android:id="@+id/imgOne"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/colorPrimary"
            app:layout_constraintBottom_toTopOf="@+id/imgThree"
            app:layout_constraintEnd_toStartOf="@id/imgTwo"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="NEW ITEM"
            android:textColor="@color/colorBlack"
            app:layout_constraintBottom_toBottomOf="@id/imgOne"
            app:layout_constraintEnd_toStartOf="@id/imgTwo"
            app:layout_constraintStart_toStartOf="parent" />
    
        <ImageView
            android:id="@+id/imgTwo"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/colorblue"
            app:layout_constraintBottom_toTopOf="@+id/imgFour"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@id/imgOne"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="NEW ITEM"
            android:textColor="@color/colorBlack"
            app:layout_constraintBottom_toBottomOf="@id/imgTwo"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@id/imgTwo" />
    
        <ImageView
            android:id="@+id/imgThree"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/colorGreen"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/imgTwo"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imgOne" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="NEW ITEM"
            android:textColor="@color/colorBlack"
            app:layout_constraintBottom_toBottomOf="@id/imgThree"
            app:layout_constraintEnd_toStartOf="@id/imgFour"
            app:layout_constraintStart_toStartOf="parent" />
    
        <ImageView
            android:id="@+id/imgFour"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/colorAccent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@id/imgThree"
            app:layout_constraintTop_toBottomOf="@+id/imgTwo" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="NEW ITEM"
            android:textColor="@color/colorBlack"
            app:layout_constraintBottom_toBottomOf="@id/imgThree"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@id/imgTwo" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

      【解决方案2】:

      这是最简单的方法。

      线性布局

      <?xml version="1.0" encoding="utf-8"?>
      <androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
      
          <androidx.appcompat.widget.LinearLayoutCompat
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="1">
      
              <androidx.appcompat.widget.AppCompatImageButton
                  android:layout_width="0dp"
                  android:layout_height="match_parent"
                  android:layout_weight="1" />
      
              <androidx.appcompat.widget.AppCompatImageButton
                  android:layout_width="0dp"
                  android:layout_height="match_parent"
                  android:layout_weight="1" />
          </androidx.appcompat.widget.LinearLayoutCompat>
      
          <androidx.appcompat.widget.LinearLayoutCompat
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="1">
      
              <androidx.appcompat.widget.AppCompatImageButton
                  android:layout_width="0dp"
                  android:layout_height="match_parent"
                  android:layout_weight="1" />
      
              <androidx.appcompat.widget.AppCompatImageButton
                  android:layout_width="0dp"
                  android:layout_height="match_parent"
                  android:layout_weight="1" />
          </androidx.appcompat.widget.LinearLayoutCompat>
      </androidx.appcompat.widget.LinearLayoutCompat>
      

      约束布局

      <?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.appcompat.widget.AppCompatImageButton
              android:id="@+id/ib1"
              android:layout_width="0dp"
              android:layout_height="0dp"
              app:layout_constraintBottom_toTopOf="@+id/ib3"
              app:layout_constraintEnd_toStartOf="@id/ib2"
              app:layout_constraintHorizontal_bias="0.5"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent" />
      
          <androidx.appcompat.widget.AppCompatImageButton
              android:id="@+id/ib2"
              android:layout_width="0dp"
              android:layout_height="0dp"
              app:layout_constraintBottom_toTopOf="@+id/ib4"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintHorizontal_bias="0.5"
              app:layout_constraintStart_toEndOf="@id/ib1"
              app:layout_constraintTop_toTopOf="parent" />
      
          <androidx.appcompat.widget.AppCompatImageButton
              android:id="@+id/ib3"
              android:layout_width="0dp"
              android:layout_height="0dp"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toStartOf="@id/ib2"
              app:layout_constraintHorizontal_bias="0.5"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toBottomOf="@+id/ib1" />
      
          <androidx.appcompat.widget.AppCompatImageButton
              android:id="@+id/ib4"
              android:layout_width="0dp"
              android:layout_height="0dp"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintHorizontal_bias="0.5"
              app:layout_constraintStart_toEndOf="@id/ib3"
              app:layout_constraintTop_toBottomOf="@+id/ib2" />
      </androidx.constraintlayout.widget.ConstraintLayout>
      

      【讨论】:

      • 为什么要使用如此复杂的视图层次结构而不是简单的 ConstraintLayout?
      • @barotia 这是 ConstraintLayout 的更新答案。
      猜你喜欢
      • 1970-01-01
      • 2018-06-18
      • 1970-01-01
      • 2020-07-21
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      相关资源
      最近更新 更多