简体   繁体   English

如何改变视野中的孩子的背景

[英]How to change background of a child in view

I'm trying to change the background of a particular constraint layout in a cardview but I can't figure out how to do it. 我正在尝试在cardview中更改特定约束布局的背景,但是我不知道该怎么做。

I have several cards with several layouts (all of them have id like unit1Layout,unit2Layout...) 我有几张具有几种布局的卡片(它们的id都像unit1Layout,unit2Layout ...)

I want to change the background of the clicked card. 我想更改点击的卡片的背景。 I tried changing the cardview's background but I'm getting as shown in the photo. 我尝试更改cardview的背景,但正如图所示。

    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="256"
    android:fillViewport="true">

    <GridLayout
        android:id="@+id/mainGrid"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:alignmentMode="alignMargins"
        android:columnCount="1"
        android:columnOrderPreserved="false"
        android:padding="14dp"
        android:rowCount="10">


        <!-- Row 1 -->
        <android.support.v7.widget.CardView
            android:id="@+id/unit1Card"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="16dp"
            android:layout_rowWeight="1"
            android:background="@drawable/cardbackground"
            app:cardCornerRadius="8dp"
            app:cardElevation="5dp">

            <android.support.constraint.ConstraintLayout
                android:id="@+id/unit1Layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:background="@drawable/cardbackground">

                <TextView
                    android:id="@+id/unit1Label"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:fontFamily="@font/myfont"
                    android:text="Unit 1"
                    android:textColor="@color/fontColor"
                    android:textSize="28sp"
                    android:textStyle="bold"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <TextView
                    android:id="@+id/unit1Words"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="16dp"
                    android:fontFamily="@font/myfont"
                    android:text="423 Words"
                    android:textColor="@color/fontColor"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/unit1Label" />

                <TextView
                    android:id="@+id/unit1Green"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="8dp"
                    android:fontFamily="@font/myfont"
                    android:text="358"
                    android:textColor="@color/fontColor"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toStartOf="@+id/unit1Yellow"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/unit1Label" />

                <TextView
                    android:id="@+id/unit1Yellow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="8dp"
                    android:fontFamily="@font/myfont"
                    android:text="32"
                    android:textColor="@color/fontColor"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/unit1Label" />

                <TextView
                    android:id="@+id/unit1Red"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="8dp"
                    android:fontFamily="@font/myfont"
                    android:text="8"
                    android:textColor="@color/fontColor"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toEndOf="@+id/unit1Yellow"
                    app:layout_constraintTop_toBottomOf="@+id/unit1Label" />
            </android.support.constraint.ConstraintLayout>
        </android.support.v7.widget.CardView>

this is how Im changing the cardview's background 这就是我如何更改cardview的背景

private void setSingleEvent(GridLayout mainGrid){
        for(int i = 0;i<mainGrid.getChildCount();i++){
            final int finalI = i;
            final CardView cardView = (CardView)mainGrid.getChildAt(i);
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if(unitNumbers[finalI+1] == 0) {
                        unitNumbers[finalI+1] = finalI+1;
                        cardView.setBackground(ContextCompat.getDrawable(cardView.getContext(), R.drawable.cardbackgroundselected));
                    }
                    else {
                        unitNumbers[finalI+1] = 0;
                        [![enter image description here][1]][1]
                        cardView.setBackground(ContextCompat.getDrawable(cardView.getContext(), R.drawable.cardbackground));

                    }
                }
            });
        }
}

I just found the answer: 我刚刚找到了答案:

private void setSingleEvent(GridLayout mainGrid){
        for(int i = 0;i<mainGrid.getChildCount();i++){
            final int finalI = i;
            final CardView cardView = (CardView)mainGrid.getChildAt(i);
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String layoutID = "unit"+ (finalI + 1) + "Layout";
                    ConstraintLayout constraintLayout = findViewById(getResources()
                            .getIdentifier(layoutID,"id",getPackageName()));
                    Log.d("unit i layout", "unit"+ (finalI + 1) + "Layout");
                    if(unitNumbers[finalI+1] == 0) {
                        unitNumbers[finalI+1] = finalI+1;

                        constraintLayout.setBackground(ContextCompat
                                .getDrawable(constraintLayout.getContext(),R.drawable.cardbackgroundselected));
                    }
                    else {
                        unitNumbers[finalI+1] = 0;
                        constraintLayout.setBackground(ContextCompat
                                .getDrawable(constraintLayout.getContext(),R.drawable.cardbackground));

                    }
                }
            });
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM