如何在自定义首选项 class 上设置自定义属性的视图属性?
How to set view attribute from custom attr on a custom Preference class?
我创建了一个 class CustomPreference
,它继承自 androidx Preference class,用于设置由我自己的组件组成的自定义布局。我在该自定义首选项 class 上定义了一个可样式化的属性。但现在我正在尝试了解如何将自定义样式属性的值传递到我的基础 SpecialCustomComponent
.
上的属性
CustomPreference.java
public class CustomPreference extends Preference {
private Context context;
private AttributeSet attrs;
public CustomPreference(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
this.context = context;
this.attrs = attrs;
setLayoutResource(R.layout.preference_widget_custom);
}
// other required constructor overloads omitted
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
// calling this method here results in an error because
// a.getResourceId(R.styleable.PreferenceWidgetCustom_preferenceTitle, 0)
// returns no resource Id at this point
setPreferenceTitle(holder);
}
@Override
protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
super.onAttachedToHierarchy(preferenceManager);
this.preferenceManager = preferenceManager;
// alternatively, calling this method here would result in a
// different error, since I don't have access to the viewHolder
// here. To make this work, I tried preserving "viewHolder" in a
// field inside onBindViewHolder (which I don't think I'm
// supposed to do anyways), but the viewHolder reference was
// null, so it didn't work.
setPreferenceTitle();
}
// This is my incorrect attempt at setting "myText" to the value of "preferenceTitle"
private void setPreferenceTitle(PreferenceViewHolder holder) {
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PreferenceWidgetSwitchCustom, 0, 0);
try {
int preferenceTitleId = a
.getResourceId(R.styleable.PreferenceWidgetCustom_preferenceTitle, 0);
((SpecialCustomComponent) holder.itemView).setText(a.getResources().getString(preferenceTitleId));
}
finally {
a.recycle();
}
}
}
我在 attrs.xml 中为我的 CustomPreference 声明了一个自定义属性,我希望用它来设置布局中我的自定义组件的底层“myText”属性。
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="PreferenceWidgetCustom">
<attr name="preferenceTitle" format="string" />
</declare-styleable>
</resources>
每个 CustomPreference 的布局在 preference_widget_custom.xml 中定义。请注意属性“myText”,我想将其设置为上面定义的“preferenceTitle”的值。
preference_widget_custom.xml
<?xml version="1.0" encoding="utf-8"?>
<SpecialCustomComponent
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:myText="this is the field I would like to be set by 'preferenceTitle'" />
我在这样的 PreferenceScreen 布局中使用这些首选项:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:title="Settings">
<CustomPreference
android:key="pref_one"
app:preferenceTitle="Preference #1"/>
<CustomPreference
android:key="pref_two"
app:preferenceTitle="Preference #2"/>
</PreferenceScreen>
将 string
资源作为 styleable
提供通常是错误的,这就是为什么您使简单的本地化工作过于复杂的原因。可以使用此模式访问任何资源:[<package_name>.]R.<resource_type>.<resource_name>
.
所以这将是 res/values/strings.xml
:
<resources>
<string name="title_preference_01">Title 01</string>
<string name="summary_preference_01">Summary 01</string>
<string name="title_preference_02">Title 02</string>
<string name="summary_preference_02">Summary 02</string>
<resources>
然后可以访问吞吐量的应用:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:title="Settings">
<CustomPreference
android:title="@string/title_preference_01"
android:summary="@string/summary_preference_01"
android:key="pref_one" />
<CustomPreference
android:title="@string/title_preference_02"
android:summary="@string/summary_preference_02"
android:key="pref_two" />
</PreferenceScreen>
Localize your app 详细解释了这一切。
此外,即使它是实际的可设置样式的值而不是通常用于本地化的字符串,您仍然需要将 PreferenceWidgetCustom
样式应用于 SpecialCustomComponent
才能访问它的值。
我解决了我的问题。正如 Martin 指出的那样,styleable
无论如何都不是真正用于字符串的。事实证明,使用内置 android:title
属性而不是自定义样式让事情变得非常简单。我所要做的就是调用 this.getTitle()
从我的 CustomPreference
class.
中访问它的值
public class CustomPreference extends Preference {
public CustomPreference(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
setLayoutResource(R.layout.preference_widget_custom);
}
// other required constructor overloads omitted
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
setPreferenceTitle();
}
private void setPreferenceTitle() {
((SpecialCustomComponent) holder.itemView).setText(this.getTitle());
}
}
我创建了一个 class CustomPreference
,它继承自 androidx Preference class,用于设置由我自己的组件组成的自定义布局。我在该自定义首选项 class 上定义了一个可样式化的属性。但现在我正在尝试了解如何将自定义样式属性的值传递到我的基础 SpecialCustomComponent
.
CustomPreference.java
public class CustomPreference extends Preference {
private Context context;
private AttributeSet attrs;
public CustomPreference(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
this.context = context;
this.attrs = attrs;
setLayoutResource(R.layout.preference_widget_custom);
}
// other required constructor overloads omitted
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
// calling this method here results in an error because
// a.getResourceId(R.styleable.PreferenceWidgetCustom_preferenceTitle, 0)
// returns no resource Id at this point
setPreferenceTitle(holder);
}
@Override
protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
super.onAttachedToHierarchy(preferenceManager);
this.preferenceManager = preferenceManager;
// alternatively, calling this method here would result in a
// different error, since I don't have access to the viewHolder
// here. To make this work, I tried preserving "viewHolder" in a
// field inside onBindViewHolder (which I don't think I'm
// supposed to do anyways), but the viewHolder reference was
// null, so it didn't work.
setPreferenceTitle();
}
// This is my incorrect attempt at setting "myText" to the value of "preferenceTitle"
private void setPreferenceTitle(PreferenceViewHolder holder) {
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PreferenceWidgetSwitchCustom, 0, 0);
try {
int preferenceTitleId = a
.getResourceId(R.styleable.PreferenceWidgetCustom_preferenceTitle, 0);
((SpecialCustomComponent) holder.itemView).setText(a.getResources().getString(preferenceTitleId));
}
finally {
a.recycle();
}
}
}
我在 attrs.xml 中为我的 CustomPreference 声明了一个自定义属性,我希望用它来设置布局中我的自定义组件的底层“myText”属性。
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="PreferenceWidgetCustom">
<attr name="preferenceTitle" format="string" />
</declare-styleable>
</resources>
每个 CustomPreference 的布局在 preference_widget_custom.xml 中定义。请注意属性“myText”,我想将其设置为上面定义的“preferenceTitle”的值。
preference_widget_custom.xml
<?xml version="1.0" encoding="utf-8"?>
<SpecialCustomComponent
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:myText="this is the field I would like to be set by 'preferenceTitle'" />
我在这样的 PreferenceScreen 布局中使用这些首选项:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:title="Settings">
<CustomPreference
android:key="pref_one"
app:preferenceTitle="Preference #1"/>
<CustomPreference
android:key="pref_two"
app:preferenceTitle="Preference #2"/>
</PreferenceScreen>
将 string
资源作为 styleable
提供通常是错误的,这就是为什么您使简单的本地化工作过于复杂的原因。可以使用此模式访问任何资源:[<package_name>.]R.<resource_type>.<resource_name>
.
所以这将是 res/values/strings.xml
:
<resources>
<string name="title_preference_01">Title 01</string>
<string name="summary_preference_01">Summary 01</string>
<string name="title_preference_02">Title 02</string>
<string name="summary_preference_02">Summary 02</string>
<resources>
然后可以访问吞吐量的应用:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:title="Settings">
<CustomPreference
android:title="@string/title_preference_01"
android:summary="@string/summary_preference_01"
android:key="pref_one" />
<CustomPreference
android:title="@string/title_preference_02"
android:summary="@string/summary_preference_02"
android:key="pref_two" />
</PreferenceScreen>
Localize your app 详细解释了这一切。
此外,即使它是实际的可设置样式的值而不是通常用于本地化的字符串,您仍然需要将 PreferenceWidgetCustom
样式应用于 SpecialCustomComponent
才能访问它的值。
我解决了我的问题。正如 Martin 指出的那样,styleable
无论如何都不是真正用于字符串的。事实证明,使用内置 android:title
属性而不是自定义样式让事情变得非常简单。我所要做的就是调用 this.getTitle()
从我的 CustomPreference
class.
public class CustomPreference extends Preference {
public CustomPreference(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
setLayoutResource(R.layout.preference_widget_custom);
}
// other required constructor overloads omitted
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
setPreferenceTitle();
}
private void setPreferenceTitle() {
((SpecialCustomComponent) holder.itemView).setText(this.getTitle());
}
}