将button添加到PreferenceFragment

我想在首选项屏幕的底部添加一些button来设置默认值并恢复默认值。 这个答案不包括如何使用PreferenceFragment做到这一点。 推荐的方法是什么?

加载首选项片段的活动:

public class SettingsActivity extends Activity { @Override public void onCreate( Bundle savedInstanceState) { super.onCreate( savedInstanceState); // load up the preferences fragment getFragmentManager().beginTransaction().replace( android.R.id.content, new PrefsSettingsFragment()).commit(); } } 

PreferenceFragment实现:

 public class PrefsSettingsFragment extends PreferenceFragment { @Override public void onCreate( Bundle savedInstanceState) { super.onCreate( savedInstanceState); addPreferencesFromResource( R.xml.preferences); } } 

的preferences.xml:

 <?xml version="1.0"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <EditTextPreference android:key="edit_text_preference_server_address" android:title="@string/preference_server_address"/> <SwitchPreference android:key="switch_preference_bat" android:title="@string/preference_bat"/> <SwitchPreference android:key="switch_preference_comm" android:title="@string/preference_comm"/> <SwitchPreference android:key="switch_preference_dev_mode" android:title="@string/preference_dev_mode" android:defaultValue="true"/> </PreferenceScreen> 

我遇到了同样的问题,我find了解决这个问题的方法。

在PreferenceFragment中覆盖方法onCreateView,使用给定的LayoutInfalter参数创build自己的View,并返回该视图。

这是我的代码。 我希望这可以有所帮助

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View v = inflater.inflate(R.layout.set_alarm, null); Button save = (Button)v.findViewById(R.id.save); Button revert = (Button)v.findViewById(R.id.revert); Button delete = (Button)v.findViewById(R.id.delete); save.setOnClickListener(this); revert.setOnClickListener(this); delete.setOnClickListener(this); if(mId == -1){ delete.setEnabled(false); } return v; } 

我稍微修改了以前的文章,这样button就会被粘贴在视图的底部。

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { LinearLayout v = (LinearLayout) super.onCreateView(inflater, container, savedInstanceState); Button btn = new Button(getActivity().getApplicationContext()); btn.setText("Button on Bottom"); v.addView(btn); btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { } }); return v; } 

只需创build您自己的设置活动的布局,其中包含ID为@android:id / list的列表视图

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:id="@+id/fragment" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp"> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent"/> </FrameLayout> <Button android:id="@+id/button" android:text="Save" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 

然后在activity类中添加首选项片段之前设置content视图

 public class SettingsActivity extends Activity { @Override public void onCreate( Bundle savedInstanceState) { super.onCreate( savedInstanceState); setContentView(R.layout.settings_activity) // load up the preferences fragment getFragmentManager().beginTransaction().replace( android.R.id.content, new PrefsSettingsFragment()).commit(); } } 

你可以试试这个

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub LinearLayout v = (LinearLayout) super.onCreateView(inflater, container, savedInstanceState); Button SendLogbtn = new Button(getActivity().getApplicationContext()); SendLogbtn.setText("send log file"); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.setMargins(100, 0, 0, 500); SendLogbtn.setLayoutParams(params); v.addView(SendLogbtn); SendLogbtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // do your code } }); return v; }