首页 > 解决方案 > 如何以编程方式更改自定义首选项布局的图像视图?

问题描述

我使用设置活动让用户从我的 android 应用程序中的不同资源中进行选择。所有首选项都是复选框。我为每个首选项定义了一个自定义布局并将其连接到复选框。当我单击首选项时,它工作正常,但我不明白首选项在哪个位置。所以我想在单击首选项时以编程方式更改自定义布局的图像视图。有没有办法做到这一点?

下面,第二项是默认文本,我想将布局更改为第一项。一切正常,但是当我单击此项目时,我想以编程方式将加号图像更改为另一个图像(以了解它已被选中)。由于这是设置活动,我找不到方法(没有 findviewbyid 等。)Android 将整个自定义布局视为一个复选框(您可以单击该行的任意位置)

设置快照

设置.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>

自定义布局.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);
    }
}

设置活动:

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);
        }
    }   
}

设置活动 xml

<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>

标签: androidandroid-layoutsettingsandroid-preferences

解决方案


您可以创建一个自定义Preference扩展CheckBoxPreference并使用它,就像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必须使用特定的资源 id 作为布局的根ViewGroup,以及使用TextViews 作为标题和摘要。这将确保定制的Preference行为就像任何股票一样Preference

通过覆盖onBindViewHolder(PreferenceViewHolder),您可以“找到”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;
    }
}

推荐阅读