如何在Android中继承布局样式

时间:2014-09-03 08:51:14

标签: android android-layout

我尝试在Android版面xml中继承样式。 现在看起来像:

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在具有@style/AppTheme.ButtonLayout样式的RelativeLayout中具有样式。当它没有这种风格的内部布局时,它应该看起来像默认的Android TextView。

1 个答案:

答案 0 :(得分:3)

您可以从以下的style.xml示例继承Android layout 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>