PreferenceActivity-dagi ActionBar

Example: Android Market

In my application I am using the new Action Bar Compatibility sample from Google (located at /samples/android-/ActionBarCompat) which works great. The only problem I have is applying this to my PreferenceActivity in order to get a screen like the settings in the Android Market (see picture).

Belgilar bilan ActionBar to'ldirish uchun har Activity ActionBarActivity sinfini kengaytirish kerak. Muammo shundaki, Activity allaqachon PreferenceActivity ni kengaytiradi va Java sinflarida bir nechta sinfni uzaytira olmaydi.

ActionBar kodini PreferenceScreen bilan birgalikda olishning usullari bo'lishi kerak. Agar kimdir ushbu umumiy muammo uchun hal qila olsa, men juda xursand bo'lardim.

PS: PreferenceScreenga qanday qilib tugmachani qo'shish mumkin kabi bir yechim ActionBar aslida sarlavha satrida bo'lgani uchun mos emas va shuning uchun bu tartibga qaraganda ko'proq Java.

12
Agar xohlasangiz, men yaratgan kutubxonani sinab ko'rishingiz mumkin: github.com/AndroidDeveloperLB/MaterialStuffLibrary
qo'shib qo'ydi muallif android developer, manba
Ko'p talvasaga tushish va aks ettirish bilan, malakali dasturchi buni amalga oshirishi mumkin. Men sinab ko'rdim - men muvaffaqiyatsiz tugadi.
qo'shib qo'ydi muallif user1974640, manba
Men rasmiy PreferenceFragment ishini qildim!
qo'shib qo'ydi muallif user1974640, manba
Ushbu mavzuni ko'rib chiqing, sizga yordam berishi mumkin: stackoverflow.com/questions/2697233/… [1]: stackoverflow.com/questions/2697233/…
qo'shib qo'ydi muallif thalsharif, manba

8 javoblar

Edit: My answer below is rather hacky and it seems like it is now outdated (for pre Android 3.0) Have a look at the other answers for less hacky and more current solutions ~pyko 2014-09-01


Men uni ishlashga muvaffaq bo'ldim - bu eng yaxshi/eng yaxshi echim ekanligiga ishonch hosil qilmadim, lekin u ishlaydi.

Quyidagi o'zgarishlarni amalga oshirish kerak edi:

  1. Make a copy of ActionBarActivity and have the new class extend PreferenceActivity

    public abstract class ActionBarPreferenceActivity extends PreferenceActivity {
       //contents exactly the same as 'ActionBarActivity'
    }
    
  2. Modify onCreate() in ActionBarHelperBase.java slightly - make a special case for PreferenceActivity classes

    @Override
    public void onCreate(Bundle savedInstanceState) {
       //If the activity is a PreferenceActivity, don't make the request
        if (!(mActivity instanceof PreferenceActivity)) {
            mActivity.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        }
    
  3. Have your PreferenceActivity extend this class and add request for FEATURE_CUSTOM_TITLE before you call super.onCreate()

    public class MyPreferenceActivity extends ActionBarPreferenceActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);//add this line
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
           //etc etc
        }
    
       //etc etc
    }
    

Aytishim mumkinki, PreferenceActivity uchun 2 va 3 o'zgarishlar kerak:

"Super.onCreate() deb atalgan zahotiyoq, ViewGroup tuziladi va shuning uchun Window parametrlarini o'zgartira olmaysiz." ( Oliverning sharhiga javob bering )

PreferenceActivity faoliyatidagi komponentlarning tuzilishi tartibini Activity funktsiyalaridan farqli deb bilaman.

15
qo'shib qo'ydi
PreferenceFragment-da oddiygina yuklanadigan normal harakat paneli sozlamalari buni amalga oshiradi. Agar kimdir bu erga kelsa, ammo bu oddiy echimni hisoblamagan bo'lsa.
qo'shib qo'ydi muallif CQM, manba
"ActionBarActivity nusxasini yarating" deganingizda, manba kodini olish va uni o'z loyihamga nusxalash kerakmi? Masalan, bu yerdanmi? Android »-ni tanlang. googlesource.com/platform/frameworks/support/+/…
qo'shib qo'ydi muallif Someone Somewhere, manba
Ehtimol, bu bir vaqtning o'zida ishlaydi, lekin appcompat-v7_19.1.0 ActionBarActivity # onCreate bilan ishlaydi ActionBarActivityDelegate.createDelegate ((ActionBarActivity) this) . Boshqacha aytganda, PreferenceActivity pastki klassida o'ta olmaysiz.
qo'shib qo'ydi muallif William, manba
Buning uchun sizga rahmat. Buning uchun toza echim yo'q kabi ko'rinadi. Endi Android uyumluluğu harakat satridan ko'ra ko'proq funksionallikni ta'minlovchi Action bar ilovalari uchun ActionBarSherlock ni ishlating.
qo'shib qo'ydi muallif Matthias Robbers, manba
Bu ishni bajarish uchun men Android 3.0 versiyasi (= 11) ning ostida bo'lganligini tekshirishim kerak edi, aks holda bu ishlamagan. Demak, imtiyozli sinfda buni super.onCreate (...) oldin qilish kerak: (ContextUtils.getAndroidApiVersion()
qo'shib qo'ydi muallif dirkvranckaert, manba

Agar support-v4 Fragment asosida PreferenceFragment ilovasini sinab ko'rishni xohlasangiz:

https://github.com/kolavar/android-support-v4-preferencefragment

Men uni o'zim ishlataman va u PreferenceActivity ni PreferenceFragment ga aylantirmaydi.

5
qo'shib qo'ydi
QOYIL!! Bu kishi Google epicni muvaffaqiyatsiz tugadi ! Rahmat, +1
qo'shib qo'ydi muallif Kling Klang, manba

Siz faqat ActionBarActivity uchun kodni klonlaysizmi va "kengaytiradi Faoliyat" ni o'zgartirish uchun "PreferenceActivity"? Keyin ActionBarActivity o'rniga yangi klassni kengaytiring.

Ko'rib turganimdek, barcha Google ilovalaridan PreferenceActivity-ning ish satriga tugma qo'yish odatiy ko'rinadi. Agar siz tugmalarni qo'ymoqchi bo'lsangiz, halo mavzusini ko'rsatish uchun qadriyatlar-v11 muqobil uslubi manbaidan foydalanishingiz mumkin va u uslubni PreferenceActivity uchun manifestga o'rnatasiz.

1
qo'shib qo'ydi
ActionBarActivityDelegate tufayli yo'q
qo'shib qo'ydi muallif user1974640, manba

I used in my application this actionbar https://github.com/johannilsson/android-actionbar and it's work great with this thread How to add a button to PreferenceScreen

1
qo'shib qo'ydi
Rahmat. Endi men ActionBarSherlock dan foydalaning, bu juda yaxshi. Ushbu kutubxonaning birinchi versiyalari johannilssonning "Actionbar" ni asos sifatida ishlatgan.
qo'shib qo'ydi muallif Matthias Robbers, manba

@Pyko ga katta javob bergani uchun minnatdorchiligimni izhor qilmoqchiman, lekin u HoneyComb va undan yuqoriroq ishlamasligi muammosi bor. yaxshi @AndroidDev kabi, uni atrofida olish uchun hack yo'li bo'lishi mumkin;  Lekin @pyko ActionBarHelperBase sinfini ifloslantiradi va @AndroidDev juda shaffof emas. Eng yaxshi usul PreferenceActivity'dan kengaytirilgan ActionBarActivityPreferences yaratish; va onCreate usulida ota-ona usulini chaqirish tartibini o'zgartiring:

@Override
protected void onCreate(Bundle savedInstanceState) {
    //IMPORTATNT: MAKE SURE actionBarHelper called before super;
    //as super oncreate of prefenceactivity is actuallying setting the content view
    mActionBarHelper.onCreate(savedInstanceState);
    super.onCreate(savedInstanceState);
}

Nima uchun "mActionBarHelper.onCreate (savedInstanceState)" deb nomlanadi; ' oldin "super.onCreate (savedInstanceState);" , chunki super (ya'ni PreferenceActivity) aslida onCreate metodida kontent ko'rinishini belgilab qo'yadi, bu esa ishdan chiqishga olib kelishi mumkin ("requestFeature ()" kontent qo'shishdan oldin chaqirilishi kerak). ishonch 'mActionBarHelper.onCreate (saqlanganInstanceState);' "super" deb nomlanadi.  Shu tarzda, "ActionBarHelperBase" ni ifloslantirishimizga hojat yo'q, shuning uchun SettingActivity-ni juda toza tutamiz, chunki "ActionBarActivityPreferences" va portlash uchun jumboqli ma'lumotlarni biriktiramiz!

1
qo'shib qo'ydi

Quyidagi o'zgarishlarga qaraganda afzallikdagi harakat panelini osongina qo'shishingiz mumkin:

AndroidManifest.xml ilovasida:


In v21/styles.xml


Back API yordami uchun v14/styles.xml ilovasida:


1
qo'shib qo'ydi

Rahmat, faqat bitta yangilash, HoneyComb va undan yuqorida qo'llab-quvvatlash uchun Custom Title liniyasidan oldin if ifodasini kiritishingiz kerak.

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
   requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
0
qo'shib qo'ydi

Siz ActionBarSherlock lib olishingiz mumkin va siz kodni SherlockPreference kengaytiradi;

0
qo'shib qo'ydi