如何在 PreferenceActivity 中的 Preference Item 右侧标记图标?

发布于 2024-11-05 07:46:37 字数 1759 浏览 3 评论 0原文

请点击查看图片 http://i1094.photobucket.com/albums/i455/tdounnyy/device。 png

在此 PreferenceActivity 的底部,“选择铃声”的右侧有一个图标/按钮。

而“振动”则不然。

该振动偏好是一种客户偏好的扩展偏好。

我想将相同的图标放在“振动”行中。 一种方法是扩展 ListPreference 或 RingtonePreference 来欺骗操作系统。

我不喜欢这样,想知道是否有更好的解决方案。

preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<!-- Layout used by PreferenceScreen. This is inflated inside 
        android.R.layout.preference. -->
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="4dip"
    android:layout_gravity="center_vertical"
    android:background="@drawable/btn_circle"
    android:src="@drawable/ic_btn_round_more" />

我添加了前缀“android”:

android:background="@android:drawable/btn_circle"
android:src="@android:drawable/ic_btn_round_more"

现在,Eclipse 说:错误:资源不是公开的。 我用谷歌搜索但没有运气。 有想过如何突破吗?

Please click to see the image
http://i1094.photobucket.com/albums/i455/tdounnyy/device.png

At the bottom of this PreferenceActivity, the "Select ringtone" has an icon/button on the right side.

And "Vibrate" does not.

This Vibrate Preference is one custome Preference extends Preference.

I want to put the same Icon in the Vibrate row.
One way is to extends ListPreference or RingtonePreference to cheat OS.

I don't like this, wondering if there is a better solution.

preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<!-- Layout used by PreferenceScreen. This is inflated inside 
        android.R.layout.preference. -->
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="4dip"
    android:layout_gravity="center_vertical"
    android:background="@drawable/btn_circle"
    android:src="@drawable/ic_btn_round_more" />

I added prefix "android" as:

android:background="@android:drawable/btn_circle"
android:src="@android:drawable/ic_btn_round_more"

Now, Eclipse says: Error: Resource is not public.
I googled but no luck.
Any thought how to break through?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪你到最终 2024-11-12 07:46:37

创建首选项 xml 文件时,您可以为首选项指定自定义布局,并且可以在其中放置图标。最简单的方法是查看 Android SDK 文件夹:platforms/data//res/layout/(我相信它)包含用于不同首选项类型的实际布局文件:您可以从其中复制其中之一并将其用作您自己的自定义偏好的基础。

When you create your preference xml file, you can specify a custom layout for a preference, and you can put an icon in it. The easiest way to do that is to look in the Android SDK folder: platforms/data//res/layout/ (I believe this it) contains the actual layout files used for the different preference types: you can probably copy one of them from there and use that as the basis of your own custom preference.

月亮邮递员 2024-11-12 07:46:37

在您的preference.xml中

android:widgetLayout="@layout/layout_contain_imageview"

或在

 setWidgetLayoutResource(R.layout.layout_contain_imageview);

layout_contain_imageview.xml的.java示例中

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ivIcon"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:src="@drawable/round_app_settings_alt_white_36"
    app:tint="@color/icon_tint"/>

当您想在运行时更改图标时,

@Override
    public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
        ImageView ivIcon = (ImageView) holder.findViewById(R.id.ivIcon);
        ivIcon.setImageResource(R.drawable.round_get_app_white_36);
    }

inside your preference.xml

android:widgetLayout="@layout/layout_contain_imageview"

or in .java

 setWidgetLayoutResource(R.layout.layout_contain_imageview);

example for layout_contain_imageview.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ivIcon"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:src="@drawable/round_app_settings_alt_white_36"
    app:tint="@color/icon_tint"/>

when you want to change icon at runtime

@Override
    public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
        ImageView ivIcon = (ImageView) holder.findViewById(R.id.ivIcon);
        ivIcon.setImageResource(R.drawable.round_get_app_white_36);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文