2012-11-21 40 views
0

我有造型customview的问题。 我想在不同的布局中使用不同的样式。但我的风格不适用。CustomView造型

CustomView布局:

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android"  
     <ImageView 
        android:id="@+id/img"      
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/> 

     <TextView 
        android:id="@+id/txt"  
        android:layout_width="wrap_content" 
       android:layout_height="wrap_content"  
       android:layout_gravity="bottom|center_horizontal"    
</merge> 

在另一个布局定义:

<com.buka.customviews.Bukaview 
     android:id="@+id/bukaId" 
     android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      style="@style/BukaStyle1"> 

styles.xml

<style name="BukaStyle1"> 
     <item name="android:textSize">10sp</item> 
</style> 

<style name="BukaStyle2"> 
     <item name="android:textSize">20sp</item> 
</style> 

由于

回答

0

您应该创建一个自定义属性:
<attr name="BukaStyleTextSize" format="dimensions" />

然后在您的自定义视图布局TextView标签,放:
android:textSize="?attr:BukaStyleTextSize"

最后,在你的样式styles.xml地说:
<item name="BukaStyleTextSize>10sp</item>

+0

仍然不起作用。我认为问题在于我在TextView上定义了BukaStyleTextSize,但是在BukaView上应用了样式。有任何想法吗? – Hoochwo

+0

我也这么认为。你能否将BukaView的属性传递给java类中的TextView?为什么不使用带有drawableLeft或drawableTop的TextView? – yDelouis

+0

其实我有更复杂的布局,我需要设置像大小/颜色和其他许多参数TextView中。我无法从代码中的attr中设置样式。 – Hoochwo