我正在尝试创建一个只有一个关于,联系和合法选项的首选项屏幕,所有这些选项在单击时只显示单独页面中的文本模糊和图标,没有shared preferences
或任何内容.
我无法理解层次结构以显示文本.我希望这个流程是:settings -> about -> the about text
目前我有这个,它给了我类别和选项,但我不知道该怎么做才能显示新文本.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Info">
<Preference android:title="About"/>
</PreferenceCategory>
...
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)
我不知道使用什么选项来将可点击进入textview.
我正在尝试使用support.v7.preference实现AppCompat应用程序的首选项.我花了几天时间来完成它,因为support.v7.preference与原生偏好有一些显着的差异......一旦你知道这一点并不是太糟糕,但遗憾的是那里的文档很少.我以为我会分享我的发现,所以其他人不必经历同样的痛苦.
您如何最好地实现AppCompat应用程序的首选项(PreferenceFragment和AppCompatAcitivity不兼容)?
android android-appcompat android-preferences preferencescreen preference-v7
我的要求
注意:我需要支持Android API 15及更高版本.
在我的PreferenceFragment中,我正在将PreferenceScreen动态添加到PreferenceCategory.
PreferenceScreen as = mgr.createPreferenceScreen(context);
as.setTitle(name);
myCategory.addPreference(as);
Run Code Online (Sandbox Code Playgroud)
当设置活动呈现时,这些PreferenceScreens仅在其行的标题中呈现,请参见下图.
我想自定义此行中显示的内容.理想情况下,我希望在下面有一个摘要标题,标题/摘要左侧的图标和右侧的某些行上的图标.请参阅下面的模型图像.
PreferenceScreen.setWidgetLayoutResource(int widgetLayoutResId)
我知道我可以使用"setWidgetLayoutResource"在我的设置活动中使用以下代码在行布局的右侧添加一个图标(以及带有"setSummary"的摘要)
PreferenceScreen as = mgr.createPreferenceScreen(context);
as.setTitle(name);
as.setSummary(summary);
as.setWidgetLayoutResource(R.layout.as_widget);
myCategory.addPreference(as);
Run Code Online (Sandbox Code Playgroud)
其中"as_widget.xml"是
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_favorite"/>
Run Code Online (Sandbox Code Playgroud)
这将产生如下的UI
这让我更接近我想要的但仍然不完全是我想要的(错过了行开头的图标).
PreferenceScreen.setLayoutResource(int layoutResId)
我相信如果我使用它,那么我可以控制整行的渲染.我在stackoverflow上看过这个例子,比如
根据我的理解,您指定的布局文件必须具有以下内容
然而,当我尝试这样做时,Android Studio突出显示"@ + android:id/summary",错误为"无法解析符号'@ + android:id/summary'.当应用程序运行时,我的行呈现为完全空白.
我的java是
PreferenceScreen as = mgr.createPreferenceScreen(context);
as.setTitle(name);
as.setSummary(summary);
as.setLayoutResource(R.layout.as_row_layout);
myCategory.addPreference(as);
Run Code Online (Sandbox Code Playgroud)
我的布局是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/widget_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">
<ImageView
android:id="@+id/icon1"
android:src="@drawable/ic_action_email" …
Run Code Online (Sandbox Code Playgroud) 我的应用程序中有以下首选项屏幕
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="edittexvalue"
android:id="@+id/edittextvalue"
android:title="Title Here"
android:summary="Summary Here"
android:defaultValue="Some Value"/>
<Preference
android:key="customPref"
android:title="Title Here"
android:summary="Summary Here"/>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)
所有这一切工作正常,但我的应用程序我有自定义背景和文本颜色/大小但我无法弄清楚如何格式化PreferenceScreen我假设你指向另一个xml?但无法弄清楚我需要的代码以及应该做出哪些改变.主要关注的是背景颜色文本很好我猜,但背景只是与我的应用程序的其余部分不匹配.所以我想设置一个自定义背景颜色,现在说红色(#ff0000)
我感谢任何可以帮助我解决这个问题的人
我正在尝试创建一个PreferenceScreen但我希望Activity设计像整个项目一样.
如何创建自定义PreferenceScreen设计?
我在我的Android应用程序中用XML定义了两个Android首选项屏幕.
例如,屏幕1
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="screen1">
<PreferenceCategory android:title="Preferences">
<CheckBoxPreference
android:defaultValue="true"
android:title="test"
android:key="test_pref"/>
</PreferenceCategory>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)
和屏幕2
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="screen2">
<CheckBoxPreference
android:key="checkbox"
android:title="Checkbox">
</CheckBoxPreference>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)
我希望屏幕2是一个单独的屏幕,可以自己访问,但我也希望它的首选项也是屏幕的一部分.有一种简单的方法可以简单地从屏幕1中引用屏幕2吗?或者我只需要在屏幕1的子偏好屏幕中重复相同的偏好内容.
如果要在某个layout.xml
文件中访问自定义视图,可以使用以下两个选项:
<package.name.MyView android:layout_width= ... />
<view class="package.name.OuterClass$MyView" android:layout_width= ... />
现在我想在一个内部做同样的事情<PreferenceScreen>
.第一种方法效果很好,但我想将所有自定义Preference
类放在我的PreferenceActivity中.我试过<Preference class="package.name.OuterClass$MyPreference" ... />
(也用'.'而不是'$')<package.name.OuterClass.MyPreference ... />
,但都失败了.
有没有人有想法?
android inner-classes preference preferencescreen android-xml
我有一个使用'自定义布局'的首选项屏幕:
android:layout="@layout/currencycodes"
Run Code Online (Sandbox Code Playgroud)
这个问题是它无法在第一次尝试时调出布局.正如您从下面的动画GIF看到的那样,我必须撤退并再次尝试再次进行布局.那为什么呢?
的preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="application_preferences">
<PreferenceCategory>
<PreferenceScreen
android:key="url_settings"
android:title="@string/url_settings"
android:summary="@string/summary_url_settings">
</PreferenceScreen>
<PreferenceScreen
android:key="currency_codes"
android:title="@string/left_handed"
android:summary="@string/summary_left_handed">
<Preference
android:key="currency_exchanges"
android:layout="@layout/currencycodes"/>
</PreferenceScreen>
<EditTextPreference
android:key="url_exchange_rate"
android:title="@string/url_exchange_rate_settings"
android:summary="@string/summary_url_exchange_rate"
android:enabled = "false"/>
</PreferenceCategory>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)
以下是使用的自定义布局.它包含一个GridView.
currencycodes.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context=".MainActivity">
<GridView
android:layout_width="328dp"
android:layout_height="479dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent" android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent" android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="16dp"
android:id="@+id/grdExchanges" android:scrollbars="horizontal|vertical"
android:smoothScrollbar="true" tools:textAlignment="center"
android:verticalScrollbarPosition="defaultPosition"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud) 我之前按照 Google 在文档中的建议,用新的 DataStore 替换了我的应用程序中的 SharedPreferences,以获得一些明显的好处。然后是添加设置屏幕的时候了,我找到了首选项库。当我看到库默认使用 SharedPreferences 而没有切换到 DataStore 的选项时,困惑就来了。您可以使用setPreferenceDataStore
提供自定义存储实现,但 DataStore 没有实现 PreferenceDataStore 接口,由开发人员决定。是的,这个命名也非常令人困惑。当我发现没有关于将 DataStore 与 Preferences Library 一起使用的文章或问题时,我变得更加困惑,所以我觉得我错过了一些东西。人们是否同时使用这两种存储解决方案?还是其中之一?如果我要在 DataStore 中实现 PreferenceDataStore,有什么我应该注意的问题/陷阱吗?
android sharedpreferences preferencescreen android-jetpack-datastore
在布局中植入微调器很简单,因为有很多样本和教程显示如何做到这一点.
但是如何在PreferenceScreen中植入微调器呢?
这可能/可行吗?
注意:我已经进行了广泛的搜索.没有"SpinnerPreference"这样的东西.我希望有.
android ×10
preferencescreen ×10
preference ×2
xml ×2
android-xml ×1
customizing ×1
java ×1
layout ×1
reference ×1