我想使用一个 9-patch drawable 作为我设置为可点击的布局背景
我使用的drawable实际上是一个选择器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="@drawable/action_item_pressed" />
<item android:drawable="@drawable/action_item" />
</selector>
我的布局是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:clickable="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:padding="8dp"
android:background="@drawable/action_item_btn">
<ImageView
android:id="@+id/icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/sword_item"
android:scaleType="centerInside"/>
<ProgressBar
android:id="@+id/progress"
android:layout_width="35dp"
android:layout_height="5dp"
android:layout_marginTop="2dp"
style="@android:style/Widget.ProgressBar.Horizontal"/>
</LinearLayout>
问题是在按下视图时action_item drawable
没有显示并且正在显示。action_item_pressed drawable
有什么想法吗?
我夸大这种观点的方式会导致这种情况吗?
顺便说一句:在 Eclipse 图形布局中,它正在显示,如果我在应用程序运行时添加视图,它就可以工作。
这是我扩充此视图的方式(它从onCreate
方法调用):
int achievementsNumber = AchievementsManager.ACTIVE_ACHIEVEMENTS_COUNT;
m_achievementsLayout = new LinearLayout(m_context.getApplicationContext());
m_achievementsLayout.setOrientation(LinearLayout.VERTICAL);
m_achievementsLayout.setGravity(Gravity.CENTER);
LayoutInflater inflater = (LayoutInflater)m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for(int i=0; i < achievementsNumber; i++){
inflater.inflate(R.layout.achievement_item, m_achievementsLayout, true);
}
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
addView(m_achievementsLayout, params);