【问题标题】:Android Custom View with custom Attributes具有自定义属性的 Android 自定义视图
【发布时间】:2013-09-01 13:36:41
【问题描述】:

我已经制作了我的自定义视图,我想为其设置一些自定义属性。我想将另一个视图的 id 作为属性传递。

自定义视图属性:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="IdNumber">
        <attr name="firstName" format="integer"/>
        <attr name="lastName" format="integer"/>
        <attr name="address" format="integer"/>
        <attr name="birthDate" format="integer"/>
    </declare-styleable>
</resources>

我使用自定义视图的布局:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/display_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:layout_centerHorizontal="true"
        android:tag="name"
        android:ems="10" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/id_number"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/display_name"/>

    <ge.altasoft.custom_views.IdNumber
        android:id="@+id/custom_id_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/id_number"
        android:paddingLeft="35dip"
        custom:firstName="@id/display_name"/>


</RelativeLayout>

自定义视图类的构造函数,我要获取属性值的地方:

 public IdNumber (Context context, AttributeSet attrs) {
        super(context, attrs);
        initViews();

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IdNumber);
        final int N = a.getIndexCount();
        for(int i = 0; i < N; i++){
            int attr = a.getIndex(i);
            switch(attr){
                case R.styleable.IdNumber_firstName:
                    int firstNameViewID = a.getInteger(attr, -1);
                    break;
            }
        }
        a.recycle();
    }

问题是int firstNameViewID = a.getInteger(attr, -1); 只是 0,而不是 View 的 id。

custom:firstName="@id/display_name"

感谢您提前提供的帮助。

【问题讨论】:

    标签: android android-layout view custom-attributes


    【解决方案1】:

    改变你的定义&lt;attr name="firstName" format="reference"/&gt; 在你的代码中使用int firstNameViewID = a.getResourceId(attr, -1);

    希望对您有所帮助!

    【讨论】:

      【解决方案2】:

      使用 custom:firstName="@+id/display_name" 并在您的样式中将其更改为 &lt;attr name="firstName" format="refernce"/&gt; 而不是整数

      【讨论】:

      • 在获取 id 时使用 a.getReference()
      • 感谢您的回答,但 a.getResourceId(attr, -1) 有所帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多