我正在开发一个材料设计应用程序.
在SettingsActivity.java&中添加settings.xml之后运行应用程序,活动显示如下:
public class SettingsActivity extends AppCompatActivity { Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_settings); SpannableString s = new SpannableString("Settings"); s.setSpan(new TypefaceSpan(this,"Roboto-Medium.ttf"),s.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setTitle(s); // Display the fragment as the main content. getFragmentManager().beginTransaction() .replace(android.R.id.content,new SettingsFragment()) .commit(); } public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener { public static final String SHARE_APP_PREFERENCE_KEY = "pref_key_share_app"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.settings); } public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) { if (key.equals(SHARE_APP_PREFERENCE_KEY)) { // do something } } } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.abc.xxx.SettingsActivity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" android:elevation="4dp"/> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceCategory android:title="@string/pref_notification" > <CheckBoxPreference android:defaultValue="true" android:key="prefNotification" android:summary="@string/pref_notify_summary" > </CheckBoxPreference> </PreferenceCategory> </PreferenceScreen>
我不知道为什么settings.xml布局与ActionBar / Toolbar重叠!
请告诉我.
提前致谢.
解决方法
将您的activity_settings.xml更改为此
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.abc.xxx.SettingsActivity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" android:elevation="4dp"/> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_below="@id/toolbar" android:layout_height="match_parent" /> </RelativeLayout>
并将代码更改为此
// Display the fragment as the main content. getFragmentManager().beginTransaction() .replace(R.id.fragment_container,new SettingsFragment()) .commit();