如何获取引用属性的值

How to obtain the value for a reference attribute

我想实现一个自动更新 TextView 的 SeekBar,用于实际值、最大值和最小值。我从 SeekBar 派生并定义了 3 个命名属性,其中 formatreference

在构造函数中,我通过调用 obtainStyledAttributes() 得到一个 TypedArrayTypedArray 包含各种属性类型的大量 getter。我缺少的是某种 Object getReference()int getReferenceId().

如何获取引用属性的值?

编辑:

我有一个 class MinMaxSlider 的属性定义,看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MinMaxSlider">
        <attr name="min" format="integer" />
        <attr name="valueView" format="reference" />
    </declare-styleable>
</resources>

从布局定义中截取的内容如下所示:

    <LinearLayout
        style="@style/ParameterLabelLayout"
        android:orientation="horizontal">
        <TextView
            style="@style/ParameterSliderLabel"
            android:text="min. Interval" />
        <TextView
            android:id="@+id/min_connection_interval_slider_value"
            style="@style/ParameterSliderValue"/>
    </LinearLayout>

    <com.roche.rcpclient.MinMaxSlider
        style="@style/ParameterSlider"
        android:id="@+id/min_connection_interval_slider"
        android:max="3200"
        custom:valueView="@id/min_connection_interval_slider_value"
        custom:min="1"/>

此处,MinMaxSlider 应引用上面的 TextView 之一以显示其当前值。

MinMaxSlider 的构造函数中,我可以查找最小属性值。

如果我尝试将 valueView 属性查找为整数,我总是得到默认值 (0),而不是我期望的 R.id.min_connection_interval_slider

编辑:查找 reference 的正确函数似乎是 getResourceId。得到的整型id可以在后面构造整体对象层级的时候使用findViewById

在我的例子中,我注册了一个 OnSeekBarChangeListener() 并在触发回调时在回调中查找视图。

您无法通过构造函数中的 findViewById 接收引用。因为它还没有附加到布局。 参考:https://developer.android.com/training/custom-views/create-view.html#applyattr

When a view is created from an XML layout, all of the attributes in the XML tag are read from the resource bundle and passed into the view's constructor as an AttributeSet. Although it's possible to read values from the AttributeSet directly, doing so has some disadvantages:

Resource references within attribute values are not resolved Styles are not applied Instead, pass the AttributeSet to obtainStyledAttributes(). This method passes back a TypedArray array of values that have already been dereferenced and styled.

您可以通过其他方式接收。 例如:

attrs.xml

 <declare-styleable name="CustomTextView">
    <attr name="viewPart" format="reference"></attr>
 </declare-styleable>

yourLayout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:gravity="center_vertical"
    android:orientation="horizontal">

  <tr.com.ui.utils.CustomTextView
                        android:id="@+id/chat_msg"                           
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right|bottom"
                        android:focusableInTouchMode="false"
                        android:gravity="left"                          
                        android:text="test"
                        app:viewPart="@id/date_view" />



                    <TextView
                        android:id="@+id/date_view"                         
                        android:layout_alignParentBottom="true"
                        android:layout_alignParentRight="true"                          
                        android:gravity="right" />

</LinearLayout>

CustomTextView.java

   public class CustomTextView extends TextView {
    private View viewPart;
    private int viewPartRef;

    public CustomTextView(Context context) {
        super(context);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView, 0, 0);

        try {
            viewPartRef = a.getResourceId(R.styleable.CustomTextView_viewPart, -1);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        viewPart = ((View) this.getParent()).findViewById(viewPartRef);
    }    

    public View getViewPart() {
        return viewPart;
    }

    public void setViewPart(View viewPart) {
        this.viewPart = viewPart;
    }}

您可以决定您的场景并修改此代码。