问题描述
是有可能访问编程被设定为一个preference布局?
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
在preference活动:
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);
}
}
资源XML:
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>
为preference自定义布局:
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>
我要的是能够从活动从那里进入iconka的ImageView和改变形象。我使用的API 8(Android 2.2的)
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)
目前的V为空,我没有任何想法,这是为什么。
Currently the "v" is null and I don't have any idea why is that.
一个暗示将大大AP preciated!
A hint will be much appreciated!
更新 - 解决方案:其实,我需要一个自定义的preference,我可以修改我的需要。这一个是一个实用指南,如何在项目中创建自己的自定义preference:的Android和放大器;阿米尔 - 安卓preferences 的看到零件时,笔者创建自定义preference类。
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.
推荐答案
尝试采取看看上的ImageView下面的讨论。可以帮助你与你的问题。
Try taking a look at the following discussions on imageview. Might help you with your issue.
Android: findViewById一个ImageView的(自定义适配器)的
Android :越来越NullPointerException异常的ImageView的IMAG =(ImageView的)findViewById(R.id.image)
这篇关于设置preference布局,改变属性,它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!