gpt4 book ai didi

java - 具有固定数量子级的 CardView

转载 作者:行者123 更新时间:2023-11-29 18:41:03 25 4
gpt4 key购买 nike

我想要一个恰好显示四张卡片(不多也不少)的 Activity ,其布局与您在下图中看到的相同。我可以在不使用 imo 相当复杂的 RecyclerView 并且不只是复制和粘贴 xml 四次的情况下实现它吗?我可以通过 ID 访问和更改每个 View 也很重要。

我目前有四个 CardView 堆叠在一起,感觉很不对劲。我应该怎么做?

How it should look like

这是我在上面的模型中使用的错误 XML 代码。我把它放在一个 fragment 中,因为它很长而且可能对问题不是很重要:

<?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"
tools:context=".LobbyActivity">

<android.support.v7.widget.CardView
android:id="@+id/player1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toTopOf="@id/player2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintWidth_percent="0.95">

<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/player1Info"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:text="Playername - 100(+3)"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textColor="@color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/player1Thumb"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/player1Ready"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="Ready: true"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="#8A000000"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/player1Thumb"
app:layout_constraintTop_toBottomOf="@+id/player1Info" />

<ImageView
android:id="@+id/player1Thumb"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/ic_lock_lock" />

</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
android:id="@+id/player2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toTopOf="@id/player3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/player1"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintWidth_percent="0.95">

<android.support.constraint.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/player2Info"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:text="Playername - 100(+3)"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textColor="@color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/player2Thumb"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/player2Ready"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="Ready: true"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="#8A000000"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/player2Thumb"
app:layout_constraintTop_toBottomOf="@+id/player2Info" />

<ImageView
android:id="@+id/player2Thumb"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/ic_lock_lock" />

</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
android:id="@+id/player3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toTopOf="@id/player4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/player2"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintWidth_percent="0.95">

<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/player3Info"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:text="Playername - 100(+3)"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textColor="@color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/player3Thumb"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/player3Ready"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="Ready: true"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="#8A000000"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/player3Thumb"
app:layout_constraintTop_toBottomOf="@+id/player3Info" />

<ImageView
android:id="@+id/player3Thumb"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/ic_lock_lock" />

</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
android:id="@+id/player4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/player3"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintWidth_percent="0.95">

<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/player4Info"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:text="Playername - 100(+3)"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textColor="@color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/player4Thumb"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/player4Ready"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="Ready: true"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="#8A000000"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/player4Thumb"
app:layout_constraintTop_toBottomOf="@+id/player4Info" />

<ImageView
android:id="@+id/player4Thumb"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/ic_lock_lock" />

</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

最佳答案

如果卡片 View 看起来相同,您可以创建一个单独的可重用布局,其中只有一个 CardView。然后,您可以使用 <include>标记添加 4 次。这样,如果您需要更改它的外观,只需执行一次。

有关如何实现此目标的更多信息 here .

示例

<include 
android:id="@+id/news_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/title"/>

注意:您可以通过设置 id 来区分每个包含的元素,就像上面的例子一样。

要获取 subview ,您需要其 ID。首先,使用 id 检索 View (或 CardView 或...)。例如:

CardView card1 = view.findViewById(cardView1);
card1.findViewById(player1Info);

与下一张包含的卡相同:

CardView card2 = view.findViewById(cardView2);
card2.findViewById(player1Info); //Notice how it is the same id here <------

关于java - 具有固定数量子级的 CardView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53042457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com