【问题标题】:How to change the custom preference layout's imageview programmatically?如何以编程方式更改自定义首选项布局的图像视图?
【发布时间】:2019-04-11 05:42:51
【问题描述】:

我使用设置活动让用户从我的 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>

【问题讨论】:

    标签: android android-layout settings android-preferences


    【解决方案1】:

    您可以创建一个从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>
    

    请注意,根据documentationandroid:layout 属性,必须将特定资源ID 用于布局的根ViewGroup 以及TextViews 用于标题和摘要。这将确保自定义 Preference 的行为与任何股票 Preference 一样。

    通过覆盖onBindViewHolder(PreferenceViewHolder),您可以“找到”ImageView 并将其分配给相应的字段ivSourceSelector。通过覆盖setChecked(),您可以在Preference 的选中状态发生变化时交换drawable。

    话虽如此,这是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;
        }
    }
    

    【讨论】:

    • 非常感谢您的回答。现在,我至少有 20 个这样的复选框(来源),我可以为所有这些复选框使用相同的自定义布局和自定义类,还是应该为首选项中的所有列表项创建布局和新类。其次,代码正在运行,但它在设置 xml 中给出警告,此处不允许“com.example.hhs.haberler.Settings.CustomCheckBoxPreference”
    • @Hüseyin - 您可以重用布局,因为您在仅包含这些膨胀布局之一的 ViewGroup 中使用“findViewById()”。您可以重用该类(在 PreferenceScreen 中),因为它将根据需要经常实例化。关于警告:对不起,但我不知道为什么。它没有出现在我的示例应用程序中。
    • 很抱歉给您带来不便。我正在尝试对所有其他人使用相同的布局。但问题是我需要从 onBindViewHolder (或从其他方法)中提取每个首选项的键值,根据此信息更改自定义布局中的 textview 和 imageview 。我查看了谷歌文档,但找不到任何东西。
    • @Hüseyin - 你试过Preference.getKey() 吗?
    • 我试过了,但 .getKey() 方法没有出现在自定义首选项类中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多