gpt4 book ai didi

android - 如何获取引用属性的值

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:25:18 25 4
gpt4 key购买 nike

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

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

如何获取引用属性的值?

编辑:

我有一个类 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>

布局定义的 fragment 如下所示:

    <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() 并在触发回调时在回调中查找 View 。

最佳答案

您无法通过构造函数中的 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.

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

属性.xml

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

你的布局.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>

自定义 TextView .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;
}}

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

关于android - 如何获取引用属性的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34860400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com