Add buttons to PreferenceFragment

我们两清 提交于 2019-12-03 11:52:58

I meet the same question, and I found a way to handle this issue.

Overwrite the method onCreateView in PreferenceFragment, using the given the LayoutInfalter by parameters to create your own View, and return this view.

It's my code. I hope this can be helpful

@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;
    }

I modified the previous post a little bit, so that the button will get attached at the bottom of the view.

    @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;
    }

Just create your own layout for the settings activity which contains list view with 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>

And then in the activity class set the content view before adding preference fragment

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();
    }
}

you may try this

@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;
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!