2014-09-03 38 views
3

我试图在Android布局xml中继承样式。 现在它看起来像:如何在Android中布局中继承样式

layout.xml:

<RelativeLayout 
    android:id="@id/btn" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    style="@style/AppTheme.ButtonLayout"> 

    <TextView 
     android:id="@id/btn_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     style="@style/AppTheme.ButtonLayout.TextView" /> 

</RelativeLayout> 

styles.xml:

<style name="AppTheme.ButtonLayout"> 
    <item name="android:textViewStyle">@style/AppTheme.ButtonLayout.TextView</item> 
</style> 

<style name="AppTheme.ButtonLayout.TextView" parent="android:Widget.Holo.Light.TextView"> 
    <item name="android:textColor">@android:color/white</item> 
</style> 

我想摆脱从TextView的style属性,但它应该继承外观RelativeLayout的。可能吗?

编辑:我希望这个TextView具有这样的风格只有在RelativeLayout与@style/AppTheme.ButtonLayout风格。当它没有这种风格的内部布局时,它应该看起来像默认的Android TextView。

回答

2

你可以在以下继承自style.xml例如Android的布局XML风格..

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
     <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.prj.name" 
     android:versionCode="1" 
     android:versionName="1.0" > 

    <uses-sdk 
    android:minSdkVersion="11" 
    android:targetSdkVersion="19" /> 

    <application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 

    <activity> </activity> 
    </application> 
</manifest> 

themes_apptheme.xml你可以调用来自父系的名称申报名称

<?xml version="1.0" encoding="utf-8"?> 
<!-- Generated with http://android-holo-colors.com --> 
<resources xmlns:android="http://schemas.android.com/apk/res/android"> 

<style name="AppTheme" parent="@style/_AppTheme" /> 

<style name="_AppTheme" parent="android:Theme.Holo.Light"> 

    <item name="android:textColorHighlight">#99ff6d00</item> 
     <item name="android:textViewStyle">@style/TextViewAppTheme</item> 
</style> 

styles_apptheme.xml //你想要什么设计,你可以从绘制文件夹中调用与PNG格式

<style name="TextViewAppTheme" parent="android:Widget.Holo.Light.TextView"> 
    <item name="android:background">@drawable/apptheme_text_holo_light</item> 
</style> 

style.xml

<style name="Widget.Holo.Light.TextView" parent="Widget.TextView"> 
    <item name="android:textcolor"> .........</item> 
</style> 

<style name="Widget.TextView"> 
    <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item> 
    <item name="android:gravity">center_vertical</item> 
</style> 
+0

谢谢,但你的做法是全球性的。当它在带样式集的RelativeLayout中时,我需要改变TextView的样式。 – milus 2014-09-03 10:57:56

+0

2014-09-03 11:13:34