当前位置: 编程技术>移动开发
本页文章导读:
▪设立属性页面 设置属性页面
设置属性页面有两种方式1、用xml完全的配置方式2、完全使用代码的方式xml配置方式:
<?xml version="1.0" encoding="utf-8"?>
<!-- This is a primitive example showing the different types of p.........
▪ 平添屏幕待机方法 添加屏幕待机方法
import android.os.SystemClock;android.os.PowerManager;//添加屏幕待机方法 public static void goToSleep(Context context,long time){ context.enforceCallingOrSelfPermission(android.Manifest.permission.DEV.........
▪ view运用 view使用
View的xml属性android:background背景android:clickable是否响应点击事件android:contentDescriptionview的简要描述android:drawingCacheQuality绘制缓存的质量,绘制缓存其实就是一个bitmap。默认是ARGB_8888(即.........
[1]设立属性页面
来源: 互联网 发布时间: 2014-02-18
设置属性页面
设置属性页面有两种方式
1、用xml完全的配置方式
2、完全使用代码的方式
xml配置方式:
下面是代码的方式:
设置属性页面有两种方式
1、用xml完全的配置方式
2、完全使用代码的方式
xml配置方式:
<?xml version="1.0" encoding="utf-8"?>
<!-- This is a primitive example showing the different types of preferences available. -->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/inline_preferences">
<CheckBoxPreference
android:key="checkbox_preference"
android:title="@string/title_toggle_preference"
android:summary="@string/summary_toggle_preference" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/dialog_based_preferences">
<EditTextPreference
android:key="edittext_preference"
android:title="@string/title_edittext_preference"
android:summary="@string/summary_edittext_preference"
android:dialogTitle="@string/dialog_title_edittext_preference" />
<ListPreference
android:key="list_preference"
android:title="@string/title_list_preference"
android:summary="@string/summary_list_preference"
android:entries="@array/entries_list_preference"
android:entryValues="@array/entryvalues_list_preference"
android:dialogTitle="@string/dialog_title_list_preference" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/launch_preferences">
<!-- This PreferenceScreen tag serves as a screen break (similar to page break
in word processing). Like for other preference types, we assign a key
here so it is able to save and restore its instance state. -->
<PreferenceScreen
android:key="screen_preference"
android:title="@string/title_screen_preference"
android:summary="@string/summary_screen_preference">
<!-- You can place more preferences here that will be shown on the next screen. -->
<CheckBoxPreference
android:key="next_screen_checkbox_preference"
android:title="@string/title_next_screen_toggle_preference"
android:summary="@string/summary_next_screen_toggle_preference" />
</PreferenceScreen>
<PreferenceScreen
android:title="@string/title_intent_preference"
android:summary="@string/summary_intent_preference">
<intent android:action="/blog_article/android.intent.action.VIEW"
android:data="http://www.android.com" />
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory
android:title="@string/preference_attributes">
<CheckBoxPreference
android:key="parent_checkbox_preference"
android:title="@string/title_parent_preference"
android:summary="@string/summary_parent_preference" />
<!-- The visual style of a child is defined by this styled theme attribute. -->
<CheckBoxPreference
android:key="child_checkbox_preference"
android:dependency="parent_checkbox_preference"
android:layout="?android:attr/preferenceLayoutChild"
android:title="@string/title_child_preference"
android:summary="@string/summary_child_preference" />
</PreferenceCategory>
</PreferenceScreen>
下面是代码的方式:
package com.example.android.apis.app;
import android.content.Intent;
import android.content.res.TypedArray;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import com.example.android.apis.R;
public class PreferencesFromCode extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setPreferenceScreen(createPreferenceHierarchy());
}
private PreferenceScreen createPreferenceHierarchy() {
// Root
PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
// Inline preferences
PreferenceCategory inlinePrefCat = new PreferenceCategory(this);
inlinePrefCat.setTitle(R.string.inline_preferences);
root.addPreference(inlinePrefCat);
// Toggle preference
CheckBoxPreference togglePref = new CheckBoxPreference(this);
togglePref.setKey("toggle_preference");
togglePref.setTitle(R.string.title_toggle_preference);
togglePref.setSummary(R.string.summary_toggle_preference);
inlinePrefCat.addPreference(togglePref);
// Dialog based preferences
PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this);
dialogBasedPrefCat.setTitle(R.string.dialog_based_preferences);
root.addPreference(dialogBasedPrefCat);
// Edit text preference
EditTextPreference editTextPref = new EditTextPreference(this);
editTextPref.setDialogTitle(R.string.dialog_title_edittext_preference);
editTextPref.setKey("edittext_preference");
editTextPref.setTitle(R.string.title_edittext_preference);
editTextPref.setSummary(R.string.summary_edittext_preference);
dialogBasedPrefCat.addPreference(editTextPref);
// List preference
ListPreference listPref = new ListPreference(this);
listPref.setEntries(R.array.entries_list_preference);
listPref.setEntryValues(R.array.entryvalues_list_preference);
listPref.setDialogTitle(R.string.dialog_title_list_preference);
listPref.setKey("list_preference");
listPref.setTitle(R.string.title_list_preference);
listPref.setSummary(R.string.summary_list_preference);
dialogBasedPrefCat.addPreference(listPref);
// Launch preferences
PreferenceCategory launchPrefCat = new PreferenceCategory(this);
launchPrefCat.setTitle(R.string.launch_preferences);
root.addPreference(launchPrefCat);
/*
* The Preferences screenPref serves as a screen break (similar to page
* break in word processing). Like for other preference types, we assign
* a key here so that it is able to save and restore its instance state.
*/
// Screen preference
PreferenceScreen screenPref = getPreferenceManager().createPreferenceScreen(this);
screenPref.setKey("screen_preference");
screenPref.setTitle(R.string.title_screen_preference);
screenPref.setSummary(R.string.summary_screen_preference);
launchPrefCat.addPreference(screenPref);
/*
* You can add more preferences to screenPref that will be shown on the
* next screen.
*/
// Example of next screen toggle preference
CheckBoxPreference nextScreenCheckBoxPref = new CheckBoxPreference(this);
nextScreenCheckBoxPref.setKey("next_screen_toggle_preference");
nextScreenCheckBoxPref.setTitle(R.string.title_next_screen_toggle_preference);
nextScreenCheckBoxPref.setSummary(R.string.summary_next_screen_toggle_preference);
screenPref.addPreference(nextScreenCheckBoxPref);
// Intent preference
PreferenceScreen intentPref = getPreferenceManager().createPreferenceScreen(this);
intentPref.setIntent(new Intent().setAction(Intent.ACTION_VIEW)
.setData(Uri.parse("http://www.android.com")));
intentPref.setTitle(R.string.title_intent_preference);
intentPref.setSummary(R.string.summary_intent_preference);
launchPrefCat.addPreference(intentPref);
// Preference attributes
PreferenceCategory prefAttrsCat = new PreferenceCategory(this);
prefAttrsCat.setTitle(R.string.preference_attributes);
root.addPreference(prefAttrsCat);
// Visual parent toggle preference
CheckBoxPreference parentCheckBoxPref = new CheckBoxPreference(this);
parentCheckBoxPref.setTitle(R.string.title_parent_preference);
parentCheckBoxPref.setSummary(R.string.summary_parent_preference);
prefAttrsCat.addPreference(parentCheckBoxPref);
// Visual child toggle preference
// See res/values/attrs.xml for the <declare-styleable> that defines
// TogglePrefAttrs.
TypedArray a = obtainStyledAttributes(R.styleable.TogglePrefAttrs);
CheckBoxPreference childCheckBoxPref = new CheckBoxPreference(this);
childCheckBoxPref.setTitle(R.string.title_child_preference);
childCheckBoxPref.setSummary(R.string.summary_child_preference);
childCheckBoxPref.setLayoutResource(
a.getResourceId(R.styleable.TogglePrefAttrs_android_preferenceLayoutChild,
0));
prefAttrsCat.addPreference(childCheckBoxPref);
a.recycle();
return root;
}
}
[2] 平添屏幕待机方法
来源: 互联网 发布时间: 2014-02-18
添加屏幕待机方法
import android.os.SystemClock;
android.os.PowerManager;
//添加屏幕待机方法
public static void goToSleep(Context context,long time){
context.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
final AlertDialog dialog = new AlertDialog.Builder(context)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(com.android.internal.R.string.sleep)
.setMessage(com.android.internal.R.string.sleep_confirm)
.setPositiveButton(com.android.internal.R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick
(DialogInterface dialog, int which){
PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
pm.goToSleep(SystemClock.uptimeMillis());
}
})
.setNegativeButton(com.android.internal.R.string.no, null)
.create();
dialog.getWindow().setType
(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
dialog.getWindow().addFlags
(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
dialog.show();
}
import android.os.SystemClock;
android.os.PowerManager;
//添加屏幕待机方法
public static void goToSleep(Context context,long time){
context.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
final AlertDialog dialog = new AlertDialog.Builder(context)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(com.android.internal.R.string.sleep)
.setMessage(com.android.internal.R.string.sleep_confirm)
.setPositiveButton(com.android.internal.R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick
(DialogInterface dialog, int which){
PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
pm.goToSleep(SystemClock.uptimeMillis());
}
})
.setNegativeButton(com.android.internal.R.string.no, null)
.create();
dialog.getWindow().setType
(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
dialog.getWindow().addFlags
(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
dialog.show();
}
[3] view运用
来源: 互联网 发布时间: 2014-02-18
view使用
View的xml属性
android:background
背景
android:clickable
是否响应点击事件
android:contentDescription
view的简要描述
android:drawingCacheQuality
绘制缓存的质量,绘制缓存其实就是一个bitmap。默认是ARGB_8888(即:rgb + alpha各用8位),减少其质量可以降低内存占用
android:duplicateParentState
android:fadingEdge
在view滚动时,是否淡出相应的边界。vertical淡出上下的边界,horizontal淡出左右的边界,none无淡出边界
android:fadingEdgeLength
淡出边界的长度
android:filterTouchesWhenObscured
view所在窗口被其它可见窗口遮住时,是否过滤触摸事件。
android:fitsSystemWindows
调整基于系统窗口的view布局,如status bar就是基于系统窗口的。
android:focusable
是否可获得焦点
android:focusableInTouchMode
在触摸时,是否可获得焦点
android:hapticFeedbackEnabled
是否启用触摸反馈,启用后就是在点击等操作时会有震动等反馈效果。
android:id
id标识
android:isScrollContainer
是否将view作为滚动容器。能够调整它所在窗口的大小,以便为输入法窗口腾出空间。不过该属性不知道怎么用,好像activity默认就是这么处理的。
android:keepScreenOn
view所在的窗口可见时,保持屏幕打开。
android:longClickable
是否响应长点击事件
android:minHeight
view的最小高度
android:minWidth
view的最小宽度
android:nextFocusDown
向下移动焦点时,下一个获取焦点的view的id
android:nextFocusLeft
向左移动焦点时,下一个获取焦点的view的id
android:nextFocusRight
向右移动焦点时,下一个获取焦点的view的id
android:nextFocusUp
向上移动焦点时,下一个获取焦点的view的id
android:onClick
点击时,要调用的方法的名称。就是OnClickListener,系统将该调用转发到了我们这边指定的方法。方法要public void xxx(View view),否则会抛找不到方法的异常
android:padding
设置上下左右的边距
android:paddingBottom
下边距
android:paddingLeft
左边距
android:paddingRight
右边距
android:paddingTop
上边距
android:saveEnabled
在配置改变等情况出现时是否保存view的状态数据。如果你的view有id,那默认系统就会帮你保存。
android:scrollX
x方向的滚动偏移。即在水平方向滚动了多少距离
android:scrollY
y方向的滚动偏移。即在垂直方向滚动了多少距离
android:scrollbarAlwaysDrawHorizontalTrack
总是绘制水平滚动条的滚动轨道
android:scrollbarAlwaysDrawVerticalTrack
总是绘制垂直滚动条的滚动轨道
android:scrollbarDefaultDelayBeforeFade
滚动条在n毫秒后开始淡出。(毫秒)
android:scrollbarFadeDuration
滚动条用多长时间淡出完毕。(毫秒)
android:scrollbarSize
垂直滚动条的宽度、水平滚动条的高度
android:scrollbarStyle
滚动条的风格。insideOverlay内贴图、insideInset内插图;outsideOverlay外贴图、outsideInset外插图;
inside就是滚动条在绘制在padding以内;outside就是不需要绘制在padding内(即view的边界处)
Overlay是贴图,就是直接覆盖在内容的上方,这样内容可能会显示到滚动条下方去;Inset是插图,就是会在对应padding上加上滚动条的宽度,以不让内容显示到滚动条下面去。
这部分的源码
android:scrollbarThumbHorizontal
水平滚动块的图片
android:scrollbarThumbVertical
垂直滚动块的图片
android:scrollbarTrackHorizontal
水平滚动条滚动轨道的图片
android:scrollbarTrackVertical
垂直滚动条滚动轨道的图片
android:scrollbars
要显示的滚动条。none不显示任何滚动条,vertical显示垂直的滚动条,horizontal显示水平的滚动条。也可以vertical|horizontal,垂直、水平的滚动条都要显示
android:soundEffectsEnabled
点击或触摸该view时,是否需要有声音效果
android:tag
string标识。类似id,id是整数标识。
android:visibility
view的可见性。gone不可见,同时不占用view的空间;invisible不可见,但占用view的空间;visible可见
View的xml属性
android:background
背景
android:clickable
是否响应点击事件
android:contentDescription
view的简要描述
android:drawingCacheQuality
绘制缓存的质量,绘制缓存其实就是一个bitmap。默认是ARGB_8888(即:rgb + alpha各用8位),减少其质量可以降低内存占用
android:duplicateParentState
android:fadingEdge
在view滚动时,是否淡出相应的边界。vertical淡出上下的边界,horizontal淡出左右的边界,none无淡出边界
android:fadingEdgeLength
淡出边界的长度
android:filterTouchesWhenObscured
view所在窗口被其它可见窗口遮住时,是否过滤触摸事件。
android:fitsSystemWindows
调整基于系统窗口的view布局,如status bar就是基于系统窗口的。
android:focusable
是否可获得焦点
android:focusableInTouchMode
在触摸时,是否可获得焦点
android:hapticFeedbackEnabled
是否启用触摸反馈,启用后就是在点击等操作时会有震动等反馈效果。
android:id
id标识
android:isScrollContainer
是否将view作为滚动容器。能够调整它所在窗口的大小,以便为输入法窗口腾出空间。不过该属性不知道怎么用,好像activity默认就是这么处理的。
android:keepScreenOn
view所在的窗口可见时,保持屏幕打开。
android:longClickable
是否响应长点击事件
android:minHeight
view的最小高度
android:minWidth
view的最小宽度
android:nextFocusDown
向下移动焦点时,下一个获取焦点的view的id
android:nextFocusLeft
向左移动焦点时,下一个获取焦点的view的id
android:nextFocusRight
向右移动焦点时,下一个获取焦点的view的id
android:nextFocusUp
向上移动焦点时,下一个获取焦点的view的id
android:onClick
点击时,要调用的方法的名称。就是OnClickListener,系统将该调用转发到了我们这边指定的方法。方法要public void xxx(View view),否则会抛找不到方法的异常
android:padding
设置上下左右的边距
android:paddingBottom
下边距
android:paddingLeft
左边距
android:paddingRight
右边距
android:paddingTop
上边距
android:saveEnabled
在配置改变等情况出现时是否保存view的状态数据。如果你的view有id,那默认系统就会帮你保存。
android:scrollX
x方向的滚动偏移。即在水平方向滚动了多少距离
android:scrollY
y方向的滚动偏移。即在垂直方向滚动了多少距离
android:scrollbarAlwaysDrawHorizontalTrack
总是绘制水平滚动条的滚动轨道
android:scrollbarAlwaysDrawVerticalTrack
总是绘制垂直滚动条的滚动轨道
android:scrollbarDefaultDelayBeforeFade
滚动条在n毫秒后开始淡出。(毫秒)
android:scrollbarFadeDuration
滚动条用多长时间淡出完毕。(毫秒)
android:scrollbarSize
垂直滚动条的宽度、水平滚动条的高度
android:scrollbarStyle
滚动条的风格。insideOverlay内贴图、insideInset内插图;outsideOverlay外贴图、outsideInset外插图;
inside就是滚动条在绘制在padding以内;outside就是不需要绘制在padding内(即view的边界处)
Overlay是贴图,就是直接覆盖在内容的上方,这样内容可能会显示到滚动条下方去;Inset是插图,就是会在对应padding上加上滚动条的宽度,以不让内容显示到滚动条下面去。
这部分的源码
public void setPadding(int left, int top, int right, int bottom) {
boolean changed = false;
mUserPaddingRight = right;
mUserPaddingBottom = bottom;
final int viewFlags = mViewFlags;
// Common case is there are no scroll bars.
/* android中用一个int变量的各个bit来标识view的一些属性
* 如这里就是用SCROLLBARS_VERTICAL(0x00000200)来标识有垂直滚动条
*/
// 存在垂直或水平滚动条时
if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
// TODO: Deal with RTL languages to adjust left padding instead of right.
// 存在垂直滚动条
if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
// overlay时,右padding加0;inset时,右padding加上滚动条宽度
right += (viewFlags & SCROLLBARS_INSET_MASK) == 0
? 0 : getVerticalScrollbarWidth();
}
// 存在水平滚动条。不过是不是代码写错了啊,应该是!= 0啊
if ((viewFlags & SCROLLBARS_HORIZONTAL) == 0) {
bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
? 0 : getHorizontalScrollbarHeight();
}
}
// 与原来的不同才要重新设置
if (mPaddingLeft != left) {
changed = true;
mPaddingLeft = left;
}
if (mPaddingTop != top) {
changed = true;
mPaddingTop = top;
}
if (mPaddingRight != right) {
changed = true;
mPaddingRight = right;
}
if (mPaddingBottom != bottom) {
changed = true;
mPaddingBottom = bottom;
}
if (changed) {
requestLayout();
}
}
android:scrollbarThumbHorizontal
水平滚动块的图片
android:scrollbarThumbVertical
垂直滚动块的图片
android:scrollbarTrackHorizontal
水平滚动条滚动轨道的图片
android:scrollbarTrackVertical
垂直滚动条滚动轨道的图片
android:scrollbars
要显示的滚动条。none不显示任何滚动条,vertical显示垂直的滚动条,horizontal显示水平的滚动条。也可以vertical|horizontal,垂直、水平的滚动条都要显示
android:soundEffectsEnabled
点击或触摸该view时,是否需要有声音效果
android:tag
string标识。类似id,id是整数标识。
android:visibility
view的可见性。gone不可见,同时不占用view的空间;invisible不可见,但占用view的空间;visible可见
最新技术文章: