怎么在ListPreference 或Preference的title前面加图标阿? 怎么在ListPreference 或Preference的title前面加图标阿?比如:(icon)北京 //北京是一个ListPreference(icon)上海 //上海是一个Preference(icon)南京 //南京市一个ListPreference 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 listpreference,采用的是内置的xml文件,你替换它,采用自己的xml文件布局就可以了 setContentView(R.layout.mylayout); 我点每个ListPreference时候,要执行addPreferencesFromResource(R.layout.main);但是我在xml文件里面加入icon,显示不出来 像这样<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> 我就是想这样的icon在title前面显示出来 icon并不是preference的属性啊,你这样添加到xml文件中,是无效的 那怎么样才能让Icon显示在title的前面呢?在java代码里面怎么改啊 你看这是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(这个属性自己查下,我不确定写法正确否) 看下preference的帮助文档,里面有两个函数setWidgetLayoutResource()和setLayoutResource(),一个好像是改整体布局,一个是改每个条目的布局,你试试哈。。 我在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 | iconPreference2 | icon我想让它显示在标题的左面,像这样:icon | Preference 1icon | Preference 2试着改了都没成功,怎么改能让图标在文字前面呢? 你这样做吧。1 自己派生一个preference class myPreference extends preference{ ..... setLayoutResource(R.layout.item); //这个item,就是你自定义的布局,前面icon,后面title}2 你自己不是listprefence什么的,对应一个preference.xml文件么,那个文件里就用myPreference做item就可以了 <?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离左边距小一些呢? <?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> 是这样的布局 Preference 里的背景色和字体颜色也能这样改吗? 如何解析这个文件 如何判断栈里是否存在一个activity??大牛来帮我啊 有人在乐pad上安装过应用吗?请问乐pad2.0如何完全退出程序啊? 请问为什么我用setPreviewCallback会黑屏呢? 急!!android(乐phone)联网问题!!! 平板android系统移植 为啥我在4.2模拟器上运行程序弹出的对话框样子还是2.3的样子 android 离线地图 错误 安卓fragment问题 初学者求大神指点 (求大神!急急急)弹出菜单 并让菜单有左右滑动效果 如何让图片随机移动, android 硬盘挂载的代码是在vold里面吗?
<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> 是这样的布局