怎么在ListPreference 或Preference的title前面加图标阿?
比如:(icon)北京 //北京是一个ListPreference
(icon)上海 //上海是一个Preference
(icon)南京 //南京市一个ListPreference
比如:(icon)北京 //北京是一个ListPreference
(icon)上海 //上海是一个Preference
(icon)南京 //南京市一个ListPreference
<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>
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(这个属性自己查下,我不确定写法正确否)
以下是: 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试着改了都没成功,怎么改能让图标在文字前面呢?
1 自己派生一个preference
class myPreference extends preference
{
.....
setLayoutResource(R.layout.item); //这个item,就是你自定义的布局,前面icon,后面title}2 你自己不是listprefence什么的,对应一个preference.xml文件么,那个文件里就用myPreference做item就可以了
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离左边距小一些呢?
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> 是这样的布局