有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

具有固定数量子项的java CardView

我想要一个活动,显示正好四张卡片(从来没有更多或更少),具有与下图中相同的布局。我是否可以在不使用imo相当复杂的RecyclerView和just copy&;粘贴xml四次?同样重要的是,我可以通过id访问和更改每个视图

我现在有四个卡片视图堆叠在一起,感觉很不对。我应该怎么做

How it should look like

下面是我在上面的模型中使用的糟糕的XML代码。我把它放在一个片段中,因为它相当长,可能对问题不太重要:

<?xml version="1.0" encoding="utf-8"?> <安卓.support.constraint.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓" xmlns:app="http://schemas.安卓.com/apk/res-auto" xmlns:tools="http://schemas.安卓.com/tools" 安卓:layout_width="match_parent" 安卓:layout_height="match_parent" tools:context=".LobbyActivity"> <安卓.support.v7.widget.CardView 安卓:id="@+id/player1" 安卓:layout_width="0dp" 安卓:layout_height="wrap_content" 安卓:layout_gravity="center" 安卓: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"> <安卓.support.constraint.ConstraintLayout xmlns:app="http://schemas.安卓.com/apk/res-auto" 安卓:layout_width="match_parent" 安卓:layout_height="wrap_content"> <TextView 安卓:id="@+id/player1Info" 安卓:layout_width="0dp" 安卓:layout_height="wrap_content" 安卓:layout_marginStart="16dp" 安卓:layout_marginTop="24dp" 安卓:layout_marginEnd="16dp" 安卓:text="Playername - 100(+3)" 安卓:textAppearance="@style/TextAppearance.AppCompat.Headline" 安卓:textColor="@color/colorPrimary" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/player1Thumb" app:layout_constraintTop_toTopOf="parent" /> <TextView 安卓:id="@+id/player1Ready" 安卓:layout_width="0dp" 安卓:layout_height="wrap_content" 安卓:layout_marginStart="16dp" 安卓:layout_marginTop="16dp" 安卓:layout_marginEnd="16dp" 安卓:text="Ready: true" 安卓:textAppearance="@style/TextAppearance.AppCompat.Body1" 安卓:textColor="#8A000000" 安卓:textSize="18sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/player1Thumb" app:layout_constraintTop_toBottomOf="@+id/player1Info" /> <ImageView 安卓:id="@+id/player1Thumb" 安卓:layout_width="80dp" 安卓:layout_height="80dp" 安卓:layout_marginStart="16dp" 安卓:layout_marginTop="16dp" 安卓:layout_marginBottom="16dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@安卓:drawable/ic_lock_lock" /> </安卓.support.constraint.ConstraintLayout> </安卓.support.v7.widget.CardView> <安卓.support.v7.widget.CardView 安卓:id="@+id/player2" 安卓:layout_width="0dp" 安卓:layout_height="wrap_content" 安卓:layout_gravity="center" 安卓: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"> <安卓.support.constraint.ConstraintLayout xmlns:app="http://schemas.安卓.com/apk/res-auto" 安卓:layout_width="match_parent" 安卓:layout_height="wrap_content"> <TextView 安卓:id="@+id/player2Info" 安卓:layout_width="0dp" 安卓:layout_height="wrap_content" 安卓:layout_marginStart="16dp" 安卓:layout_marginTop="24dp" 安卓:layout_marginEnd="16dp" 安卓:text="Playername - 100(+3)" 安卓:textAppearance="@style/TextAppearance.AppCompat.Headline" 安卓:textColor="@color/colorPrimary" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/player2Thumb" app:layout_constraintTop_toTopOf="parent" /> <TextView 安卓:id="@+id/player2Ready" 安卓:layout_width="0dp" 安卓:layout_height="wrap_content" 安卓:layout_marginStart="16dp" 安卓:layout_marginTop="16dp" 安卓:layout_marginEnd="16dp" 安卓:text="Ready: true" 安卓:textAppearance="@style/TextAppearance.AppCompat.Body1" 安卓:textColor="#8A000000" 安卓:textSize="18sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/player2Thumb" app:layout_constraintTop_toBottomOf="@+id/player2Info" /> <ImageView 安卓:id="@+id/player2Thumb" 安卓:layout_width="80dp" 安卓:layout_height="80dp" 安卓:layout_marginStart="16dp" 安卓:layout_marginTop="16dp" 安卓:layout_marginBottom="16dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@安卓:drawable/ic_lock_lock" /> </安卓.support.constraint.ConstraintLayout> </安卓.support.v7.widget.CardView> <安卓.support.v7.widget.CardView 安卓:id="@+id/player3" 安卓:layout_width="0dp" 安卓:layout_height="wrap_content" 安卓:layout_gravity="center" 安卓: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"> <安卓.support.constraint.ConstraintLayout xmlns:app="http://schemas.安卓.com/apk/res-auto" 安卓:layout_width="match_parent" 安卓:layout_height="wrap_content"> <TextView 安卓:id="@+id/player3Info" 安卓:layout_width="0dp" 安卓:layout_height="wrap_content" 安卓:layout_marginStart="16dp" 安卓:layout_marginTop="24dp" 安卓:layout_marginEnd="16dp" 安卓:text="Playername - 100(+3)" 安卓:textAppearance="@style/TextAppearance.AppCompat.Headline" 安卓:textColor="@color/colorPrimary" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/player3Thumb" app:layout_constraintTop_toTopOf="parent" /> <TextView 安卓:id="@+id/player3Ready" 安卓:layout_width="0dp" 安卓:layout_height="wrap_content" 安卓:layout_marginStart="16dp" 安卓:layout_marginTop="16dp" 安卓:layout_marginEnd="16dp" 安卓:text="Ready: true" 安卓:textAppearance="@style/TextAppearance.AppCompat.Body1" 安卓:textColor="#8A000000" 安卓:textSize="18sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/player3Thumb" app:layout_constraintTop_toBottomOf="@+id/player3Info" /> <ImageView 安卓:id="@+id/player3Thumb" 安卓:layout_width="80dp" 安卓:layout_height="80dp" 安卓:layout_marginStart="16dp" 安卓:layout_marginTop="16dp" 安卓:layout_marginBottom="16dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@安卓:drawable/ic_lock_lock" /> </安卓.support.constraint.ConstraintLayout> </安卓.support.v7.widget.CardView> <安卓.support.v7.widget.CardView 安卓:id="@+id/player4" 安卓:layout_width="0dp" 安卓:layout_height="wrap_content" 安卓:layout_gravity="center" 安卓: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"> <安卓.support.constraint.ConstraintLayout xmlns:app="http://schemas.安卓.com/apk/res-auto" 安卓:layout_width="match_parent" 安卓:layout_height="wrap_content"> <TextView 安卓:id="@+id/player4Info" 安卓:layout_width="0dp" 安卓:layout_height="wrap_content" 安卓:layout_marginStart="16dp" 安卓:layout_marginTop="24dp" 安卓:layout_marginEnd="16dp" 安卓:text="Playername - 100(+3)" 安卓:textAppearance="@style/TextAppearance.AppCompat.Headline" 安卓:textColor="@color/colorPrimary" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/player4Thumb" app:layout_constraintTop_toTopOf="parent" /> <TextView 安卓:id="@+id/player4Ready" 安卓:layout_width="0dp" 安卓:layout_height="wrap_content" 安卓:layout_marginStart="16dp" 安卓:layout_marginTop="16dp" 安卓:layout_marginEnd="16dp" 安卓:text="Ready: true" 安卓:textAppearance="@style/TextAppearance.AppCompat.Body1" 安卓:textColor="#8A000000" 安卓:textSize="18sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/player4Thumb" app:layout_constraintTop_toBottomOf="@+id/player4Info" /> <ImageView 安卓:id="@+id/player4Thumb" 安卓:layout_width="80dp" 安卓:layout_height="80dp" 安卓:layout_marginStart="16dp" 安卓:layout_marginTop="16dp" 安卓:layout_marginBottom="16dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@安卓:drawable/ic_lock_lock" /> </安卓.support.constraint.ConstraintLayout> </安卓.support.v7.widget.CardView> </安卓.support.constraint.ConstraintLayout>

