我需要自定義我的布局,即root_preferences. 例如:為 SwitchPreferenceCompat 或 EditTextPreference 設定重力。這是我第一次使用設定片段,我認為兩者settingFragment并root_Perfenses沒有像普通的片段,我不能給它編號,我不能設定重力。
我的 settingFragment 類:
import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat
import com.example.holyquran.R
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
}
}
我的布局:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory app:title="@string/messages_header">
<EditTextPreference
app:key="signature"
app:title="??? ?????"
app:useSimpleSummaryProvider="true" />
<ListPreference
app:defaultValue="reply"
app:entries="@array/reply_entries"
app:entryValues="@array/reply_values"
app:key="reply"
app:title="@string/reply_title"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
<PreferenceCategory app:title="?????">
<SwitchPreferenceCompat
app:key="sync"
app:title="????" />
<SwitchPreferenceCompat
app:dependency="sync"
app:key="attachment"
app:summaryOff="@string/attachment_summary_off"
app:summaryOn="@string/attachment_summary_on"
app:title="@string/attachment_title" />
</PreferenceCategory>
<PreferenceCategory
app:icon="@drawable/setting"
app:title="settings">
<SeekBarPreference
android:key="volume_notifications"
app:defaultValue="50"
app:showSeekBarValue="true"
app:title="volume" />
<SwitchPreferenceCompat
android:key="notifications"
app:title="Disable notifications"
android:summaryOff="Yow will no longer receive notifications"
android:summaryOn="Yow will receive all notifications"/>
現在我想將重力設定<PreferenceCategory app:title="@string/messages_header">為中心,我知道這樣做并不常見。
還有一件事如何sharePerference在設定片段中使用?我想添加一個SeekBarPreference,我的用戶可以通過像這樣移動它來更改應用程式文本的大小
。
最后但并非最不重要的一點:我的應用程式有振動,并且設定中有一個切換開關,即“????”,當它打開時,振動被啟用,否則它是可禁用的,所以怎么說如果開關是啟用的,否則它被禁用?
正如我之前所說,我沒有任何設定片段的經驗,因為我不應該這樣做這是我的作業狂
這是專案的鏈接,設定檔案夾在 ui 檔案夾中
uj5u.com熱心網友回復:
現在我想將重力設定
<PreferenceCategory app:title="@string/messages_header">為中心,我知道這樣做并不常見。
要做到這一點,您需要創建一個自定義布局,該布局具有TextViewid 的標題,title并且通常將重力設定為android:gravity。您也TextView可以使用在首選項 xml 中無法使用的其他屬性
例如:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
要將其應用于PreferenceCategoryuseandroid:layout屬性:
假設您為標題創建的布局是preference_message_header_title.xml:
<PreferenceCategory
android:layout="@layout/preference_message_header_title"
android:title="@string/messages_header">
還有一件事如何在設定片段中使用 sharePerfenses?我想添加一個 SeekBarPreference,我的用戶可以通過移動它來更改應用程式文本的大小。
最后但并非最不重要的一點:我的應用程式有振動,并且設定中有一個切換開關,即“????”,當它開啟時,振動被啟用,否則它是可禁用的,所以怎么說如果開關是啟用的,否則它被禁用?
這兩個請求相當陌生,但一般的想法是您在首選項 XML 中附加一個首選項鍵,android:key就像您已經對某些首選項所做的那樣。
在設定片段類中,您呼叫findPreference(myPreferenceKey)以獲取與該鍵關聯的首選項,并setOnPreferenceChangeListener()在其上收聽用戶對該首選項所做的更改;然后在偵聽器回呼中通過更改SharedPreference用于跟蹤文本大小或振動狀態的相應值來正常應用它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/345411.html
