我试图找到最好的方法来使用Android Studio中的布局XML创建响应式网格布局。
首先让我解释一下设计。 如下所示,我想开发一个包含10张卡的界面。 随着设备尺寸的变化,我希望纸牌的高度缩小,然后分别缩小纸牌内的比赛(根据我可以施加的限制)。
所以我做了几次尝试。 首先,我尝试了约束布局(如下所述)。 这导致卡的高度由卡内的内容定义,因此,在较小的设备上查看时,卡的大小不会改变。
然后我尝试了GridLayout。 再次导致卡的大小由内容决定,布局无法动态缩放。
因此,我试图找出下一步可以尝试的方法,以便获得所需的结果。 也许使用Cards是个坏主意? 我应该改用线性布局吗? 有没有我可以参考的例子? 要么 选择不同分辨率的不同布局是可以的吗?
此时问题太多。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/midGuide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintGuide_percent="0.50121653"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/l1_title"
android:layout_width="0dp"
android:layout_height="85dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:fontFamily="@font/anton"
android:paddingStart="0dp"
android:text="1"
android:textColor="#FFFFFF"
android:textSize="50sp"
app:autoSizeTextType="uniform"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/bestScoretext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:text="Best Score"
android:textAlignment="textEnd"
android:textColor="#FFFFFF"
android:textSize="14sp"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toTopOf="@+id/bestScore"
app:layout_constraintEnd_toEndOf="@+id/bestScore" />
<TextView
android:id="@+id/bestScore"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:fontFamily="sans-serif-medium"
android:text="1.9pt"
android:textAlignment="viewEnd"
android:textColor="#FFFFFF"
android:textSize="38sp"
app:autoSizeTextType="uniform"
app:layout_constraintBaseline_toBaselineOf="@+id/l1_title"
app:layout_constraintEnd_toEndOf="parent" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="20dp"
android:background="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/l1c6"
app:srcCompat="?attr/homeAsUpIndicator" />
<!-- Card 1 -->
<androidx.cardview.widget.CardView
android:id="@+id/l1c0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="5dp"
android:layout_marginTop="10dp"
android:padding="5dp"
app:cardBackgroundColor="@color/bgHome"
app:cardCornerRadius="12dp"
app:cardElevation="0dp"
app:cardMaxElevation="0dp"
app:layout_constraintEnd_toStartOf="@+id/midGuide"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/l1_title"
app:layout_constraintBottom_toTopOf="@+id/l1c2">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rounded"
android:padding="10dp">
<TextView
android:id="@+id/subLevel0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=".0"
android:textColor="@color/cardview_light_background"
android:textSize="30sp"
app:autoSizeTextType="uniform"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/subScore0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3.8pt"
android:textColor="@color/yellow"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/c0Guide"
app:layout_constraintStart_toStartOf="@+id/subLevel0" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/subLevel0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.81"
app:layout_constraintStart_toEndOf="@+id/c0Guide"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_1_0_icon" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/c0Guide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginEnd="75dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintGuide_begin="51dp"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<!-- Card 2 -->
<androidx.cardview.widget.CardView
android:id="@+id/l1c1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="20dp"
android:padding="5dp"
app:cardBackgroundColor="@color/bgHome"
app:cardCornerRadius="12dp"
app:cardElevation="0dp"
app:cardMaxElevation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/midGuide"
app:layout_constraintTop_toTopOf="@+id/l1c0">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rounded"
android:padding="10dp">
<TextView
android:id="@+id/subLevel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=".1"
android:textColor="@color/cardview_light_background"
android:textSize="30sp"
app:autoSizeTextType="uniform"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/subScore1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4.2pt"
android:textColor="@color/yellow"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/subLevel1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/subLevel1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.81"
app:layout_constraintStart_toEndOf="@+id/c1Guide"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_1_1_icon" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/c1Guide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginEnd="75dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintGuide_begin="51dp"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<!-- Card 3 -->
<androidx.cardview.widget.CardView
android:id="@+id/l1c2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="5dp"
android:layout_marginTop="10dp"
android:padding="5dp"
app:cardBackgroundColor="@color/bgHome"
app:cardCornerRadius="12dp"
app:cardElevation="0dp"
app:cardMaxElevation="0dp"
app:layout_constraintEnd_toStartOf="@+id/midGuide"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/l1c0"
app:layout_constraintBottom_toTopOf="@+id/l1c4">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rounded"
android:padding="10dp">
<TextView
android:id="@+id/subLevel2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=".2"
android:textColor="@color/cardview_light_background"
android:textSize="30sp"
app:autoSizeTextType="uniform"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/subScore2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3.8pt"
android:textColor="@color/yellow"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/c2Guide"
app:layout_constraintStart_toStartOf="@+id/subLevel2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/subLevel2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.81"
app:layout_constraintStart_toEndOf="@+id/c2Guide"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_1_2_icon" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/c2Guide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginEnd="75dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintGuide_begin="51dp"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<!-- Card 4 -->
<androidx.cardview.widget.CardView
android:id="@+id/l1c3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="20dp"
android:padding="5dp"
app:cardBackgroundColor="@color/bgHome"
app:cardCornerRadius="12dp"
app:cardElevation="0dp"
app:cardMaxElevation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/midGuide"
app:layout_constraintTop_toTopOf="@+id/l1c2">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rounded"
android:padding="10dp">
<TextView
android:id="@+id/subLevel3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=".3"
android:textColor="@color/cardview_light_background"
android:textSize="30sp"
app:autoSizeTextType="uniform"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/subScore3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4.2pt"
android:textColor="@color/yellow"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/subLevel3" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/subLevel3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.81"
app:layout_constraintStart_toEndOf="@+id/c3Guide"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_1_3_icon" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/c3Guide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginEnd="75dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintGuide_begin="51dp"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<!-- Card 5 -->
<androidx.cardview.widget.CardView
android:id="@+id/l1c4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="5dp"
android:layout_marginTop="10dp"
android:padding="5dp"
app:cardBackgroundColor="@color/bgHome"
app:cardCornerRadius="12dp"
app:cardElevation="0dp"
app:cardMaxElevation="0dp"
app:layout_constraintEnd_toStartOf="@+id/midGuide"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/l1c2"
app:layout_constraintBottom_toTopOf="@+id/l1c6">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rounded"
android:padding="10dp">
<TextView
android:id="@+id/subLevel4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=".4"
android:textColor="@color/cardview_light_background"
android:textSize="30sp"
app:autoSizeTextType="uniform"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/subScore4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3.8pt"
android:textColor="@color/yellow"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/c4Guide"
app:layout_constraintStart_toStartOf="@+id/subLevel4" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/subLevel4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.81"
app:layout_constraintStart_toEndOf="@+id/c4Guide"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_1_0_icon" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/c4Guide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginEnd="75dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintGuide_begin="51dp"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<!-- Card 6 -->
<androidx.cardview.widget.CardView
android:id="@+id/l1c5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="20dp"
android:padding="5dp"
app:cardBackgroundColor="@color/bgHome"
app:cardCornerRadius="12dp"
app:cardElevation="0dp"
app:cardMaxElevation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/midGuide"
app:layout_constraintTop_toTopOf="@+id/l1c4">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rounded"
android:padding="10dp">
<TextView
android:id="@+id/subLevel5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=".5"
android:textColor="@color/cardview_light_background"
android:textSize="30sp"
app:autoSizeTextType="uniform"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/subScore5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4.2pt"
android:textColor="@color/yellow"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/subLevel5" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/subLevel5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.81"
app:layout_constraintStart_toEndOf="@+id/c5Guide"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_1_3_icon" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/c5Guide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginEnd="75dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintGuide_begin="51dp"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<!-- Card 7 -->
<androidx.cardview.widget.CardView
android:id="@+id/l1c6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="5dp"
android:layout_marginTop="10dp"
android:padding="5dp"
app:cardBackgroundColor="@color/bgHome"
app:cardCornerRadius="12dp"
app:cardElevation="0dp"
app:cardMaxElevation="0dp"
app:layout_constraintEnd_toStartOf="@+id/midGuide"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/l1c4"
app:layout_constraintBottom_toTopOf="@+id/l1c8">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rounded"
android:padding="10dp">
<TextView
android:id="@+id/subLevel6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=".6"
android:textColor="@color/cardview_light_background"
android:textSize="30sp"
app:autoSizeTextType="uniform"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/subScore6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3.8pt"
android:textColor="@color/yellow"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/c6Guide"
app:layout_constraintStart_toStartOf="@+id/subLevel6" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/subLevel6"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.81"
app:layout_constraintStart_toEndOf="@+id/c6Guide"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_1_0_icon" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/c6Guide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginEnd="75dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintGuide_begin="51dp"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<!-- Card 8 -->
<androidx.cardview.widget.CardView
android:id="@+id/l1c7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="20dp"
android:padding="5dp"
app:cardBackgroundColor="@color/bgHome"
app:cardCornerRadius="12dp"
app:cardElevation="0dp"
app:cardMaxElevation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/midGuide"
app:layout_constraintTop_toTopOf="@+id/l1c6">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rounded"
android:padding="10dp">
<TextView
android:id="@+id/subLevel7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=".7"
android:textColor="@color/cardview_light_background"
android:textSize="30sp"
app:autoSizeTextType="uniform"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/subScore7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4.2pt"
android:textColor="@color/yellow"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/subLevel7" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/subLevel7"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.81"
app:layout_constraintStart_toEndOf="@+id/c5Guide"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_1_3_icon" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/c7Guide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginEnd="75dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintGuide_begin="51dp"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<!-- Cards end -->
<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="left" />
</androidx.constraintlayout.widget.ConstraintLayout>
答案 0 :(得分:0)
尝试将每张卡的宽度设置为0dp
,然后在每张卡周围添加边距。这样,宽度将水平扩展,并使高度保持最小(wrap_content
)。
您也可以签出flexbox-layout。