Android:如何设置PreferenceActivity Preference的自定义布局的ImageView

时间:2013-12-09 15:02:36

标签: android android-layout

我有一个偏好,我将其布局 - 在XML文件中 - 设置为由我创建的自定义布局

   <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:key="help_preference_screen">
   <Preference android:key="help_preference" android:title="" android:layout="@layout/help_image_text" />
   </PreferenceScreen>

自定义布局包含ImageView&amp; TextView的。

<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/help_frame_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:id="@+id/help_linear_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/help_image_view"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/daj_help_1" />

    <TextView
        android:id="@+id/help_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="8dp"
        android:text=""
        android:textSize="16sp"
        android:textColor="#ffffff"/>

</LinearLayout> </FrameLayout>

在运行时我想访问/找到ImageView(help_image_view)来设置它的图像,我该怎么做?

public class helpactivity extends PreferenceActivity implements OnSharedPreferenceChangeListener
 {
  public void onCreate(Bundle savedInstanceState) 
   {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.userpreferences);
   }
 }

1 个答案:

答案 0 :(得分:1)

如果首选项有唯一的布局,并且子视图ID是唯一的(即每个Preference对象具有不同的ImageView ID布局),则可以这样做。然后您可以通过id访问ImageView。即:

((ImageView) findViewById(R.id.help_image_view)).setImageResource(someResId);

首选项API isn't designed for such purposes并没有提供操作ImageView源/图像的功能。