0
我想在運行時添加Preference
s。問題是,它們看起來不同於靜態定義的Preference
s。我想我可能需要將AttributeSet
傳遞給構造函數,但我不知道如何獲取系統的默認AttributeSet。動態添加的首選項的文本太大
如何動態添加Preference
看起來像靜態的?
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.preference.PreferenceManager;
public static class MyPref extends PreferenceFragmentCompat
{
@Override
public void onCreatePreferences(Bundle bundle, String s)
{
addPreferencesFromResource(R.xml.my_preferences);
final PreferenceCategory category1 = (PreferenceCategory) findPreference("category1");
Preference p = new Preference(this.getContext());
p.setTitle("Santos L. Helper");
p.setSummary("Dynamically added");
category1.addPreference(p);