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

时间:2017-01-05 11:13:40

标签: android android-layout background

我正在linear layout,其中有一个imageview和一个textview,当我触摸linear layout时,它应突出显示图像以及内部文字不同颜色的线性布局。我已经尝试使用设置背景进行线性布局,因为我已经使用ontouch-listener它没有显示颜色,即使我试图以编程方式更改颜色但它没有效果。< / p>

有人可以告诉我在触摸线性布局时如何更改imageviewtextview颜色。

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>

选择器:

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
    android:drawable="@android:drawable/list_selector_background" />

代码:

    li_otherexpense.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            startActivity(new Intent(NewDaybook_Activity.this,     AddingNewExpense.class));
            NewDaybook_Activity.this.finish();
            return false;
        }
    });

2 个答案:

答案 0 :(得分:0)

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

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;
            }
        });

答案 1 :(得分:0)

试试这个,

  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;
            }
        });