如何在android中突出显示线性布局中的选定项

如何在android中突出显示线性布局中的选定项,android,android-layout,background,Android,Android Layout,Background,我有一个线性布局,其中有一个imageview和一个textview,当我触摸线性布局时,它应该用不同的颜色突出显示线性布局内的图像和文本。我曾尝试在按下线性布局时使用set background,因为我使用了ontouch listener,它不显示颜色,即使我尝试通过编程更改颜色,但它不起作用 有人能告诉我当触摸线性布局时,如何更改imageview和textview的颜色吗 Xml: <LinearLayout android:id="@+id/line

我有一个线性布局,其中有一个imageview和一个textview,当我触摸线性布局时,它应该用不同的颜色突出显示线性布局内的图像和文本。我曾尝试在按下线性布局时使用set background,因为我使用了ontouch listener,它不显示颜色,即使我尝试通过编程更改颜色,但它不起作用

有人能告诉我当触摸线性布局时,如何更改imageview和textview的颜色吗

Xml:



    <LinearLayout
        android:id="@+id/linear_otherexpense"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="@drawable/linearlayout_check"
        android:gravity="center"
        android:orientation="vertical"
        android:weightSum="1">

        <ImageView
            android:id="@+id/img_otherexpense"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.7"
            android:scaleType="centerInside"
            android:src="@mipmap/expense"
            android:tint="#6666FF" />

        <TextView
            android:id="@+id/tv_otherexpense"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:gravity="center"
            android:text="@string/dayexpense"
            android:tint="@color/colorPrimary" />
    </LinearLayout>

您可以使用此代码。这对我来说很好。也许会有帮助

findViewById(R.id.linear_otherexpense).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        // When you Touch Down 
                        // U can change Text and Image As per your Functionality  

                        break;
                    case MotionEvent.ACTION_UP:

                        //When you Release the touch 
                        // U can change Text and Image As per your Functionality

                        break;
                }


                return true;
            }
        });
试试这个

  linearLayout = (LinearLayout) findViewById(R.id.linear_otherexpense);
         imageView=(ImageView)findViewById(R.id.img_otherexpense);
        linearLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                linearLayout.setBackgroundColor(getResources().getColor(R.color.colorAccent));
                imageView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
                return true;
            }
        });
findViewById(R.id.linear_otherexpense).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        // When you Touch Down 
                        // U can change Text and Image As per your Functionality  

                        break;
                    case MotionEvent.ACTION_UP:

                        //When you Release the touch 
                        // U can change Text and Image As per your Functionality

                        break;
                }


                return true;
            }
        });
  linearLayout = (LinearLayout) findViewById(R.id.linear_otherexpense);
         imageView=(ImageView)findViewById(R.id.img_otherexpense);
        linearLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                linearLayout.setBackgroundColor(getResources().getColor(R.color.colorAccent));
                imageView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
                return true;
            }
        });