让我自己对打开的大量看起来相关但没有完全回答我的问题的 SO 选项卡感到非常困惑,所以最后我承认失败并问我自己的问题。
我想实现一个设置页面。我正在使用片段和 ViewPager,因为我有两个主屏幕在相同的标题和操作栏(编码和解码屏幕)下运行。这是我的代码片段(试图去掉看起来不相关的部分)
MainActivity.java
package uk.ac.ox.bras2756.colourgrid;
import android.app.ActionBar; ...etc
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
ViewPager mViewPager; ...etc
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
final ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(
actionBar.newTab()
.setText(mAppSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
@Override
public void onTabUnselected / Selected / Reselected ...etc
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:...etc
}
}
public static class LaunchpadSectionFragmentEncode extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_encode, container, false);
...return rootView;
}
}
public static class LaunchpadSectionFragmentDecode extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_decode, container, false);
...return rootView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.activity_main, menu);
return(super.onCreateOptionsMenu(menu));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
Intent intent = new Intent(this, SetPreferenceActivity.class);
startActivity(intent);
return(true);
}
return(super.onOptionsItemSelected(item));
}
}
}
activity_main.xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
PrefsFragment.java
package uk.ac.ox.bras2756.colourgrid;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.util.Log;
public class PrefsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
SetPreferenceActivity.java
package uk.ac.ox.bras2756.colourgrid;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class SetPreferenceActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.i("SetPrefs", "Got there");
getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragment()).commit();
}
}
res\xml\preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:key="prefkey_error"
android:summary="@string/pref_sum_error"
android:title="@string/pref_title_error"
android:defaultValue="true" />
<PreferenceCategory
android:title="@string/pref_decode"
android:key="prefkey_decode">
<ListPreference
android:key="prefkey_matrix"
android:title="@string/pref_title_matrix"
android:summary="@string/pref_sum_matrix"
android:entries="@array/matrixArray"
android:entryValues="@array/matrixArrayValues" />
<ListPreference
android:key="prefkey_rotate"
android:title="@string/pref_title_rotate"
android:summary="@string/pref_sum_rotate"
android:entries="@array/rotateArray"
android:entryValues="@array/rotateArrayValues" />
<CheckBoxPreference
android:key="prefkey_weight"
android:summary="@string/pref_sum_weight"
android:title="@string/pref_title_weight"
android:defaultValue="true" />
<ListPreference
android:key="prefkey_colour"
android:title="@string/pref_title_colour"
android:entries="@array/colourArray"
android:entryValues="@array/colourArrayValues" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/pref_camera"
android:key="prefkey_camera">
<CheckBoxPreference
android:key="prefkey_hotspots"
android:title="@string/pref_title_hotspots"
android:defaultValue="true" />
</PreferenceCategory>
</PreferenceScreen>
我的应用程序加载正常,但当然因为缺少某些东西,选项按钮没有显示。我错过了什么?我会一直在这里检查,直到我能解决这个问题——时间紧迫!
此外,由于某种原因,我的首选项文件旁边总是有 * 表示它没有保存,当我在手机上运行应用程序时,它总是要求我先保存它,尽管手动保存它。不知道那里发生了什么!
如果您需要查看更多代码,请告诉我,尝试仅提供所需的内容,但我不是 100% 确定。
编辑:全部排序。更新了我上面的代码,以防它可以帮助其他人。