简体   繁体   English

android主题和库项目

[英]android themes and library projects

I have a library project, which is a TabActivity, and redefined some styles to draw my custom ToggleButton with a different background. 我有一个图书馆项目,它是一个TabActivity,并重新定义了一些样式以使用不同的背景绘制自定义的ToggleButton。 I added a styles.xml file: 我添加了一个styles.xml文件:

     <resources>
         <style name="custom_button" parent="@android:style/Widget.Button">
             <item name="android:gravity">center_vertical|center_horizontal</item>
             <item name="android:textColor">#FFFFFFFF</item>
             <item name="android:shadowColor">#FF000000</item>
             <item name="android:shadowDx">0</item>
             <item name="android:shadowDy">-1</item>
             <item name="android:shadowRadius">0.2</item>
             <item name="android:textSize">16dip</item>
             <item name="android:textStyle">bold</item>
             <item name="android:background">@drawable/btn_custom</item>
             <item name="android:focusable">true</item>
             <item name="android:clickable">true</item>
         </style>
     </resources>

And a themes.xml file: 还有一个themes.xml文件:

<resources>
    <style name="custom_theme" parent="android:style/Theme.NoTitleBar">
        <item name="android:buttonStyleToggle">@style/custom_button</item>
    </style>
</resources>

And applied the theme in the manifest file. 并在清单文件中应用了主题。

Everything works ok if I start the activity directly instead of being a library project. 如果我直接开始活动而不是图书馆项目,那么一切正常。 But if I launch this activity (using an intent) from another project, the custom theme is not applied to the toggle buttons, they look like the default android theme. 但是,如果我从另一个项目启动此活动(使用意图),则自定义主题不会应用于切换按钮,它们看起来像默认的android主题。 Other resources are loaded, the library project has some drawables and they are painted ok. 加载了其他资源,库项目中有一些可绘制对象,并且它们被绘制为正常。 I'm not using assets. 我没有使用资产。

Other weird thing is that if I change the button text: 其他奇怪的是,如果我更改按钮文本:

ToggleButton tbLocal = (ToggleButton)findViewById(R.id.tblocal);
tbLocal.setText("Local");

... when the activity is started, text is not changed and the default text (YES/NO) is used. ...当活动开始时,文本不会更改,并且使用默认文本(YES / NO)。 If I start the library project activity directly this doesn't happen neither. 如果我直接启动库项目活动,则不会发生。

What can be wrong? 有什么事吗

You have two manifest files: one in your library project and another in your application project. 您有两个清单文件:一个在您的库项目中,另一个在您的应用程序项目中。 You need to apply your theme in the application AndroidManifest.xml file, and not in the library one (actually it is useless to define <activity> element in the library project manifest). 您需要在应用程序AndroidManifest.xml文件中而不是在库中应用主题(实际上,在库项目清单中定义<activity>元素是没有用的)。

For toggle button on/off text use the following: 对于切换按钮的开/关文本,请使用以下命令:

ToggleButton tbLocal = (ToggleButton)findViewById(R.id.tblocal);
tbLocal.setTextOn("LocalOn");
tbLocal.setTextOff("LocalOff");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM