Android开发-selector-setSelected-AndroidStudio

来源:互联网 发布:mac微信接收的文件 编辑:程序博客网 时间:2024/04/30 17:26



selector_1.xml:

<?xml version="1.0" encoding="utf-8" ?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_selected="false"        android:drawable="@drawable/png01" />    <item android:state_selected="true"        android:drawable="@drawable/png02" /></selector>

selector_2.xml:

<?xml version="1.0" encoding="utf-8" ?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_selected="false"        android:drawable="@drawable/png03" />    <item android:state_selected="true"        android:drawable="@drawable/png04" /></selector>
activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:background="#ffffff"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity" >    <FrameLayout        android:id="@+id/oneFragment_container"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_above="@+id/buttonList">    </FrameLayout>    <FrameLayout        android:id="@+id/twoFragment_container"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_above="@+id/buttonList">    </FrameLayout>    <FrameLayout        android:id="@+id/threeFragment_container"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_above="@+id/buttonList">    </FrameLayout>    <View        android:layout_width="fill_parent"        android:layout_height="1px"        android:background="?android:attr/listDivider"        android:layout_below="@+id/oneFragment_container"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true" />    <!--我是一条黑线-->    <ImageView        android:layout_above="@+id/buttonList"        android:layout_width="fill_parent"        android:layout_height="0.1dp"        android:background="#000000" />    <LinearLayout        android:id="@+id/buttonList"        android:background="#ffffff"        android:layout_width="fill_parent"        android:layout_height="50sp"        android:layout_alignParentBottom="true"        android:orientation="horizontal" >        <!--android:id="@+id/oneLinearLayout"-->        <LinearLayout            android:id="@+id/oneLinearLayout"            android:layout_margin="1dp"            android:layout_width="match_parent"            android:layout_weight="1"            android:orientation="vertical"            android:layout_height="match_parent">            <ImageView                android:id="@+id/imageView_1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:src="@drawable/selector_1" />        </LinearLayout>        <!--android:id="@+id/rbAddress"-->        <LinearLayout            android:id="@+id/twoLinearLayout"            android:layout_margin="1dp"            android:layout_width="match_parent"            android:layout_weight="1"            android:orientation="vertical"            android:layout_height="match_parent">            <ImageView                android:id="@+id/imageView_2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:src="@drawable/selector_2" />        </LinearLayout>        <!--android:id="@+id/threeLinearLayout"-->        <LinearLayout            android:id="@+id/threeLinearLayout"            android:layout_margin="1dp"            android:layout_width="match_parent"            android:layout_weight="1"            android:orientation="vertical"            android:layout_height="match_parent">            <ImageView                android:id="@+id/imageView_3"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:src="@drawable/selector_3" />        </LinearLayout>    </LinearLayout></RelativeLayout>
MainActivity.java:

点击ImageView_1:

imageView_1.setSelected(true);imageView_2.setSelected(false);imageView_3.setSelected(false);

点击ImageView_2:

imageView_1.setSelected(false);imageView_2.setSelected(true);imageView_3.setSelected(false);

点击ImageView_3:

imageView_1.setSelected(false);imageView_2.setSelected(false);imageView_3.setSelected(true);


1 0
原创粉丝点击