效果说明:点击后,弹出窗口,并且被点击项背景颜色改变
布局文件:
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/corner_all">
<TextView
android:id="@+id/aa_ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello world"
android:textSize="20sp"
android:padding="10dp"
android:drawableLeft="@drawable/uc_one"
android:drawablePadding="10dp"
android:drawableEnd="@drawable/list_arrow"
android:clickable="true"
// android:focusable="true"
// android:focusableInTouchMode="true"
android:background="@drawable/selector_draw"
/>
<!-- android:background="@android:drawable/list_selector_background" -->
<View
android:layout_width="fill_parent"
android:layout_height="1.0dip"
android:layout_marginLeft="15.0dip"
android:layout_marginRight="15.0dip"
android:background="#ffc0c0c0" />
<TextView
android:id="@+id/aa_ll2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello world"
android:textSize="20sp"
android:drawableLeft="@drawable/uc_two"
android:padding="10dp"
android:drawablePadding="10dp"
android:drawableEnd="@drawable/list_arrow"
android:clickable="true"
// android:focusable="true"
// android:focusableInTouchMode="true"
android:background="@drawable/selector_draw" />
<View
android:layout_width="fill_parent"
android:layout_height="1.0dip"
android:layout_marginLeft="15.0dip"
android:layout_marginRight="15.0dip"
android:background="#ffc0c0c0" />
<TextView
android:id="@+id/aa_ll3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello world"
android:textSize="20sp"
android:drawableLeft="@drawable/uc_three"
android:padding="10dp"
android:drawablePadding="10dp"
android:drawableEnd="@drawable/list_arrow"
android:clickable="true"
// android:focusable="true"
// android:focusableInTouchMode="true"
android:background="@drawable/selector_draw" />
</LinearLayout>
res/drawable/selector_draw.xml(点击效果)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/blue" />
<item android:state_focused="true" android:drawable="@color/blue" />
<item android:drawable="@color/whilte" />
</selector>
res/drawable/corner_all.xml(圆角效果)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<stroke android:width="2dp" android:color="#fad3cf" />
<corners android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topRightRadius="10dp"
android:topLeftRadius="10dp" />
</shape>
res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="blue">#1200ff</color>
<color name="whilte">#00000000</color>
</resources>
activity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
System.out.println("onCreate");
TextView tv_a = (TextView)findViewById(R.id.aa_ll1);
TextView tv_b = (TextView)findViewById(R.id.aa_ll2);
TextView tv_c = (TextView)findViewById(R.id.aa_ll3);
tv_a.setOnClickListener(new onClickListenerImpl());
tv_b.setOnClickListener(new onClickListenerImpl());
tv_c.setOnClickListener(new onClickListenerImpl());
}
/*** 监听按钮
**/
private class onClickListenerImpl implements OnClickListener{
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.aa_ll1:
Toast.makeText(getApplicationContext(), "first", Toast.LENGTH_LONG).show();
break;
case R.id.aa_ll2:
Toast.makeText(getApplicationContext(), "two", Toast.LENGTH_LONG).show();
break;
case R.id.aa_ll3:
Toast.makeText(getApplicationContext(), "three", Toast.LENGTH_LONG).show();
break;
}
}
}
源码:http://download.csdn.net/detail/clpliji/4596023
另一种实现相同效果的xml:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:background="@drawable/preference_first_item"
android:clickable="true"
android:gravity="center_vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="个人信息"
android:textColor="#000"
android:textSize="17sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="true"
android:scaleType="matrix"
android:src="@drawable/mm_submenu" />
</RelativeLayout>
</LinearLayout>
在这个LinearLayout中,重复RelativeLayout,改变它的背景图片(图片是.9.png格式的,三个图片,上面圆角,下面圆角,中间直角,用圆角xml替代图片实现效果不好),对它进行监听,也可以实现上面的效果