11

我想在我的首选项屏幕底部添加几个按钮,用于设置默认值和恢复默认值。这个答案不包括如何使用 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);             
    }       
}

首选项.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>
4

4 回答 4

10

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

重写 PreferenceFragment 中的 onCreateView 方法,使用给定的 LayoutInfalter by 参数创建自己的 View,并返回这个 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;
    }
于 2012-09-26T16:02:15.767 回答
8

我稍微修改了上一篇文章,以便按钮将附加在视图的底部。

    @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;
    }
于 2013-11-15T11:47:38.587 回答
5

只需为包含 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>

然后在活动类中设置内容视图,然后添加偏好片段

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();
    }
}
于 2016-02-27T11:32:23.173 回答
1

你可以试试这个

@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;
        }
于 2013-02-12T18:58:43.627 回答