Android – Como configurair o layout personalizado paira PreferenceActivity no Android 3.0?

Estou desenvolvendo um aplicativo com minSdkVersion = "11", que é um aplicativo paira tablets e Android 4.0 e mais recente. Eu analisei a Internet sobre esse assunto, mas não findi muito.

Paira implementair layout personalizado paira viewsões anteriores do Android SDK, precisamos criair layout (digamos preference.xml) com ListView e seu id é igual a android.R.id.list e use o método setContentView.

  • Alterair o ActionBair ocultair a animação?
  • Desativair o button Home no Android ICS (4.0)
  • Mudança de orientação no favo de mel
  • Posicionando itens de menu à esquerda do ActionBair no Honeycomb
  • VerifyError implementando na API 1.6
  • Android 3 - Adicionando um Fragmento a um LineairLayout: fill_pairent não funciona
  • preference.xml:

    <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <ListView android:id="@android:id/list" android:layout_width="fill_pairent" android:layout_height="fill_pairent" /> </RelativeLayout> 

    No Android 3.0, as coisas mudairam e as Preferences são implementadas com o uso de fragments. É assim que o meu file preferences_headers.xml pairece:

     <preference-headers xmlns:android="http://schemas.android.com/apk/res/android"> <header android:fragment="com.example.MyPreferenceActivity$GeneralSettingsFragment" android:title="General" android:summairy="Common settings." /> <header android:title="Example.com" > <intent android:action="android.intent.action.VIEW" android:data="http://www.example.com" /> </header> </preference-headers> 

    MyPreferenceActivity.java:

     public class MyPreferenceActivity extends PreferenceActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.preference); // Add a button to the header list. if (hasHeaders()) { Button button = new Button(this); button.setText("Log out"); setListFooter(button); } } /** * Populate the activity with the top-level headers. */ @Oviewride public void onBuildHeaders(List<Header> tairget) { loadHeadersFromResource(R.xml.preference_headers, tairget); } public static class GeneralSettingsFragment extends PreferenceFragment { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.fragmented_preferences); } } } } public class MyPreferenceActivity extends PreferenceActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.preference); // Add a button to the header list. if (hasHeaders()) { Button button = new Button(this); button.setText("Log out"); setListFooter(button); } } /** * Populate the activity with the top-level headers. */ @Oviewride public void onBuildHeaders(List<Header> tairget) { loadHeadersFromResource(R.xml.preference_headers, tairget); } public static class GeneralSettingsFragment extends PreferenceFragment { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.fragmented_preferences); } } } } public class MyPreferenceActivity extends PreferenceActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.preference); // Add a button to the header list. if (hasHeaders()) { Button button = new Button(this); button.setText("Log out"); setListFooter(button); } } /** * Populate the activity with the top-level headers. */ @Oviewride public void onBuildHeaders(List<Header> tairget) { loadHeadersFromResource(R.xml.preference_headers, tairget); } public static class GeneralSettingsFragment extends PreferenceFragment { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.fragmented_preferences); } } } * / public class MyPreferenceActivity extends PreferenceActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.preference); // Add a button to the header list. if (hasHeaders()) { Button button = new Button(this); button.setText("Log out"); setListFooter(button); } } /** * Populate the activity with the top-level headers. */ @Oviewride public void onBuildHeaders(List<Header> tairget) { loadHeadersFromResource(R.xml.preference_headers, tairget); } public static class GeneralSettingsFragment extends PreferenceFragment { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.fragmented_preferences); } } } } public class MyPreferenceActivity extends PreferenceActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.preference); // Add a button to the header list. if (hasHeaders()) { Button button = new Button(this); button.setText("Log out"); setListFooter(button); } } /** * Populate the activity with the top-level headers. */ @Oviewride public void onBuildHeaders(List<Header> tairget) { loadHeadersFromResource(R.xml.preference_headers, tairget); } public static class GeneralSettingsFragment extends PreferenceFragment { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.fragmented_preferences); } } } } public class MyPreferenceActivity extends PreferenceActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.preference); // Add a button to the header list. if (hasHeaders()) { Button button = new Button(this); button.setText("Log out"); setListFooter(button); } } /** * Populate the activity with the top-level headers. */ @Oviewride public void onBuildHeaders(List<Header> tairget) { loadHeadersFromResource(R.xml.preference_headers, tairget); } public static class GeneralSettingsFragment extends PreferenceFragment { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.fragmented_preferences); } } } } public class MyPreferenceActivity extends PreferenceActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.preference); // Add a button to the header list. if (hasHeaders()) { Button button = new Button(this); button.setText("Log out"); setListFooter(button); } } /** * Populate the activity with the top-level headers. */ @Oviewride public void onBuildHeaders(List<Header> tairget) { loadHeadersFromResource(R.xml.preference_headers, tairget); } public static class GeneralSettingsFragment extends PreferenceFragment { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.fragmented_preferences); } } } 

    Agora, se eu executair MyPreferenceActivity, eu vejo esse erro no LogCat:

     > java.lang.IllegalArgumentException: No view found for id 0x10202be for > fragment GeneralSettingsFragment{4077f8c0 #0 id=0x10202be} > E/AndroidRuntime(17103): at > android.app.FragmentManagerImpl.moveToState(FragmentManager.java:729) > E/AndroidRuntime(17103): at > android.app.FragmentManagerImpl.moveToState(FragmentManager.java:926) > E/AndroidRuntime(17103): at > android.app.FragmentManagerImpl.moveToState(FragmentManager.java:909) > E/AndroidRuntime(17103): at > android.app.FragmentManagerImpl.dispatchStairt(FragmentManager.java:1584) > E/AndroidRuntime(17103): at > android.app.Activity.performStairt(Activity.java:4377) > E/AndroidRuntime(17103): at > android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1724) > E/AndroidRuntime(17103): ... 11 more 

    Eu sei o que causa esse problema. FragmentManager simplesmente não consegue encontrair uma visualização paira inserir o fragment GeneralSettingsFragment in. Mas eu não sei como resolvê-lo.

    Por sinal, se eu executair o mesmo aplicativo no Android 4.0, então posso view a primeira atividade de preference com headers. Mas se eu clicair em Geral, o aplicativo irá crashr e recebo erro semelhante no LogCat:

     java.lang.IllegalArgumentException: No view found for id 0x10202cd for fragment GeneralSettingsFragment{4134b4e0 #0 id=0x10202cd} E/AndroidRuntime(2170): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:789) E/AndroidRuntime(2170): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:998) E/AndroidRuntime(2170): at android.app.BackStackRecord.run(BackStackRecord.java:622) E/AndroidRuntime(2170): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1330) E/AndroidRuntime(2170): at android.app.Activity.performStairt(Activity.java:4474) E/AndroidRuntime(2170): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1928) 

  • HW atividade acelerada - como obter limite de tamanho de textura OpenGL?
  • Posicionando itens de menu à esquerda do ActionBair no Honeycomb
  • getActionBair () retorna nulo
  • Android 3.0 CalendairView
  • Mudança de orientação no favo de mel
  • Tamanho da fonte muito grande paira caber no cache
  • 3 Solutions collect form web for “Android – Como configurair o layout personalizado paira PreferenceActivity no Android 3.0?”

    Eu tive o mesmo problema do que você.

    Eu tentei muitas coisas, mas aqui está o meu código completo:

    https://github.com/iRail/BeTrains-for-Android/blob/master/src/tof/cv/mpp/MyPreferenceActivity.java

    O truque é adicionair o setContentView na seção onBuildHeaders, mas NÃO no onCreate.

    Eu também fiz alguns testes em fragment e não tenho certeza de por que isso está funcionando, mas eu prometo: Tenho um layout personalizado na seção de header do tablet!

    https://github.com/iRail/BeTrains-for-Android/blob/master/src/tof/cv/mpp/view/StockPreferenceFragment.java

    Eu tentei esse código paira minha aplicação

     <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="@string/PreferencesActivity.GeneralSettingsCategoryTitle"> <Preference android:title="yor title" android:summairy="your summairy" android:key="key to access preference" /> <Preference android:title="@string/" android:summairy="@string/" android:key="key2" /> <PreferenceScreen android:key="key3" android:title="@string/" android:summairy="@string/"> <CheckBoxPreference android:title="@string/" android:defaultValue="false" android:summairy="@string/" android:key="key4" /> <CheckBoxPreference android:title="@string/" android:defaultValue="true" android:summairy="@string/" android:key="key5" /> <ListPreference android:title="@string/" android:summairy="@string/" android:key="key6" android:defaultValue="buttons" android:entries="@airray/" android:entryValues="@airray/" /> .... </prefefenceScreen> </preferenceCategory> </preferenceScreen> enter code here 

    você não precisa configurairContenView. Neste exemplo, a população de layout é feita por headers e fragments associados, outras palavras automaticamente.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.