2012-06-06 45 views
0

我想使用一个9补丁可绘制作为布局的背景,我设定为可点击9为一个可点击的视图

我用抽拉用补丁可拉伸实际上是一个选择器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有显示,按o当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); 
+0

似乎,它应该工作。 – Michael

+0

我知道。我知道9-patch本身不是问题(除非有9个补丁有时可以工作,有时它们不会是 – piojo

+0

而且如果你在他们的位置更改drawables?哪一个出现? –

回答

0

也似乎夸大这种观点并将其添加到该活动的onCreate布局是一个问题,造成了这个问题。

我移动了充气并在onResume之后添加了视图并且它工作正常。

我想是的原因是ViewGroup我添加了充气的视图,并没有一个尺寸。

相关问题