`

android中如何自定义attributes

 
阅读更多
写程序中可能需要用到一些自定义的view控件,这样就需要增加一些自定义的属性。

比如说我要做个股票报价的TextView,涨是红色,跌是绿色

1。先在values目录下创建styles.xml

<resources>

<style name=" Widget. MyTextView"  parent="android:Widget" >

<item name="positiveColor" >#FFFF0000</item>

<item name="negetiveColor" >#FF00FF00</item>

</style>

</resources>

2。上面的步骤完成后,在values目录下创建一个attrs.xml文件,告诉程序你自定义的item是什么类型的

<resources>

<declare-styleable name=" MyTextView" >

<attr name=" positiveColor "  format="color"  /> 

<attr name=" negetiveColor "  format=" color " />

</declare-styleable>

</resources>

3。在layout设计中引用自定义view并加入style

<com.pan.MyTextView

style="@style/ Widget. MyTextView"  

android:layout_width="wrap_content" 

android:layout_height="wrap_content" />

4。activity中读取

int positiveColor;

int negetiveColor;

public   MyTextView(Context context, AttributeSet attrs, int  defStyle)

{

             super (context, attrs, defStyle);

             TypedArray a = context.obtainStyledAttributes(   attrs, R.styleable.TestView, defStyle, 0 );

              int  n = a.getIndexCount();                          

              for  (int  i = 0 ; i < n; i++)

              {

                 int  attr = a.getIndex(i);

                 switch  (attr)

                 {

                       case  R.styleable.MyTextView_positiveColor:

                               positiveColor = a.getColor(attr, Color.RED);

                                break ;

                      case  R.styleable.MyTextView_negetiveColor:

                               negetiveColor = a.getColor(attr, Color.Green);

                                break ;

                  }        

               }     

分享到:
评论

相关推荐

    解决Android中自定义DialogFragment解决宽度和高度问题

    关于详解Android应用中DialogFragment的基本用法,大家可以参考下。 1、 概述 DialogFragment在android 3.0时被引入。是一种特殊的Fragment,用于在Activity的内容之上展示一个模态的对话框。典型的用于:展示警告框...

    Android代码-自定义字体,且不需要写代码哦~

    simply add the fonts to your app, replace your views with their Font counterparts, and specify the fonts through attributes, styles and themes. All fonts are referenced into (subdirectories of) the ...

    Android代码-有趣的添加产品自定义View

    You can customize view, through this attributes * app:ipv_middle_icon - main view icon * app:ipv_highlight_background_color - background color when view is expand * app:ipv_default_backgrou

    Android代码-可定制的循环进度指示器

    CircularProgressIndicator Simple but customizable view for displaying progress With custom colors With or without dot ...Since all attributes have default values, you can specify none of them

    Android代码-Android自定义多种风格进度控件

    add the progressview in your layout.xml and set the attributes // style Horizontal // style Circle In the Activity you can also set all of the attributes in activity horizonta

    Android代码-在Layout中显示进度

    Custom Progress Layout for Android Here is the youtube link XML Definition Attributes to use Use ProgressLayout progressLayout = (ProgressLayout) findViewById(R.id.progressLayout); //Start it ...

    Android 自定义SeekBar 实现分段显示不同背景颜色的示例代码

    在最近的开发工作中,要实现一个调色板的进度条,SeekBar要分成10段显示不同颜色,功夫不负有心人,终于实现了这个功能,下面分享给大家 示例图: 1.自定义SeekBar ...import android.util.AttributeS

    Android代码-NumberPickerView

    在Android项目的开发中会用到NumberPicker组件,但是默认风格的NumberPicker具有一些不灵活的属性,定制起来也比较麻烦,并且缺少一些过渡动效,因此在应用开发时,一般采用自定义的控件来完成选择功能。 控件截图 =...

    Android代码-支持TextView默认支持的所有标签

    public void handleTag(boolean open, String tag, Editable output, Attributes attrs) { if(tag.toLowerCase().equals("span")){ if(open){ //开标签,output是空(sax还没读到),attrs有值 for(i

    Android代码-通过贝塞尔曲线实现的药瓶和液体效果

    &gt; * 1.The project does not use any image resources, colors of bottle and water can be changed by custom attributes; &gt; * 2.LoadingView will automatically adjust the width and height; 2 How to Use: ...

    android电子签名,屏幕上手写签名

    1,android 如何让自定义dialog的宽度跟屏幕的宽度一样? 在你dialog.show();后面加上 WindowManager windowManager = getWindowManager(); Display display = windowManager.getDefaultDisplay(); WindowManager....

    自定义dialog

    customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER; // 设置点击屏幕Dialog不消失 customProgressDialog.setCanceledOnTouchOutside(false); return customProgressDialog; }...

    Android使用Dialog制作popWindow气泡提示框.rar

    这是一个Android使用Dialog制作popWindow气泡提示框的源代码例子,气泡提示是主动弹出,您可以通过此源码学习制作气泡view、气泡dialog、气泡显示内容的自定义,获取屏幕的宽和高,一些初始化代码:  //设置...

    Android代码-类似于增强的SeekBar小部件

    Developers can customize the following attributes (both via XML and programmatically): bar color bar thickness tick height number of ticks connecting line thickness connecting line col

    Android代码-polygonsview

    正在学习自定义View的同学,可以看看源码,希望对你们有帮助! 如果你觉得还不错,可以给个star,支持下,谢谢了! 原始图效果 模仿效果 gif png 流程 绘制中心线,用于计算外层多边形各点的坐标 绘制...

    Android代码-带波动效果的索引侧边栏,支持左右手模式和自定义索引

    Description of Attributes Attributes Format Default Description sidebar_text_color color Color.GRAY Text color of side bar. sidebar_text_size dimension 14sp Text size of side bar. sidebar_...

    NumberPickerView

    在Android项目的开发中会用到NumberPicker组件,但是默认风格的NumberPicker具有一些不灵活的属性,定制起来也比较麻烦,并且缺少一些过渡动效,因此在应用开发时,一般采用自定义的控件来完成选择功能。 控件截图 =...

    activlog:Activity Logger 是一款用于记录体育活动的 Android 应用程序

    数据库架构 (SQLite) 表类别列名类型类别编号整数名称文本餐桌活动列名类型活动编号整数类别编号整数名称文本启用布尔值表activity_attributes 列名类型activity_attribute_id 整数活动编号整数类型int /* 距离、持

    LrcView1Demo

    自定义view实现,通过和弦动态的控制歌词滚动实现;

Global site tag (gtag.js) - Google Analytics