---
title: "设置界面设计"
author: "AndLi"
date: "2011-12-25T21:45:12Z"
tags: []
categories: ["移动开发"]
slug: "untitled"
draft: false
lastmod: "2011-12-25T21:48:18Z"
id: 1325356
url: "https://andli.iteye.com/blog/1325356"
---

**如何获得用户名的值呢：**

**// 获得配置参数：用户名**  
  **final String userName = PreferenceManager.getDefaultSharedPreferences(**  
    **this).getString("user\_name", null);**  
![](http://dl.iteye.com/upload/attachment/611419/86f83509-ac89-3587-91d7-8ac4a2fe6839.jpg)  
 AndoridManifest.xml

```
<activity android:name=".view.setting.SetView" >
            <intent-filter >
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
```

 

通过菜单调用：

```
// 菜单-----------------------------------------------------------

	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 1, 0, "设置").setIcon(android.R.drawable.ic_menu_preferences);
		return super.onCreateOptionsMenu(menu);
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		// 设置
		case 1:
			SetView.launch(this);
			break;

		default:

		}
		return super.onOptionsItemSelected(item);
	}
```

 设置页面：

```
package com.lilin.mediaplay.view.setting;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceActivity;

import com.lilin.mediaplay.R;

/**
 * 设置界面
 * 
 * @author lilin
 * @date 2011-12-13 上午12:06:03
 * @ClassName: SettingsActivity
 */
public class SetView extends PreferenceActivity {

	public static void launch(Context c) {
		Intent intent = new Intent(c, SetView.class);
		c.startActivity(intent);
	}

	public void onCreate(Bundle savedInstanceState) {
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
		setContentView(R.layout.setview);
		setTitle("系统设置");
	}

}
```

 布局文件：

一个是preferences

```
<?xml version="1.0" encoding="utf-8"?>
	<!-- 系统设置界面 -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

	<PreferenceCategory android:title="常用设置"
		android:summary="Application settings">
		<!-- 编辑框 -->
		<EditTextPreference 
		    android:key="user_name"
			android:defaultValue="@null" 
			android:title="用户名" 
			android:summary="请设置您的个人用户名" />
		<ListPreference 
			android:key="download_format"
			android:title="下载格式" 
			android:summary="请设置音频下载格式" 
			android:entries="@array/audio_format"
			android:entryValues="@array/audio_format_values" />
			
		<CheckBoxPreference 
			android:key="wifi_only"
			android:defaultValue="false" 
			android:title="WIFI设置" 
			android:summary="只有WIFI模式下才联网" />
		<CheckBoxPreference 
			android:key="roaming_protection"
			android:defaultValue="true" 
			android:title="Roaming Protection"
			android:summary="Disable listening abroad" />

	</PreferenceCategory>

	<PreferenceCategory android:title="其他设置"
		android:summary="Interaction with outside applications">

		<CheckBoxPreference android:key="lastfm_scrobble"
			android:defaultValue="false" 
			android:title="Last.fm Scrobbling"
			android:summary="Requires official last.fm client" />

	</PreferenceCategory>
</PreferenceScreen>
```

  

一个是布局文件：

```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/setbg_dark"
        android:gravity="center"
        android:minHeight="75dip"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:minHeight="75dip"
            android:orientation="horizontal"
            android:paddingLeft="13dip"
            android:paddingRight="13dip" >

            <ImageView
                android:layout_width="48dip"
                android:layout_height="48dip"
                android:src="@drawable/settings" >
            </ImageView>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingLeft="13dip" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:singleLine="true"
                    android:text="系统设置"
                    android:textColor="#ffffff"
                    android:textSize="18dip" >
                </TextView>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#ffffff"
                    android:textSize="12dip" >
                </TextView>

                <TextView
                    android:id="@+id/ItemsCountTextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:text=" "
                    android:textColor="#ffffff"
                    android:textSize="12dip" >
                </TextView>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

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

</LinearLayout>
```

 数组参数：

```
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- 音频格式 -->
    <string-array name="audio_format">
        <item>MP3</item>
        <item>OGG</item>
    </string-array>
    <string-array name="audio_format_values">
        <item>mp31</item>
        <item>ogg2</item>
    </string-array>

</resources>
```