更改子视图的背景颜色

时间:2018-01-01 12:14:42

标签: android layout-inflater android-inflate

我有父视图和子视图。子视图以编程方式膨胀。子视图的TextView有一些背景。 TextView上有一个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);
                            }
                        });

1 个答案:

答案 0 :(得分: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);
   }
}