设置首选项布局并更改其中的属性

发布于 2024-12-15 22:31:43 字数 3426 浏览 2 评论 0原文

是否可以以编程方式访问设置为首选项的布局?

这是我所拥有的,一个非常简单的项目 - 概念验证

首选项活动:

package com.example;

import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.view.View;

public class PreferenceExampleActivity extends PreferenceActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

        ImageView v = (ImageView) findViewById(R.id.iconka);

    }
}

资源 XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:key="settings">
    <PreferenceCategory 
        android:title="Category Setting Name" 
        android:order="1" 
        android:key="Main">
        <Preference 
            android:order="1" 
            android:title="Setting" 
            android:summary="Setting1" 
            android:layout="@layout/profile_preference_row"
            android:key="profile" />
    </PreferenceCategory>
</PreferenceScreen>

首选项的自定义布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/widget_frame"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingRight="?android:attr/scrollbarSize">



    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignLeft="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:maxLines="4" />

    </RelativeLayout>
    <ImageView
        android:id="@+id/iconka"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />
</LinearLayout>

我想要的是能够从活动访问“iconka”ImageView 并更改图像从那里。我正在使用 API 8 (Android 2.2)

目前“v”为空,我不知道为什么会这样。

提示将不胜感激!

更新 - 解决方案: 实际上,我需要一个自定义首选项,我可以根据自己的需要进行修改。这是一份实用指南,介绍如何在项目中创建您自己的自定义首选项: Android 和 Android阿米尔 - Android 偏好设置 请参阅作者创建自定义首选项类时的部分。

is it possible to access programmatically a layout which is set to a Preference?

Here is what I have, a very simple project - proof of concept

The Preference Activity:

package com.example;

import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.view.View;

public class PreferenceExampleActivity extends PreferenceActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

        ImageView v = (ImageView) findViewById(R.id.iconka);

    }
}

The resource XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:key="settings">
    <PreferenceCategory 
        android:title="Category Setting Name" 
        android:order="1" 
        android:key="Main">
        <Preference 
            android:order="1" 
            android:title="Setting" 
            android:summary="Setting1" 
            android:layout="@layout/profile_preference_row"
            android:key="profile" />
    </PreferenceCategory>
</PreferenceScreen>

The custom layout for the Preference:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/widget_frame"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingRight="?android:attr/scrollbarSize">



    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignLeft="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:maxLines="4" />

    </RelativeLayout>
    <ImageView
        android:id="@+id/iconka"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />
</LinearLayout>

What I want is to be able to access the "iconka" ImageView from the Activity and change the image from there. I am using API 8 (Android 2.2)

Currently the "v" is null and I don't have any idea why is that.

A hint will be much appreciated!

Update - The Solution:
Actually, I needed a custom preference which I can modify for my needs. This one is a practical guide how to create your own Custom preference in your project:
Android & Amir - Android Preferences
See the part when the author creates a custom preference class.

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

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

发布评论

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

评论(3

不回头走下去 2024-12-22 22:31:43

经过 20 分钟的拉扯头发后,我找到了解决这个问题的优雅方法。首先,扩展首选项,然后重写 getView(View ConvertView, ViewGroupparent) 方法。我的情况是这样的:我有一个带有应用程序图标和 2 个文本视图(应用程序名称和版本)的首选项布局。我想以编程方式更改应用程序版本。我该怎么做?看看下面:

public class AboutUsPreference extends Preference {

    public AboutUsPreference(Context context) {
        super(context);
    }

    public AboutUsPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AboutUsPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public View getView(View convertView, ViewGroup parent) {
        View v = super.getView(convertView, parent);
        ((TextView)v.findViewById(R.id.textView2)).setText(getAppVersion());        
        return v;
    }

    private String getAppVersion(){
        PackageInfo pInfo = null;
        try {
            pInfo = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), 0);
        } catch (NameNotFoundException e) {
            Log.e(getClass().getName(), e.getMessage(), e);
            return "";
        }

        String version = pInfo.versionName;
        return getContext().getString(R.string.version, version);
    }


}

解决方案是这样的:View v = super.getView(convertView,parent); 当重写 getView 方法时。 super.getview 调用将返回您的布局视图。

我的 xml 首选项如下所示:

<com.audioRec.android.settings.aboutUs.AboutUsPreference
        android:layout="@layout/about_preference_layout"/>

After 20 min of hair pulling I found an elegant solution to this problem. First, extend the preference, then override the getView(View convertView, ViewGroup parent) method. My case was this: I had a preference layout with app icon and 2 text views(app name and version). I want to change app version programatically. How do I do this? just take a look below:

public class AboutUsPreference extends Preference {

    public AboutUsPreference(Context context) {
        super(context);
    }

    public AboutUsPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AboutUsPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public View getView(View convertView, ViewGroup parent) {
        View v = super.getView(convertView, parent);
        ((TextView)v.findViewById(R.id.textView2)).setText(getAppVersion());        
        return v;
    }

    private String getAppVersion(){
        PackageInfo pInfo = null;
        try {
            pInfo = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), 0);
        } catch (NameNotFoundException e) {
            Log.e(getClass().getName(), e.getMessage(), e);
            return "";
        }

        String version = pInfo.versionName;
        return getContext().getString(R.string.version, version);
    }


}

The solution is this: View v = super.getView(convertView, parent); when overriding getView method. super.getview call will return your layout view.

And my xml preference looks like:

<com.audioRec.android.settings.aboutUs.AboutUsPreference
        android:layout="@layout/about_preference_layout"/>
空城缀染半城烟沙 2024-12-22 22:31:43

尝试 getLayoutResource 获取 View首选项的 ,然后获取您的 ImageView

try getLayoutResource to get the View of the preference and then get your ImageView

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文