Android 中的共享首选项和内部存储有什么区别?数据存储在哪里?看起来Android系统为每个应用程序分配了特定的空间量。在使用共享首选项时,我的应用程序内存不足并抛出 OutOfMemory 异常。如果我使用内部存储将这些数据保存到文件中,这会解决问题吗?如果内部存储也为应用程序使用相同的有限分配空间,那么如何解决这个问题?
我按照谷歌指南(https://developer.android.com/guide/topics/ui/settings)将 PreferenceFragmentCompat 添加到我的应用程序中。
此外,我还有另一个片段(Fragment A),其中包含 MotionLayout 中的 RecyclerView 。MotionLayout用于一个小动画,它通过onSwipe将RecyclerView扩展到垂直全屏。
现在我遇到以下问题:在调用 PreferenceFragment 然后返回到 Fragment A 后,MotionLayout onSwipe 动画的性能确实很差/滞后。根据日志,滑动时会多次调用 onDraw 方法。
有趣的事实是,当我还没有启动 PreferenceFragment 时,onDraw 方法只被调用一次(@onCreate)?!
此外,调用我的其他片段的任意片段 X 不会影响我的片段 A的性能?!
这可能是什么原因?我该如何继续我的调查?
android android-preferences android-fragments preferencefragment android-motionlayout
SharedPreferences(尽管有很多缺点)的优点是可以访问存储数据的 XML。
Jetpack DataStore 在哪里存储数据以及文件是否可访问?
android android-preferences sharedpreferences android-jetpack-datastore
我的应用程序在 res/xml/preferences.xml 中有以下代码:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Wi-Fi settings">
   <EditTextPreference
            android:key="pref_voice_threshold_top"
            android:title="@string/title_pref_voicetopthreshold"
            android:dialogTitle="@string/dialog_title_pref_voicetopthreshold" 
            android:defaultValue="20"
            android:inputType="number"/>
</PreferenceCategory>
</PreferenceScreen>
我想知道我是否可以在代码中使用此首选项,以便我可以通过下载 xml 文件来更新它?
因此,我目前在 PreferenceActivity 中显示上述首选项,效果很好,但是我希望能够通过每周从互联网下载新设置来更新设置。
所以我的问题是如何在代码中打开此首选项并将其值设置为新下载的值?
如何在运行时更改CheckBoxPreference的视图?
具体来说,我想根据用户是否选中此框来更改CheckBoxPreference摘要.
如果这是一个普通的观点,我可以这样做:
view1 = (TextView)findViewById(R.id.idView1);
view1.setText("some text");
但CheckBoxPreference没有id,所以我不知道如何获得它的"句柄".
有没有办法在首选项中显示已安装的应用程序列表?
我正在创建一个通过Intent启动其他应用程序的应用程序:
 PackageManager pm = getApplicationContext().getPackageManager();
 Intent appStartIntent = pm.getLaunchIntentForPackage("NAME OF THE PACKAGE");
 if (null != appStartIntent) { 
          getApplicationContext().startActivity(appStartIntent);
 }
我需要一种从中获取包名称的方法ListPreference,最好的方法是ListPreference包含所有已安装应用程序的图标名称.
这怎么可能?
在从图像选择器返回后(向用户显示他/她选择的内容),我正在尝试设置可点击首选项的图标图像.
像这样的简单设置不起作用,我不知道为什么:
public boolean onActivityResult(int requestCode, int resultCode, Intent intent) {
    mIconPathPreference.setWidgetLayoutResource(R.layout.preference_icon);
}
特别是当将行setWidgetLayoutResource放在其他地方时确实添加了一个图标.
偏好布局:
<Preference
    android:key="@string/iconPath_key"
    android:title="@string/iconPath"
    android:summary="@string/iconPath_summ"
    android:dependency="@string/iconEnable" />
preference_icon布局:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:src="@drawable/ic_launcher" />
我想从设置为的自定义布局访问视图preference
设置屏幕.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android">
    <Preference
            android:key="storage"
            android:layout="@layout/layout_storage" />
    ...
</PreferenceScreen>
设置Fragment.kt
class SettingsFragment: PreferenceFragmentCompat() {
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        addPreferencesFromResource(R.xml.settings_screen)
    }
}
stackoverflow 上有一个与此类似的问题,但在该答案中,他是从主布局文件而不是从preference
PreferenceFragmentCompat 自定义布局
我还发现这篇文章,他使用onBindView方法来访问自定义视图。
https://code.luasoftware.com/tutorials/android/override-layout-of-android-preference/
但没有方法onBindView调用PreferenceFragmentCompat
更新
布局存储.xml
class SettingsFragment: PreferenceFragmentCompat() {
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        addPreferencesFromResource(R.xml.settings_screen)
    }
}
现在我想得到progressBar
在我使用支持库v7的应用程序中,我想添加一个首选项屏幕.由于Google完全没有提供有关该主题的文档,因此我查找了Stack Overflow上其他地方发现的帖子.
所以这是我的项目:
activity_preferences.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:support="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <include layout="@layout/toolbar_default"/>
    <fragment
        android:name=".FragmentPreferences"
        name=".FragmentPreferences"
        android:tag=".FragmentPreferences"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
ActivityPreferences.java
package com.example.testandroidsupportv7;
import com.example.testandroidsupportv7.R;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class ActivityPreferences extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preferences);
    }
}
FragmentPreferences.java
package com.example.testandroidsupportv7;
import com.example.testandroidsupportv7.R;
import android.os.Bundle;
import android.support.v7.preference.PreferenceFragmentCompat;
public class FragmentPreferences extends PreferenceFragmentCompat
{
    @Override
    public void onCreatePreferences(Bundle bundle, String s) {
        addPreferencesFromResource(R.xml.preferences);
    }
}
AndroidManifest.xml中
<activity
        android:name=".ActivityPreferences"
        android:label="@string/app_name"
        android:theme="@style/MyTheme.NoActionBar" …android android-appcompat android-preferences android-support-library