在重定向之前按下时替换imageview图像

时间:2016-06-22 06:42:18

标签: android android-fragments imageview

我有几个图像内部线性布局。我想在重定向到下一个活动之前点击更改图像源。我的线性布局最终被包含在片段中。我在主要活动中使用过的那个片段。我该怎么做才能改变来源?

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="false"
        android:layout_alignParentStart="false"
        android:layout_alignParentEnd="false">

        <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
            android:layout_margin="5dp">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/background"
            android:layout_weight="1"
            android:clickable="true"
            android:onClick="setContentView"
            android:id="@+id/campus"
            android:contextClickable="false">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/acad"
                android:src="@drawable/nap"
                android:layout_marginLeft="24dp"
                android:layout_marginTop="22dp"
                android:layout_marginBottom="2dp"  />


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="ACADEMIA"
                android:id="@+id/textView"
                android:textColor="#993300"
                android:textAlignment="center" />
        </LinearLayout>

The image shows the layout I am trying to change, an image with brown b/g is changing to white

enter image description here

1 个答案:

答案 0 :(得分:0)

选择drawable selector.xml

在正常的

点击和打盹后,将nap_click绘制成你想要的东西
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/nap_click" android:state_pressed= "true"/>
    <item android:drawable="@drawable/nap"/>

</selector>

并在xml中设置为您的src或背景

  android:src="@drawable/selector"

  android:background="@drawable/selector"

你想要这样......

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="false"
>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:layout_weight="3"
  >

    <LinearLayout

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/selector"
        android:layout_weight="1"
        android:clickable="true"
        android:onClick="setContentView"
        android:orientation="vertical"
        android:id="@+id/campus"

        android:weightSum="1"

        android:contextClickable="false">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/acad"
            android:src="@drawable/apply_job"

            android:layout_marginTop="22dp"
            android:layout_marginBottom="2dp"  />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ACADEMIA"
            android:id="@+id/textView"
            android:textColor="#993300"
            android:textAlignment="center" />
    </LinearLayout>

    <LinearLayout

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/selector"
        android:layout_weight="1"
        android:clickable="true"
        android:onClick="setContentView"
        android:orientation="vertical"
        android:id="@+id/campus2"
        android:gravity="center"
        android:weightSum="1"
        android:contextClickable="false">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/acad2"
            android:src="@drawable/apply_job"

            android:layout_marginTop="22dp"
            android:layout_marginBottom="2dp"  />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ACADEMIA"
            android:id="@+id/textView2"
            android:textColor="#993300"
            android:textAlignment="center" />
    </LinearLayout>

    <LinearLayout

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/selector"
        android:layout_weight="1"
        android:clickable="true"
        android:onClick="setContentView"
        android:orientation="vertical"
        android:id="@+id/campus1"
        android:gravity="center"
        android:weightSum="1"
        android:contextClickable="false">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/acad1"
            android:src="@drawable/apply_job"

            android:layout_marginTop="22dp"
            android:layout_marginBottom="2dp"  />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ACADEMIA"
            android:id="@+id/textView1"
            android:textColor="#993300"
            android:textAlignment="center" />
    </LinearLayout>
    </LinearLayout>


<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:layout_weight="3"
    >

    <LinearLayout

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/selector"
        android:layout_weight="1"
        android:clickable="true"
        android:onClick="setContentView"
        android:orientation="vertical"
        android:id="@+id/campus3"

        android:weightSum="1"

        android:contextClickable="false">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/acad3"
            android:src="@drawable/apply_job"

            android:layout_marginTop="22dp"
            android:layout_marginBottom="2dp"  />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ACADEMIA"
            android:id="@+id/textView3"
            android:textColor="#993300"
            android:textAlignment="center" />
    </LinearLayout>

    <LinearLayout

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/selector"
        android:layout_weight="1"
        android:clickable="true"
        android:onClick="setContentView"
        android:orientation="vertical"
        android:id="@+id/campus5"
        android:gravity="center"
        android:weightSum="1"
        android:contextClickable="false">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/acad5"
            android:src="@drawable/apply_job"

            android:layout_marginTop="22dp"
            android:layout_marginBottom="2dp"  />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ACADEMIA"
            android:id="@+id/textView5"
            android:textColor="#993300"
            android:textAlignment="center" />
    </LinearLayout>

    <LinearLayout

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/selector"
        android:layout_weight="1"
        android:clickable="true"
        android:onClick="setContentView"
        android:orientation="vertical"
        android:id="@+id/campus4"
        android:gravity="center"
        android:weightSum="1"
        android:contextClickable="false">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/acad4"
            android:src="@drawable/apply_job"

            android:layout_marginTop="22dp"
            android:layout_marginBottom="2dp"  />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ACADEMIA"
            android:id="@+id/textView4"
            android:textColor="#993300"
            android:textAlignment="center" />
    </LinearLayout>
