我有惯例认为:
public class MyView extends RelativeLayout {
private LayoutInflater inflater;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.my_view, this);
}
}
注
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"/>
我还有一个背景选择:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/top_press" /> <!-- pressed -->
<item android:drawable="@drawable/top_idle" /> <!-- default -->
</selector>
当我以挑选人为背景时,它就没有工作:
<com.example.MyView style="@style/someStyle"
android:background="@drawable/item_selector_top" android:clickable="true"/>
然而,当我从内部Xml带去背景时,它确实发挥了作用:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/item_selector_top"/>
谁知道为什么?