如何在Android布局中继承样式

5

我尝试在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个回答

5
您可以从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 //你想要设计的样式可以从drawable文件夹中以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>

谢谢,但你的方法是全局的。我需要在RelativeLayout中设置样式时改变TextView的样式。 - milus
<TExt View ..... style="@style/_AppTeam 在您声明父名称的位置调用。 - Htut

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接