</LinearLayout>



<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:layout_weight="3"
    android:layout_margin="5dp">

    <LinearLayout

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/selector"
        android:layout_weight="1"
        android:clickable="true"
        android:onClick="setContentView"
        android:orientation="vertical"
        android:id="@+id/campus6"

        android:weightSum="1"

        android:contextClickable="false">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/acad6"
            android:src="@drawable/apply_job"

            android:layout_marginTop="22dp"
            android:layout_marginBottom="2dp"  />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ACADEMIA"
            android:id="@+id/textView6"
            android:textColor="#993300"
            android:textAlignment="center" />
    </LinearLayout>

    <LinearLayout

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/selector"
        android:layout_weight="1"
        android:clickable="true"
        android:onClick="setContentView"
        android:orientation="vertical"
        android:id="@+id/campus7"
        android:gravity="center"
        android:weightSum="1"
        android:contextClickable="false">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/acad7"
            android:src="@drawable/apply_job"

            android:layout_marginTop="22dp"
            android:layout_marginBottom="2dp"  />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ACADEMIA"
            android:id="@+id/textView7"
            android:textColor="#993300"
            android:textAlignment="center" />
    </LinearLayout>

    <LinearLayout

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/selector"
        android:layout_weight="1"
        android:clickable="true"
        android:onClick="setContentView"
        android:orientation="vertical"
        android:id="@+id/campus8"
        android:gravity="center"
        android:weightSum="1"
        android:contextClickable="false">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/acad8"
            android:src="@drawable/apply_job"

            android:layout_marginTop="22dp"
            android:layout_marginBottom="2dp"  />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ACADEMIA"
            android:id="@+id/textView8"
            android:textColor="#993300"
            android:textAlignment="center" />
    </LinearLayout>
</LinearLayout>



<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:layout_weight="3"
    android:layout_margin="5dp">

    <LinearLayout

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/selector"
        android:layout_weight="1"
        android:clickable="true"
        android:onClick="setContentView"
        android:orientation="vertical"
        android:id="@+id/campus9"

        android:weightSum="1"

        android:contextClickable="false">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/acad9"
            android:src="@drawable/apply_job"

            android:layout_marginTop="22dp"
            android:layout_marginBottom="2dp"  />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ACADEMIA"
            android:id="@+id/textView9"
            android:textColor="#993300"
            android:textAlignment="center" />
    </LinearLayout>

    <LinearLayout

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/selector"
        android:layout_weight="1"
        android:clickable="true"
        android:onClick="setContentView"
        android:orientation="vertical"
        android:id="@+id/campus10"
        android:gravity="center"
        android:weightSum="1"
        android:contextClickable="false">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/acad10"
            android:src="@drawable/apply_job"

            android:layout_marginTop="22dp"
            android:layout_marginBottom="2dp"  />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ACADEMIA"
            android:id="@+id/textView10"
            android:textColor="#993300"
            android:textAlignment="center" />
    </LinearLayout>

    <LinearLayout

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/selector"
        android:layout_weight="1"
        android:clickable="true"
        android:onClick="setContentView"
        android:orientation="vertical"
        android:id="@+id/campus11"
        android:gravity="center"
        android:weightSum="1"
        android:contextClickable="false">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/acad11"
            android:src="@drawable/apply_job"

            android:layout_marginTop="22dp"
            android:layout_marginBottom="2dp"  />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ACADEMIA"
            android:id="@+id/textView11"
            android:textColor="#993300"
            android:textAlignment="center" />
    </LinearLayout>
</LinearLayout>
</LinearLayout>

selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="#993400" android:state_pressed= "true"/>
    <item android:drawable="#FFFFFF"/>

</selector>

apply_job.png在这里.png或其他你按下其他按下或正常

你setonclicklistner在布局上像

campus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Change the textColor on click 


       // txtHelp.setTextColor();

        }
    });

或使用了TouchonListner ....