2013-05-09 157 views
2

我從一個xml文件使用SherlockPreferenceActivity for Preference活動。下面的代碼:自定義操作欄Sherlock偏好活動列表視圖

public class Setting extends SherlockPreferenceActivity 
{ 
protected void onCreate(Bundle paramBundle) 
{ 
    super.onCreate(paramBundle); 
    addPreferencesFromResource(R.xml.setting); 
} 

一切工作正常,但我需要留餘量,右是0dip(下圖) enter image description here

我曾嘗試在ABS源代碼中找到,但未能將其自定義。任何人都有這樣做?

更新:我上傳更多細節圖像 enter image description here

Setting.xml的內容:

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 
    <PreferenceCategory android:title="@string/system_setting" > 
     <CheckBoxPreference 
      android:defaultValue="true" 
      android:key="mode_on_off" 
      android:summaryOff="Sub Setting 1" 
      android:summaryOn="Sub Setting 1 On" 
      android:title="Setting 1" /> 
     <CheckBoxPreference 
      android:defaultValue="true" 
      android:key="filter_on_off" 
      android:summaryOff="Sub Setting 2" 
      android:summaryOn="Sub Setting 2 On" 
      android:title="Setting 2" /> 
     <CheckBoxPreference 
      android:defaultValue="false" 
      android:key="screen" 
      android:summaryOff="Sub Setting 3" 
      android:summaryOn="Sub Setting 3 on" 
      android:title="Setting 2" /> 

    </PreferenceCategory> 
</PreferenceScreen> 

回答

1

我設法得到它的工作 - 不知道這是否是最好的/乾淨的解決方案,但它的工作原理。

不得不做出以下更改:

  1. 讓ActionBarActivity的副本,並有新的類擴展PreferenceActivity

    公共抽象類ActionBarPreferenceActivity擴展PreferenceActivity {// 內容完全一樣的「 ActionBarActivity' }

  2. 修改onCreate()在ActionBarHelperBase.java中稍微 - 爲Prefere做一個特例nceActivity類

    @覆蓋 公共無效的onCreate(捆綁savedInstanceState){// 如果活動是PreferenceActivity,不作要求 如果(!(mActivity的instanceof PreferenceActivity)){ mActivity.requestWindowFeature(窗口.FEATURE_CUSTOM_TITLE); }

  3. 有你PreferenceActivity擴展此類和FEATURE_CUSTOM_TITLE添加請求調用super.onCreate(前)

公共類MyPreferenceActivity擴展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 

} 據正如我所看到的,需要更改2和3,因爲對於PreferenceActivity:

「210

」只要你調用super.onCreate(),ViewGroup就會被設置,所以你不能改變Window的參數。「 (請參閱Oliver對答案的評論)

我猜測PreferenceActivity活動中組件的創建順序與普通的Activity活動不同。

+0

可能是我沒有寫清楚,我已經上傳細節圖像 – DzungPV 2013-05-09 16:08:25

0

用這樣的方式:

enter image description here

在活動

import android.os.Bundle; 

import com.actionbarsherlock.app.ActionBar; 
import com.actionbarsherlock.app.SherlockPreferenceActivity; 

public class MainActivity extends SherlockPreferenceActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // setContentView(R.layout.custom_infowindow); 
     final ActionBar actionBar = getSupportActionBar(); 
     actionBar.setCustomView(R.layout.custom_infowindow); 
     actionBar.setDisplayShowTitleEnabled(false); 
     actionBar.setDisplayShowCustomEnabled(true); 
     actionBar.setDisplayUseLogoEnabled(false); 
     actionBar.setDisplayShowHomeEnabled(false); 

     addPreferencesFromResource(R.xml.test); 
    } 

} 

custom_infowindow.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Right Now Event" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="#FF0000" /> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="5dp" 
     android:background="@color/_blue" /> 

</LinearLayout> 
+0

我嘗試了你的方法,但不工作,我已經上傳更多的細節圖像 – DzungPV 2013-05-09 16:06:34