7

プリファレンスに設定されているレイアウトにプログラムでアクセスすることは可能ですか?

これが私が持っているものです。非常に単純なプロジェクトです-概念実証

Preference アクティビティ:

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>

私が欲しいのは、Activity から「iconka」ImageView にアクセスし、そこから画像を変更できるようにすることです。API 8 (Android 2.2) を使用しています

現在、「v」は null であり、その理由がわかりません。

ヒントをいただければ幸いです。

更新 - 解決策: 実際、必要に応じて変更できるカスタム設定が必要でした。これは、プロジェクトで独自のカスタム設定を作成する方法の実用的なガイドです: Android & Amir - Android 設定 作成者がカスタム設定クラスを作成するときの部分を参照してください。

4

3 に答える 3

9

20分間髪を引っ張った後、この問題に対するエレガントな解決策を見つけました. まず、設定を拡張してから、getView(View convertView, ViewGroup parent) メソッドをオーバーライドします。私の場合はこれでした:アプリアイコンと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"/>
于 2013-08-22T15:32:02.603 に答える
1

imageviewに関する以下の説明をご覧ください。あなたの問題であなたを助けるかもしれません。

Android:ImageViewのfindViewById(カスタムアダプター)

Android:ImageViewのNullPointerExceptionを取得するimag =(ImageView)findViewById(R.id.image)

于 2011-11-15T23:40:08.550 に答える
1

getLayoutResourceを試しViewて設定を取得してから、ImageView

于 2011-11-15T22:34:32.810 に答える