共 (5) 个答案

  1. # 1 楼答案

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

    更多关于如何实现这个here的信息

    示例

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

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

    要获取子视图,您需要它的id。首先,使用id检索视图(或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 <------  
    
  2. # 2 楼答案

    使用LinearLayout。。。(可选)使用唯一的id属性转换为4次include

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.LinearLayoutCompat
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical">
    
        <android.support.design.card.MaterialCardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp">
            <Space
                android:layout_width="match_parent"
                android:layout_height="80dp"/>
        </android.support.design.card.MaterialCardView>
    
        <android.support.design.card.MaterialCardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp">
            <Space
                android:layout_width="match_parent"
                android:layout_height="80dp"/>
        </android.support.design.card.MaterialCardView>
    
        <android.support.design.card.MaterialCardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp">
            <Space
                android:layout_width="match_parent"
                android:layout_height="80dp"/>
        </android.support.design.card.MaterialCardView>
    
        <android.support.design.card.MaterialCardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp">
            <Space
                android:layout_width="match_parent"
                android:layout_height="80dp"/>
        </android.support.design.card.MaterialCardView>
    
    </android.support.v7.widget.LinearLayoutCompat>
    

    这看起来很像你在那里看到的:

    XML preview screenshot

  3. # 3 楼答案

    如果您正在使用RecyclerView,请使用^{}来实现这一点

    List<String> dataSource = new ArrayList<String>();
    
    
    RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        recyclerView.setLayoutParams(lp);
    
    
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
        setContentView(recyclerView);
        recyclerView.setAdapter(new TextRecyclerAdapter(dataSource));
        recyclerView.setLayoutManager(gridLayoutManager);
    

    here is git example for this

  4. # 4 楼答案

    如前所述,当您不需要额外的视图组来获得良好的UI性能时,可以使用here<merge/>

    The tag helps eliminate redundant view groups in your view hierarchy when including one layout within another. For example, if your main layout is a vertical LinearLayout in which two consecutive views can be re-used in multiple layouts, then the re-usable layout in which you place the two views requires its own root view. However, using another LinearLayout as the root for the re-usable layout would result in a vertical LinearLayout inside a vertical LinearLayout. The nested LinearLayout serves no real purpose other than to slow down your UI performance.

    <merge xmlns:android="http://schemas.android.com/apk/res/android">
    
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/add"/>
    
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/delete"/>
    
    </merge>
    

    Now, when you include this layout in another layout (using the <include/> tag), the system ignores the <merge> element and places the two buttons directly in the layout, in place of the <include/> tag.

    希望这对你有帮助

  5. # 5 楼答案

    当然可以。 在你的情况下,回收者观点不会给你带来任何好处

    但不要忘记创建style或使用<include>

    如果未来的设计会有所改变,这将对你有所帮助

    在你的情况下,我建议使用include。你可以读一下here。主要想法是创建传统的cardview_布局。xml及其使用

    <include layout="@layout/cardview_layout"/>
    

    在主xml文件中