怎么在ListPreference 或Preference的title前面加图标阿?
比如:(icon)北京  //北京是一个ListPreference
(icon)上海    //上海是一个Preference
(icon)南京   //南京市一个ListPreference

解决方案 »

  1.   

    listpreference,采用的是内置的xml文件,你替换它,采用自己的xml文件布局就可以了 setContentView(R.layout.mylayout);
      

  2.   

    我点每个ListPreference时候,要执行addPreferencesFromResource(R.layout.main);但是我在xml文件里面加入icon,显示不出来
      

  3.   

    像这样
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
        android:title="@string/setting_switch_key">   
       <Preference
         android:icon = "@drawable/icon"
          android:key="@string/setting_line_key"
          android:title="@string/setting_line_key" 
          android:summary="@string/setting_line_key"
          android:persistent="true"/></PreferenceScreen>
      

  4.   

    我就是想这样的icon在title前面显示出来
      

  5.   

    icon并不是preference的属性啊,你这样添加到xml文件中,是无效的
      

  6.   

    那怎么样才能让Icon显示在title的前面呢?在java代码里面怎么改啊 
      

  7.   

     你看这是preference的一段代码,说明你可以指定preference的layout(布局)
    case com.android.internal.R.styleable.Preference_layout:
                        mLayoutResId = a.getResourceId(attr, mLayoutResId);
                        break;
    解决方法,
    1 可以通过代码设置
    setLayoutResource(int layoutResId) ,这个layoutid指向你自己定义的添加了icon的layout布局文件
    2 也可以通过xml文件配置
    android:Preference_layout(这个属性自己查下,我不确定写法正确否)
      

  8.   

    看下preference的帮助文档,里面有两个函数setWidgetLayoutResource()和setLayoutResource(),一个好像是改整体布局,一个是改每个条目的布局,你试试哈。。
      

  9.   

    我在xml文件里面加了android:widgetLayout="@layout/list"
    以下是: list.xml
    <?xml version="1.0" encoding="utf-8"?><!-- Layout used by ImageOptionPreference for the image option style. 
         This is inflated inside android.R.layout.preference. 
         -->
    <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pref_current_img"
        android:layout_width="54dip"
        android:layout_height="54dip"
        android:src="@drawable/icon"
        android:layout_marginLeft="2dip"
        android:layout_gravity="center_vertical"
        android:focusable="false"
        android:clickable="false"   
        android:padding="2dip"   
        />可以在Preference上加上icon了,但是icon总是显示在标题的右面,目前这样显示:
    Preference1   | icon
    Preference2   | icon
    我想让它显示在标题的左面,像这样:
    icon | Preference 1
    icon | Preference 2试着改了都没成功,怎么改能让图标在文字前面呢?
      

  10.   

    你这样做吧。
    1 自己派生一个preference 
      class  myPreference extends preference

      .....
      setLayoutResource(R.layout.item); //这个item,就是你自定义的布局,前面icon,后面title}2 你自己不是listprefence什么的,对应一个preference.xml文件么,那个文件里就用myPreference做item就可以了
      

  11.   

    <?xml version="1.0" encoding="utf-8"?><!-- Layout used by ImageOptionPreference for the image option style. 
         This is inflated inside android.R.layout.preference. 
         -->
    <LineaLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
    <?xml version="1.0" encoding="utf-8"?><!-- Layout used by ImageOptionPreference for the image option style. 
         This is inflated inside android.R.layout.preference. 
         -->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
    <LinearLayout 
        android:id="@+id/LinearLayout01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="3sp"
          >
    <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pref_current_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon"
        android:focusable="false"
        android:clickable="false"
        />
        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pref_current_txt"
        android:text="@string/hello"
       android:layout_width="130dip"
        android:layout_height="30dip"
        
        android:layout_gravity="center_vertical"
        android:focusable="false"
        android:clickable="false"
    />
        </LinearLayout></LinearLayout>
    我在xml文件里面加了一个textview, 现在可以显示成:左边icon,右边title了,但是现在的布局很难看
    icon离左边总是有很大的距离,
    不论我怎么修改  android:layout_marginLeft=   icon都是距离最左边有很大距离, 
    怎么样才能使得icon离左边距小一些呢?
      

  12.   

    <?xml version="1.0" encoding="utf-8"?> <!-- Layout used by ImageOptionPreference for the image option style. 
        This is inflated inside android.R.layout.preference. 
        --> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:orientation="vertical"> 
    <LinearLayout 
        android:id="@+id/LinearLayout01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:layout_marginLeft="3sp" 
          > 
    <ImageView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/pref_current_img" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/icon" 
        android:focusable="false" 
        android:clickable="false" 
        /> 
        <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/pref_current_txt" 
        android:text="@string/hello" 
      android:layout_width="130dip" 
        android:layout_height="30dip" 
        
        android:layout_gravity="center_vertical" 
        android:focusable="false" 
        android:clickable="false" 
    /> 
        </LinearLayout> </LinearLayout> 是这样的布局
      

  13.   

    Preference 里的背景色和字体颜色也能这样改吗?