首页建站preferencescreen(preferencescreen详解)

preferencescreen(preferencescreen详解)

编程之家2024-02-04102次浏览

一、Android:persistent 于preferencescreen中有什么用

Android中,persistent于preferencescreen中对布局界面的可控和高效有帮助,具体体现在:

preferencescreen(preferencescreen详解)

1、在Android系统源码中,绝大多数应用程序的UI布局采用了Preference的布局结构,而不是平时在模拟器中构建应用程序时使用的View布局结构,例如,Setting模块中布局

2、FMRadio应用程序中则使用了View布局结构(可能是该应用程序。归根到底,Preference布局结构和View的布局结构本质上还是大同小异;

3、Preference的优点在于布局界面的可控性和高效率以及可存储值的简洁性(每个PreferenPreferencece存储在相对应下的SharedPreference文件夹下)。

二、preferencescreen可以添加button吗

您好,希望以下回答能帮助您:

可以的。其实我觉得,按照原来的方法,把自定义布局inflate一下,然后调用取到的布局.findViewById应该是可以取到button的。

毕竟你的button已经显示出来了,说明布局时存在的。

preferencescreen(preferencescreen详解)

只不过你直接用findViewById的时候,默认的是当前页面的view,而你的自定义布局与当前页面的view是独立的。

我没有用过preferenceScreen,不知道android:layout那个具体是怎么实现的,这只是我的猜测。

如您还有疑问可继续追问。

三、android preferencescreen走哪个intent

addPreferencesFromResource(R.xml.setting_preference);因为最近的项目我都要把程序的资源文件都放到另一个apk中。而上面这个方法中只能传本地的或系统的资源id。那么我就找到了类似的方法:addPreferencesFromIntent(Intent intent);百度goolge了一下发现都是没有这个方法的例子只有搜索google的里面的api: public void addPreferencesFromIntent(Intent intent) Since: API Level 1 This method is deprecated. This function is not relevant for a modern fragment-based PreferenceActivity. Adds preferences from activities that match the given Intent. Parameters intent The Intent to query activities.这样介绍就很简单了,只是让我们去查询activities。没有说明xml是什么给的。没办法我只能去看源码,看他们是什么解析Intent的,那我就给出一个可用的intent。因为是继承PreferenceFragment的,我就从源码找到frameworks/base/core/java/android/preference/PreferenceFragment.java这个类: view plaincopy to clipboardprint?<span style="white-space:pre"></span>public void addPreferencesFromIntent(Intent intent){ requirePreferenceManager(); setPreferenceScreen(mPreferenceManager.inflateFromIntent(intent, getPreferenceScreen()));}<span style="white-space:pre"></span>public void addPreferencesFromIntent(Intent intent){ requirePreferenceManager(); setPreferenceScreen(mPreferenceManager.inflateFromIntent(intent, getPreferenceScreen()));}然后这里又用到了mPreferenceManager.inflateFromIntentt(intent, getPreferenceScreen()),那么在找到这个类frameworks/base/core/java/android/preference/PreferenceManager.java: view plaincopy to clipboardprint?<span style="white-space:pre"></span>PreferenceScreen inflateFromIntent(Intent queryIntent, PreferenceScreen rootPreferences){ final List<ResolveInfo> activities= queryIntentActivities(queryIntent); final HashSet<String> inflatedRes= new HashSet<String>(); for(int i= activities.size()- 1; i>= 0; i--){ final ActivityInfo activityInfo= activities.get(i).activityInfo; final Bundle metaData= activityInfo.metaData; if((metaData== null)!metaData.containsKey(METADATA_KEY_PREFERENCES)){ continue;}// Need to concat the package with res ID since the same res ID// can be re-used across contexts final String uniqueResId= activityInfo.packageName+":"+ activityInfo.metaData.getInt(METADATA_KEY_PREFERENCES); if(!inflatedRes.contains(uniqueResId)){ inflatedRes.add(uniqueResId); final Context context; try{ context= mContext.createPackageContext(activityInfo.packageName, 0);} catch(NameNotFoundException e){ Log.w(TAG,"Could not create context for"+ activityInfo.packageName+":"+ Log.getStackTraceString(e)); continue;} final PreferenceInflater inflater= new PreferenceInflater(context, this); final XmlResourceParser parser= activityInfo.loadXmlMetaData(context.getPackageManager(), METADATA_KEY_PREFERENCES); rootPreferences=(PreferenceScreen) inflater.inflate(parser, rootPreferences, true); parser.close();}} rootPreferences.onAttachedToHierarchy(this); return rootPreferences;}<span style="white-space:pre"></span>PreferenceScreen inflateFromIntent(Intent queryIntent, PreferenceScreen rootPreferences){ final List<ResolveInfo> activities= queryIntentActivities(queryIntent); final HashSet<String> inflatedRes= new HashSet<String>(); for(int i= activities.size()- 1; i>= 0; i--){ final ActivityInfo activityInfo= activities.get(i).activityInfo; final Bundle metaData= activityInfo.metaData; if((metaData== null)!metaData.containsKey(METADATA_KEY_PREFERENCES)){ continue;}// Need to concat the package with res ID since the same res ID// can be re-used across contexts final String uniqueResId= activityInfo.packageName+":"+ activityInfo.metaData.getInt(METADATA_KEY_PREFERENCES); if(!inflatedRes.contains(uniqueResId)){ inflatedRes.add(uniqueResId); final Context context; try{ context= mContext.createPackageContext(activityInfo.packageName, 0);} catch(NameNotFoundException e){ Log.w(TAG,"Could not create context for"+ activityInfo.packageName+":"+ Log.getStackTraceString(e)); continue;} final PreferenceInflater inflater= new PreferenceInflater(context, this); final XmlResourceParser parser= activityInfo.loadXmlMetaData(context.getPackageManager(), METADATA_KEY_PREFERENCES); rootPreferences=(PreferenceScreen) inflater.inflate(parser, rootPreferences, true); parser.close();}} rootPreferences.onAttachedToHierarchy(this); return rootPreferences;}从上面的代码我们就知道要从intent中获取一组activity。通过 activityInfo.metaData.getInt(METADATA_KEY_PREFERENCES)我们知道就是从activity拿的<meta-data这些数据,那么我们在AndroidManifest.xml中eclipse提示的<meta-data只有三个属性:那么我么就不难得出结论<meta-data中的nane就是METADATA_KEY_PREFERENCES,而xml就是resource。接下来还有一个问题就是METADATA_KEY_PREFERENCES的值是多少。搜一下在PreferenceManager.java中有如下定义: view plaincopy to clipboardprint? public static final String METADATA_KEY_PREFERENCES="android.preference"; public static final String METADATA_KEY_PREFERENCES="android.preference";知道了以上原理我们来验证一下:新建一个工程,在里面的activity标签中加<meta-data,如下: view plaincopy to clipboardprint?<span style="white-space:pre"></span><activity android:name="com.winca.style.defaultskin.RadioReadSettingXMLActivity" android:icon="@drawable/radio" android:label="@string/radio_app_name"><meta-data android:name="android.preference" android:resource="@xml/setting_preference"/></activity><span style="white-space:pre"></span><activity android:name="com.winca.style.defaultskin.RadioReadSettingXMLActivity" android:icon="@drawable/radio" android:label="@string/radio_app_name"><meta-data android:name="android.preference" android:resource="@xml/setting_preference"/></activity>这个xml就是从我们程序中拷贝多来放到新建的工程中的。原来我们工程用的addPreferencesFromResource(R.xml.setting_preference);这语句,我们这样替换: view plaincopy to clipboardprint?<pre name="code" class="java"><span style="white-space:pre"></span>Intent xmlIntent= new Intent(); ComponentName component= new ComponentName("com.winca.style.defaultskin","com.winca.style.defaultskin.RadioReadSettingXMLActivity"); xmlIntent.setComponent(component); addPreferencesFromIntent(xmlIntent);<pre name="code" class="java"><span style="white-space:pre"></span>Intent xmlIntent= new Intent(); ComponentName component= new ComponentName("com.winca.style.defaultskin","com.winca.style.defaultskin.RadioReadSettingXMLActivity"); xmlIntent.setComponent(component); addPreferencesFromIntent(xmlIntent);运行一下,正常显示xml里面的。问题解决。

preferencescreen(preferencescreen详解)
窗口化游戏(win10游戏窗口化的方法是什么)电脑只能进安全模式(怎么解决电脑只能进入安全模式问题)