如何以编程方式更改自定义首选项布局的图像视图?
How to change the custom preference layout's imageview programmatically?
我使用设置 activity 让用户 select 来自我的 android 应用中的不同资源。所有首选项都是复选框。我为每个首选项定义了一个自定义布局并将其连接到复选框。当我单击首选项时,它工作正常,但我无法理解首选项的位置。所以我想在单击首选项时以编程方式更改自定义布局的图像视图。有办法吗?
下面第二项是默认的文字,我想把排版改成第一项。一切正常,但我想在单击此项时以编程方式将加号图像更改为另一个图像(以了解它已被选中)。由于这是设置 activity,我找不到方法(没有 findviewbyid 等。)Android 将整个自定义布局表现为复选框(您可以单击该行的任意位置)
Settings.xml
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:layout="@layout/source_item"
android:key="gundem_haberturk"
android:title="@string/haberturk"
android:defaultValue="false"/>
</PreferenceScreen>
自定义Layout.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout_source_item"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_source_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="100dp"
android:maxHeight="40dp"
android:src="@drawable/logo_haberturk"/>
<TextView
android:id="@+id/tv_source_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Habertürk"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/iv_source_selector"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/checkbox_unchecked"/>
</RelativeLayout>
设置片段:
public class GundemSettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle bundle, String rootKey) {
addPreferencesFromResource(R.xml.gundem_settings);
}
}
设置Activity:
public class GundemSettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gundem_settings);
Toolbar toolbar = findViewById(R.id.settings_toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
}
设置Activityxml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hhs.haberler.Settings.GundemSettingsActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/settings_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<fragment
android:layout_marginTop="?attr/actionBarSize"
android:id="@+id/settings_fragment"
android:name="com.example.haberler.Settings.GundemSettingsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>
您可以创建一个从 CheckBoxPreference
扩展而来的自定义 Preference
,并像 CheckBoxPreference
:
一样使用它
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<your.package.name.CustomCheckBoxPreference
android:layout="@layout/custom_checkbox_preference"
android:key="custom_checkbox_pref"
android:title="CustomCheckBoxPreferenceTitle"
android:defaultValue="false"/>
</PreferenceScreen>
请注意,根据 android:layout
属性的 documentation,布局的根 ViewGroup
以及 TextView
必须使用特定的资源 ID标题和摘要。这将确保自定义 Preference
的行为与任何股票 Preference
.
一样
通过覆盖 onBindViewHolder(PreferenceViewHolder)
,您可以 "find" ImageView
并将其分配给相应的字段 ivSourceSelector。通过覆盖 setChecked()
,您可以在 Preference
的选中状态发生变化时交换可绘制对象。
话虽如此,这是 CustomCheckBoxPreference
的代码:
public class CustomCheckBoxPreference extends CheckBoxPreference {
private ImageView ivSourceSelector;
public CustomCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public CustomCheckBoxPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomCheckBoxPreference(Context context) {
super(context);
}
@Override
public void setChecked(boolean checked) {
super.setChecked(checked);
if(ivSourceSelector != null) {
ivSourceSelector.setImageResource(getResourceId(checked));
}
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
ivSourceSelector = holder.itemView.findViewById(R.id.iv_source_selector);
if(ivSourceSelector != null){
ivSourceSelector.setImageResource(getResourceId(isChecked()));
}
}
private int getResourceId(boolean checked) {
return checked ? R.drawable.custom_checkbox_checked : R.drawable.custom_checkbox_unchecked;
}
}
我使用设置 activity 让用户 select 来自我的 android 应用中的不同资源。所有首选项都是复选框。我为每个首选项定义了一个自定义布局并将其连接到复选框。当我单击首选项时,它工作正常,但我无法理解首选项的位置。所以我想在单击首选项时以编程方式更改自定义布局的图像视图。有办法吗?
下面第二项是默认的文字,我想把排版改成第一项。一切正常,但我想在单击此项时以编程方式将加号图像更改为另一个图像(以了解它已被选中)。由于这是设置 activity,我找不到方法(没有 findviewbyid 等。)Android 将整个自定义布局表现为复选框(您可以单击该行的任意位置)
Settings.xml
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:layout="@layout/source_item"
android:key="gundem_haberturk"
android:title="@string/haberturk"
android:defaultValue="false"/>
</PreferenceScreen>
自定义Layout.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout_source_item"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_source_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="100dp"
android:maxHeight="40dp"
android:src="@drawable/logo_haberturk"/>
<TextView
android:id="@+id/tv_source_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Habertürk"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/iv_source_selector"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/checkbox_unchecked"/>
</RelativeLayout>
设置片段:
public class GundemSettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle bundle, String rootKey) {
addPreferencesFromResource(R.xml.gundem_settings);
}
}
设置Activity:
public class GundemSettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gundem_settings);
Toolbar toolbar = findViewById(R.id.settings_toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
}
设置Activityxml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hhs.haberler.Settings.GundemSettingsActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/settings_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<fragment
android:layout_marginTop="?attr/actionBarSize"
android:id="@+id/settings_fragment"
android:name="com.example.haberler.Settings.GundemSettingsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>
您可以创建一个从 CheckBoxPreference
扩展而来的自定义 Preference
,并像 CheckBoxPreference
:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<your.package.name.CustomCheckBoxPreference
android:layout="@layout/custom_checkbox_preference"
android:key="custom_checkbox_pref"
android:title="CustomCheckBoxPreferenceTitle"
android:defaultValue="false"/>
</PreferenceScreen>
请注意,根据 android:layout
属性的 documentation,布局的根 ViewGroup
以及 TextView
必须使用特定的资源 ID标题和摘要。这将确保自定义 Preference
的行为与任何股票 Preference
.
通过覆盖 onBindViewHolder(PreferenceViewHolder)
,您可以 "find" ImageView
并将其分配给相应的字段 ivSourceSelector。通过覆盖 setChecked()
,您可以在 Preference
的选中状态发生变化时交换可绘制对象。
话虽如此,这是 CustomCheckBoxPreference
的代码:
public class CustomCheckBoxPreference extends CheckBoxPreference {
private ImageView ivSourceSelector;
public CustomCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public CustomCheckBoxPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomCheckBoxPreference(Context context) {
super(context);
}
@Override
public void setChecked(boolean checked) {
super.setChecked(checked);
if(ivSourceSelector != null) {
ivSourceSelector.setImageResource(getResourceId(checked));
}
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
ivSourceSelector = holder.itemView.findViewById(R.id.iv_source_selector);
if(ivSourceSelector != null){
ivSourceSelector.setImageResource(getResourceId(isChecked()));
}
}
private int getResourceId(boolean checked) {
return checked ? R.drawable.custom_checkbox_checked : R.drawable.custom_checkbox_unchecked;
}
}