我正在尝试更改cardview中特定约束布局的背景,但我无法弄清楚如何操作。
我有几张卡有几个布局(所有卡都有像unit1Layout,unit2Layout ......)
我想更改点击卡的背景。 我尝试更改了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>
这就是我改变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));
}
}
});
}
}
答案 0 :(得分:0)
我刚刚找到答案:
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));
}
}
});
}
}