在应用程序自定义视图(Android版) [英] Custom Views in the application (Android)

查看:113
本文介绍了在应用程序自定义视图(Android版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些项目,如按钮,复选框被拉伸,进度条,我想那些定义为可绘我beeing应用程序使用的所有按钮的默认样式。

I've a drawable for some items like buttons, checkboxes, progress bars and I'd like to define those drawables for beeing the default style of all the buttons my app uses.

这有可能无需单独每个View宣告风格?

Is that possible without declaring the style in each View individually?

如果答案是使用一个主题为应用程序我倒是AP preciate一个例子(改变例如所有按钮的样式),因为我不知道如何去定义它。

If the answer is to use a Theme for the app I'd appreciate an example (changing the style of all the buttons for example) because I don't understand how to define it.

在此先感谢

推荐答案

答案是使用一个主题,您所期望。良好的文档可以在Android开发者网站上找到: http://developer.android.com /guide/topics/ui/themes.html

The answer is to use a theme as you were expecting. Good documentation can be found on android developer site: http://developer.android.com/guide/topics/ui/themes.html

有关,你可以做这样的按钮:
定义一个新的主题:

For the button you can do like this: Define a new theme:

<style name="MyTheme" parent="@android:style/Theme.Holo">
    <item name="android:buttonStyle">@style/button</item>
</style>

当按钮样式定义为:

Where the button style is defined as:

<style name="button" parent="@android:style/Widget.Button">
    <item name="android:background">@drawable/button_background_selector</item>
    <item name="android:textColor">@drawable/button_text_selector</item>
</style>

该button_background_selector的定义是这样的:

The button_background_selector is defined like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/button_pressed" android:state_pressed="true"></item>
    <item android:drawable="@drawable/button_disabled" android:state_enabled="false"></item>
    <item android:drawable="@drawable/button_default"></item>

</selector>

所有这些图形内容可以是你的图像 - button_ pressed可以在绘制文件夹PNG格式。
您也可以将主题应用到您的应用程序在这样的清单:

All those drawables can be your images - button_pressed can be a png in the drawable folders. You also have to apply the theme to your application in the manifest like this:

<application
    android:theme="@style/MyTheme" >
</application>

希望这有助于:)

Hope this helps :)

这篇关于在应用程序自定义视图(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