对于Android的TextView的自定义属性 [英] Android custom attribute for TextView

查看:216
本文介绍了对于Android的TextView的自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  如何读取Android的自定义属性

最近,我读到自定义属性。我想自定义属性添加到TextView的。

Recently I read about custom attributes. I want to add a custom attribute to TextView.

到目前为止,我有:

ATTR文件:

<resources>
    <attr name="binding" format="string" />
    <declare-styleable name="TextView">
        <attr name="binding" />
    </declare-styleable>

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
     xmlns:custom="http://schemas.android.com/apk/res/de.innosoft.android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

        <TextView
            custom:binding="test"/>

由于一个TextView

Given a TextView

TextView tv = ...

我怎么会那么得到属性(北京时间测试)的价值?我读到obtainStyledAttributes,但不知道究竟如何在这里使用它。

How would I then get the value of that attribute (which ist "test")? I read about obtainStyledAttributes but do not know exactly how to use it here.

推荐答案

据我知道你有2个选择:

As I know you have 2 options:


  • 创建一个扩展的TextView 并具有构造函数的AttributeSet 您自己的看法。然后,你可以在这个构造器自定义属性。检查本教程:创建一个视图类

  • 实施自己的 LayoutInfalter.Factory 在那里你处理自定义属性。

  • Create your own view that extends TextView and has constructor that takes AttributeSet. Then you can get custom property in this constructor. Check this tutorial: Creating a View Class.
  • Implement own LayoutInfalter.Factory where you handle custom attributes.

最好检查一下这个问题:如何读取Android的自定义属性这几乎是一样的。

Better check this question: How to read custom attributes in Android it's almost the same.

这篇关于对于Android的TextView的自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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