更改子视图的背景颜色

问题描述 投票:0回答:1

我有父视图和子视图。子视图以编程方式膨胀。儿童视图有一些背景的TextViewTextView上有一个onclick事件。我想要的是当用户点击第一个TextView时它的背景颜色应该改变,当用户选择第二个时,第一个textview的背景应该回到它的默认背景,第二个应该改变。

我改变了背景颜色但是我很难恢复背景。这是我的代码:

我的父母观点:

<com.google.android.flexbox.FlexboxLayout
    android:id="@+id/viewCategoriesLinearlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:flexWrap="wrap"
    app:alignItems="stretch"
    app:alignContent="stretch"
    android:layout_marginTop="@dimen/_10sdp"
    android:layout_marginLeft="@dimen/_5sdp"
    android:layout_marginStart="@dimen/_5sdp" />

我的孩子观点:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">

    <TextView
        android:id="@+id/categoryChip"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/category_tag_background"
        android:text="TAG"
        android:layout_marginTop="@dimen/_25sdp"
        android:paddingLeft="@dimen/_5sdp"
        android:paddingTop="@dimen/_5sdp"
        android:paddingBottom="@dimen/_5sdp"
        android:paddingRight="@dimen/_5sdp"
        android:layout_marginLeft="@dimen/_5sdp"
        android:layout_marginRight="@dimen/_5sdp"
        android:textColor="@color/textcolorLogin"
        android:textSize="@dimen/_11ssp" />

    <TextView
        android:id="@+id/categorychipid"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:visibility="gone"/>


</LinearLayout>

这就是我膨胀和改变背景颜色的方式:

    FlexboxLayout categoryInformationHeader = view.findViewById(R.id.viewCategoriesLinearlayout);
    final View editChildView = getActivity().getLayoutInflater().inflate(R.layout.tag_layout, null);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                                                ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(10,2,10,2);
    editChildView.setLayoutParams(layoutParams);
    editParentLL.addView(editChildView);
    final TextView editTvChip = editChildView.findViewById(R.id.chip12345);
    final String shelfShareCategoryTitle = crsEditShelfShare.getString(crsEditShelfShare.getColumnIndex("title"));

    editTvChip.setText(shelfShareCategoryTitle);

        editTvChip.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                editTvChip.setBackgroundResource(R.color.colorPrimary);
                            }
                        });
android layout-inflater android-inflate
1个回答
0
投票

当您一次添加一个子视图时,将其保存在array变量上:

private ArrayList<TextView> childViews = new ArrayList<>(); //add this

@Override 
public void onCreate(Bundle saveInstanceState) {
    ....
}

点击按钮后,将其添加到您的arraylist

childViews.add(editTvChip); //add it to your arraylist, so we can reset it
editTvChip.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
      clearBackgrounds(); //call reset background first
      editTvChip.setBackgroundResource(R.color.colorPrimary);
   }
});

添加此方法以清除textView背景:

/**
* Reset all the background of each textViews
*
*/
private void clearBackgrounds() {
   for (TextView textView : childViews) {
      textView.setBackgroundResource(R.color.yourDefaultBackground);
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.