`

android 界面组件添加定制属性(转)

阅读更多

In tutorial #4.1, I mentioned that we passed custom attributes for the text and image variables from the XML resource file to our custom class. This is a critical skill for performing true object-oriented programming and how to do it wasn’t obvious from Google’s Android API Demos.

Luckily I was pointed to the solution myself by an experienced Android programmer in Guatemala by the username of cadlg (thanks again!). If you want to see the official Google Android example though, look at Android’s APIDemos’ custom LabelView example.

So here we go. We’ll use the same code as Tutorial 4.1 to keep this simple.

 

Setting Up Your Custom Class’s XML Resource Files

We’ll only review the code for the TextOnlyButton as it’s identical in concept to the ImageOnlyButton.

First we’ll create a new file in /res/values called attrs.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>

<declare-styleable name=”TextOnlyButton”>

<attr name=”textColorNotFocused” format=”integer”/>
<attr name=”textColorFocused” format=”integer”/>

</declare-styleable>

</resources>

As you see, we first declared a ‘styleable’ with the name of our custom Class. Two attributes were then added to contain the values of our focused & unfocused text colors. By default, attributes have values of String, but in our case, we needed integers to represent the resource id’s we’ll declare in our colors.xml file. You can also declare formats such as “boolean” & others if that suits the requirements of your own project.

Next, we declare values for these custom attributes in our layout’s XML file: tutorial4.xml

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”

xmlns:pj=”http://schemas.android.com/apk/res/com.pocketjourney.tutorials”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:padding=”10px”>

<com.pocketjourney.view.TextOnlyButton

android:id=”@+id/text_only_button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginTop=”5px”
style=”?android:attr/buttonStyleSmall”
android:text=”Text Button”
pj:textColorNotFocused=”@drawable/white”
pj:textColorFocused=”@drawable/android_orange”/>

</LinearLayout>

Referring to our new attributes is actually a two step process. First we declared a new namespace (in our case called ‘pj’ as short for PocketJourney) in the parent layout of our custom class:

xmlns:pj=”http://schemas.android.com/apk/res/com.pocketjourney.tutorials”

Next we specified the values of our new attributes in the XML usage of our TextOnlyButton:

pj:textColorNotFocused=”@drawable/white”
pj:textColorFocused=”@drawable/android_orange”

Now you can see why we specified our format=”integer”. Our custom attributes point to the resource id’s of colors specified in our colors.xml file.

Retrieving Custom Attributes During Class Instantiation

Since our Activity has many constructors, we delegate the attribute parsing to an init() method to keep our code clean.

int notFocusedTextColor, focusedTextColor;

private void init(AttributeSet attrs) {

Resources.StyledAttributes a = getContext().obtainStyledAttributes(attrs,R.styleable.TextOnlyButton);
notFocusedTextColor = a.getColor(R.styleable.TextOnlyButton_textColorNotFocused, 0xFF000000);
focusedTextColor = a.getColor(R.styleable.TextOnlyButton_textColorFocused, 0xFF000000);

}

By now you’ve undoubtedly seen the AttributeSet that is always passed into an Activity. Well now you get to use it. First we obtain the StyledAttributes instance by requesting just the StyledAttributes for our custom Class. Next, we call the getColor() and pass two variables: the name of the attribute we want along with a default value in case the user did not specify one.

Take note of our styled attribute’s name as it’s a combination of our custom class’s name and the attribute we specified in the attrs.xml file (e.g.TextOnlyButton_textColorNotFocused).

And That’s It

You can now readily pass your own custom attributes and keep your View variables cleanly enclosed in your XML files. You can download the source to see for yourself. Just look at Tutorial #4.

分享到:
评论

相关推荐

    Android 5.0 新组件Demo

    在Android 5.0(代号Lollipop)的更新中,谷歌引入了多个新组件以提升用户体验和...通过学习这个Demo,开发者能够掌握Android 5.0新组件的基本用法,提升自己的开发技能,同时也能理解这些组件如何改善用户界面和体验。

    android 界面编写工具

    每个组件都有可定制的属性,如大小、颜色、字体、对齐方式等。开发者可以通过界面直接设置这些属性,观察实时效果,这有助于快速迭代和微调设计。 4. **布局管理**: Android支持多种布局管理器,如LinearLayout、...

    android 自定义View并添加属性

    本教程将深入探讨如何在Android中自定义View并为其添加属性,以实现更灵活的界面定制。 首先,自定义View通常涉及到继承一个现有的View类,如View、TextView或ImageView等,或者直接继承ViewGroup来创建自定义容器...

    Android自定义组件开发详解.docx

    【Android自定义组件开发详解】 Android自定义组件的开发是Android应用开发中的一个重要部分,它涉及到自定义View和ViewGroup的创建,以及对canvas和paint的深入理解和运用。自定义组件能够满足开发者对于UI设计的...

    Android布局属性总结

    `android:id`用于给视图分配唯一标识,便于查找和引用,`android:tag`则可以添加自定义标签方便查找和逻辑处理,以及`android:scrollbarThumbHorizontal`等滚动条相关属性,可以自定义滚动条的样式。 总的来说,...

    Android自定义组件一[文].pdf

    1. **定义属性**:在 attrs.xml 文件中定义组件所需的属性,如TitleViewPager的tLayout、bImage和bMargin等,属性类型可以参考Android官方文档。 2. **创建布局资源**:在 item.xml 文件中定义组件的布局结构,如...

    “可动态布局”的Android抽屉组件之构建基础

    在`xml`布局文件中,我们通常会将其作为根视图,并设置相应的属性,如`android:layout_width="match_parent"`、`android:layout_height="match_parent"`以及`android:fitsSystemWindows="true"`,以确保抽屉能正确...

    Android代码-SurfaceView添加组件view不被组件覆盖.zip

    在Android开发中,SurfaceView是一种特殊的视图,它允许开发者在应用程序中创建高性能的图形界面,比如游戏或者视频播放器。通常,SurfaceView有一个独立的渲染线程,用于处理高耗时的图形操作,以避免阻塞主线程,...

    非常炫酷的android界面

    在Android开发中,创建一个“非常炫酷的android界面”是一项关键任务,这不仅可以提升用户体验,也能让应用在众多应用中脱颖而出。在这个过程中,“androidgridview”是一个常用的组件,它可以帮助我们实现美观且...

    Android漂亮好看的登陆,注册界面!程序源码

    首先,我们要理解Android界面设计的基本原则。在Android中,界面通常由各种组件(Widgets)组成,如EditText用于输入文本,Button用于触发动作,ImageView用于显示图片等。这些组件可以通过XML布局文件进行定义和...

    封装一个基于iOS与Android双平台的Toast组件和加载Loading组件

    为了保证在iOS和Android上的表现一致,需要使用平台特定的样式和组件属性。 - 使用`Platform`模块来检测运行环境,并根据平台进行条件渲染,以确保组件在iOS和Android上的视觉效果符合各自平台的设计规范。 - 注意...

    Android-Android可定制的三态开关按钮

    这个组件通常被称为TriStateToggleButton,它扩展了Android原生的ToggleButton,以满足开发者对更丰富用户界面的需求。 在Android开发中,Button和ToggleButton是最常见的控件,它们用于接收用户的点击事件并展示...

    Android图形用户界面(UI)开发教程合集

    对于更高级的用户界面设计,Android支持手势识别、动画效果和可定制的视图。手势识别让应用可以响应用户的滑动、点击等动作,增加交互性。动画则可以为界面添加动态效果,提升用户体验。例如,Activity的过渡动画、...

    android自定义的翻转textview

    同时,我们需要添加一些自定义属性,如翻转速度、是否循环翻转等,以便开发者可以根据需求进行定制。 ```java public class AnimTextView extends TextView { // 添加自定义属性 private int flipDuration; ...

    android 日期选择组件

    在Android开发中,日期选择组件是用户界面设计中不可或缺的一部分,尤其在需要用户输入或选择特定日期的应用场景中。"android 日期选择组件"通常指的是一个可以方便用户选择日期的UI控件,它通常包括对日、月和年的...

    android界面效果汇总

    "android界面效果汇总"这个主题包含了Android UI设计的多种技术和实践,旨在为开发者提供一套全面的设计资源和指南。 1. **Material Design**:Android的官方设计语言,强调层次、动作和颜色的使用,提供了丰富的...

    androidHtml5多属性选择

    在实现HTML5多属性选择时,我们首先需要在布局文件中添加一个WebView,通过设置其加载的网页URL或HTML字符串来呈现HTML5的选择界面。 2. HTML5表单元素:HTML5提供了多种表单元素,如`&lt;select&gt;`、`...

    android_textview的属性.zip_android_android 属性

    在Android开发中,TextView是用于显示单行或多行文本的视图组件,它在界面设计中扮演着至关重要的角色。本教程将深入讲解`android_textview`的属性及其使用,帮助开发者更好地理解和应用这些属性来定制TextView的...

    Android自定义组件解析[郝留有]

    自定义控件的设计与实现,不仅要求开发者深刻理解Android的UI架构和绘制流程,还需掌握如何有效利用GDI等基础技术,以构建高效、美观且功能丰富的用户界面组件。通过上述步骤和技巧,开发者能够灵活地创建满足特定...

    Android 仿微信聊天界面

    在Android开发中,仿微信聊天界面是一个常见的需求,它涉及到许多关键技术和组件的集成。这个项目可能包括消息列表的展示、输入框设计、表情键盘、发送按钮、未读消息提示等多个方面。以下是一些相关的重要知识点: ...

Global site tag (gtag.js) - Google Analytics