将PreferenceScreen添加到linearlayout

时间:2014-01-17 20:58:05

标签: java android xml android-layout

嗨,我是Android和Java的新手,遇到了问题。我的问题是我正在尝试将PreferenceScreen添加到我的线性布局中,这样我就可以在 res / layout / activity_main.xml 中的搜索栏上设置setOnSeekBarChangeListener()。目前,PreferenceScreen和LinearLayout被添加到视图中,但它们彼此重叠,这不是我想要的。相反,我希望PreferenceScreen代替我的布局文件中的ListView。

RES / XML / settings.xml中

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:title="Category"
        android:key="category_preference">
        <SwitchPreference
            android:key="switch_preference"
            android:title="Title"
            android:summary="Summary"
            android:defaultValue="true" />

        <CheckBoxPreference
            android:key="checkbox_preference"
            android:title="Title"
            android:summary="Summary"
            android:defaultValue="true" />
    </PreferenceCategory>
</PreferenceScreen>

RES /布局/ activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <SeekBar
        android:id="@+id/main_seek_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
        setContentView(R.layout.activity_main);

        SeekBar seekBar = (SeekBar) findViewById(R.id.main_seek_bar);

        seekBar.setOnTouchListener(new SeekBar.OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                // TODO Auto-generated method stub
                return false;
            }
        });
    }

    public static class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
        public static final String KEY_PREF_SYNC_CONN = "pref_syncConnectionType";

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.settings);
            onSharedPreferenceChanged(null, "");
        }

        @Override
        public void onResume() {
            super.onResume();
            getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
        }

        @Override
        public void onPause() {
            super.onPause();
            getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
        }

        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            if (key.equals("switch_preference")) {
                Toast.makeText(getActivity(), "Switch", Toast.LENGTH_LONG).show();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

您的res / layout / activity_main.xml文件中的

更改

<ListView android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

<fragment android:id="@+id/fragment_settings"
          android:layout_width="fill_parent"
          android:name="yourpackagename.MainActivity$SettingsFragment"
          android:layout_height="fill_parent" />

然后在您的MainActivity.java文件中删除以下行

getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();