自定义view的构造方法和样式主题

来源:互联网 发布:手柄要下载什么软件 编辑:程序博客网 时间:2024/05/01 00:59
//java代码中使用view时调用public Button(Context context) {    this(context, null);}//XML文件中使用view时调用,用于layout文件实例化,会把XML内的参数通过AttributeSet带入到View内。第二个参数中就包含自定义的属性。public Button(Context context, AttributeSet attrs) {    this(context, attrs, 0);}//主题的style信息,也会从XML里带入.从xml加载时执行和应用一个特定的风格。public Button(Context context, AttributeSet attrs, int defStyle) {    super(context, attrs, defStyle);}

Android 风格与主题(style and theme)

1、什么是Style,什么是Theme?

Style 和 theme:是一个包含一种 或者 多种格式化 属性 的集合 ,并且 style和theme都是资源,存放在res/values/styles.xml 文件夹下 即可,style和theme的定义格式相同,在res.values中建立一个styles.xml,然后创建节点resources-style-item。
不过style是针对view来说的,比如 TextView,EditText这些,而Theme必须针对整个activity或者 整个application,你必须在AndroidManifest.xml中 的或者中定义。

先来看看style,比如如下一段代码:

<?xml version="1.0" encoding="utf-8"?><resources>    <style name="CodeFont" parent="@android:style/TextAppearance.Medium">        <item name="android:layout_width">fill_parent</item>        <item name="android:layout_height">wrap_content</item>        <item name="android:textColor">#00FF00</item>        <item name="android:typeface">monospace</item>    </style></resources>

可以看到这个style的名字为CodeFont。parent后面就是父类的style,CodeFont继承这个父类的属性.可以看到这个父类的style是android中默认的,你也可以继承你自定义的style,这时候不需要再写 parent属性,而是使用ContFont.red这样的方式,而且你可以继续继承,写成ContFont.red.small。 接下来 每一个item定义一个属性。定义属性的最好方法就是在api文档里找到这个view的xml属性,比如在EditText中有InputType 这个属性,那么在你的style里面你就可以来定义它。

这样一个style就写好了。

使用也非常简单,我们只要在写我们的view时,加入style标签就可以了,就像这样

<TextView    style="@style/CodeFont"    android:text="@string/hello" />

下面讲讲主题,主题需要在AndroidManifest.xml中注册。如果你想整个程序都使用这个主题,你可以这样写

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

如果你只需要在某个Activity中使用主题,那么只要在Activity标签中写入android:theme= 就可以了,android有很多好的默认主题,比如

<activity     android:theme="@android:style/Theme.Dialog">

这就会使你的整个Activity变成一个对话框形式。
或者,如果你希望背景是透明的,可以这样写

<activity     android:theme="@android:style/Theme.Translucent">

同样的我们也可以继承父类theme,写法和style一样。你也可以自己定义一个theme,写个例子

<?xml version="1.0" encoding="utf-8"?><resources> <style name="CustomTheme">     <item name="android:windowNoTitle">true</item>     <item name="windowFrame">@drawable/screen_frame</item>    <item name="windowBackground">@drawable/screen_background_white</item>     <item name="panelForegroundColor">#FF000000</item>     <item name="panelBackgroundColor">#FFFFFFFF</item>    <item name="panelTextColor">?panelForegroundColor</item>    <item name="panelTextSize">14</item>    <item name="menuItemTextColor">?panelTextColor</item>     <item name="menuItemTextSize">?panelTextSize</item> </style></resources>

如果你要在Java代码中加载主题的话,只要用setTheme(R.style.CustomTheme)就可以了,不过记得一定要在初始化任何view之前,比如一定要放在我们常用的setContentView()之前。通常,不建议这么做。

Android系统的 themes.xml 和 style.xml (位于/base/core/res/res/values/) 包含了很多系统定义好的style,建议在里面挑个合适的,然后再继承修改。

下边是SDK中主题的一个例子:

<?xml version="1.0" encoding="utf-8"?> <resources>  <style name="CustomTheme">      <item name="android:windowNoTitle">true</item>      <item name="windowFrame">@drawable/screen_frame</item>      <item name="windowBackground">@drawable/screen_background_white</item>      <item name="panelForegroundColor">#FF000000</item>      <item name="panelBackgroundColor">#FFFFFFFF</item>      <item name="panelTextColor">?panelForegroundColor</item>       <item name="panelTextSize">14</item>       <item name="menuItemTextColor">?panelTextColor</item>       <item name="menuItemTextSize">?panelTextSize</item>   </style> </resources>

注意:我们用了@符号和?符号来应用资源。
@符号 表明 我们引用的资源是前边定义过的(或者在前一个项目中或者在Android 框架中)。
问号?表明 我们引用的资源的值在 当前的 主题当中定义过。

通过引用在里边定义的名字 可以做到(panelTextColor 用的颜色 和 panelForegroundColor中定义的一样 )。这中技巧只能用在XML资源当中

在程序中 使用主题的方法:

protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);      setTheme(android.R.style.Theme_Light);      setContentView(R.layout.linear_layout_3); }

如果你喜欢一个主题,但是想做一些轻微的改变,你只需要将这个主题添加为父主题。比如我们修改Theme.Dialog主题。我们来继承Theme.Dialog来生成一个新的主题。

<style name=”CustomDialogTheme”             parent=”@android:style/Theme.Dialog” >

继承了Theme.Dialog后,我们可以按照我们的要求来调整主题。我们可以修改在Theme.Dialog中定义的每个item元素的值,然后我们在Android Manifest 文件中使用CustomDialogTheme 而不是 Theme.Dialog 。

Android中自定义样式与View的构造函数中的第三个参数defStyle的意义

Android 风格与主题(style and theme)

Android 样式和主题(style & theme)

0 0
原创粉丝点击