我想添加几个按钮来我的喜好屏幕底部设置默认值和恢复默认设置。 这个答案不包括如何做到这一点使用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>
我遇到同样的问题,我找到了一种方法来处理这个问题。
覆盖方法onCreateView在PreferenceFragment,使用给定的LayoutInfalter通过参数来创建自己的视图,并返回这一观点。
这是我的代码。 我希望这可以帮助
@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;
}
我修改了以前的帖子一点点,这样按钮将在视图的底部得到重视。
@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;
}
刚创建的设置活动包含ID为@android列表视图自己的布局:ID /名单
<?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>
然后在活动类设置的内容视图中添加偏好片段之前
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;
}