一、preferencefragment加图片
PreferenceFragment是一个用于管理偏好设置的Fragment,它提供了一个简单的方法来显示设置页面。可以将PreferenceFragment视为一个容器,其中包含一些Preference,可以对其进行访问和编辑。在Preference界面中,可以添加图片作为设置项的图标,使其更具有可读性和美观性。以下是向PreferenceFragment中添加图标的代码示例:
//在xml文件中设置图标
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="general_settings"
android:title="普通设置"
android:summary="设置常规选项">
<Preference
android:key="audio_play"
android:title="播放音乐"
android:summary="勾选该项后,音乐播放时将发出声音"
android:icon="@drawable/ic_audio" />
</PreferenceCategory>
</PreferenceScreen>
在上述示例中,添加了一个名为“audio_play”的偏好设置项,它的图标是@drawable/ic_audio。在实际开发中,可以根据需要更改图标,使其更符合应用程序的设计。
二、preferencefragment 自定义界面
默认情况下,PreferenceFragment会使用系统提供的布局和样式来显示偏好设置界面。但是,如果需要自定义偏好设置页面的外观和行为,则可以创建自己的布局文件,并在PreferenceFragment中将其设置为视图。以下是一个自定义偏好设置界面的代码示例:
//自定义布局文件my_preference.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="general_settings"
android:title="普通设置"
android:summary="设置常规选项">
<CheckBoxPreference
android:key="audio_play"
android:title="播放音乐"
android:summary="勾选该项后,音乐播放时将发出声音"
android:defaultValue="true" />
</PreferenceCategory>
</PreferenceScreen>
//自定义PreferenceFragment
public class MyPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.my_preference);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.preference_layout, container, false);
return view;
}
}
//preference_layout.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="这是一个自定义的设置界面"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
<fragment
android:id="@+id/my_preference_fragment"
android:name="com.example.MyPreferenceFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
在上述代码示例中,MyPreferenceFragment继承自PreferenceFragment,并在onCreate()方法中指定了自己的布局文件my_preference.xml。在onCreateView()方法中,将preference_layout.xml设置为视图,并将MyPreferenceFragment添加到布局中。这样就可以自定义偏好设置页面的界面,使其更符合应用程序的设计需求。
三、preferencefragment toolbar选取
在支持toolbar的应用程序中,可以将PreferenceFragment与Toolbar结合使用,使其在UI中良好地融合。以下是一个使用Toolbar的代码示例:
//MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getFragmentManager().beginTransaction().replace(R.id.content_frame, new MyPreferenceFragment()).commit();
}
}
//MyPreferenceFragment.java
public class MyPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.my_preference);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
toolbar.setTitle("偏好设置");
toolbar.setNavigationIcon(R.drawable.ic_back);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().onBackPressed();
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.preference_layout, container, false);
return view;
}
}
//preference_layout.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
<fragment
android:id="@+id/my_preference_fragment"
android:name="com.example.MyPreferenceFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
在上述代码示例中,MainActivity中的Toolbar与MyPreferenceFragment的Toolbar进行了关联。在MyPreferenceFragment中,通过view.findViewById(R.id.toolbar)获取到布局文件preference_layout.xml中的Toolbar,并设置其标题和返回按钮。如果需要自定义Toolbar的样式,可以在styles.xml文件中设置ThemeOverlay.AppCompat.ActionBar样式。这样就可以在PreferenceFragment中使用Toolbar,使其在UI中更加美观和方便。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/153870.html
微信扫一扫
支付宝扫一扫