java - TextView 的 Android 自定义属性

标签 java android attributes

<分区>

Possible Duplicate:
How to read custom attributes in Android

最近我读到有关自定义属性的内容。我想向 TextView 添加自定义属性。

到目前为止我有:

属性文件:

<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

TextView tv = ...

然后我将如何获得该属性的值(即“测试”)?我阅读了有关 obtainStyledAttributes 的信息,但不知道如何在此处使用它。

最佳答案

确切地说,你可以像那样扩展你的 TextView

 public class CustomTV extends TextView {

 public final String YOURATTRS;

 public CustomTV(Context context, AttributeSet attrs) {
    super(context, attrs);
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomTV);
    YOURATTRS = ta.getString(R.styleable.CustomTV_binding);
    ta.recycle();

 // use your attrs or not
 }

和 attrs.xml :

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

关于java - TextView 的 Android 自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14360645/

相关文章:

java - Clojure GUI 计算器。 "-"总是返回 0 而 "/"总是返回 1

java - 将 post 数据从 JSP 发送到 Java Servlet

android - 预防多个 Android 设备上的 Firebase 同一帐户

asp.net - 在 ASP MVC5 中对同一操作定义属性路由后如何重定向到操作

java - 使用 Jackson 将 ebean 映射到 json 时如何避免无限循环引用?

java - 自动适应添加到 SashForm 运行时的 Composite

java - Intellij Idea 预览不工作(渲染问题)

android - Android 4.4 Kitkat 中的内部文件选择器

javascript - jQuery .data 方法 - 根据存储数据动态添加或删除元素

jquery - 使用jquery获取两个字符串之间的字符串