android 知识点累积.docx

上传人:b****7 文档编号:9645278 上传时间:2023-02-05 格式:DOCX 页数:62 大小:47.36KB
下载 相关 举报
android 知识点累积.docx_第1页
第1页 / 共62页
android 知识点累积.docx_第2页
第2页 / 共62页
android 知识点累积.docx_第3页
第3页 / 共62页
android 知识点累积.docx_第4页
第4页 / 共62页
android 知识点累积.docx_第5页
第5页 / 共62页
点击查看更多>>
下载资源
资源描述

android 知识点累积.docx

《android 知识点累积.docx》由会员分享,可在线阅读,更多相关《android 知识点累积.docx(62页珍藏版)》请在冰豆网上搜索。

android 知识点累积.docx

android知识点累积

如何获取标题栏和状态栏高度

1.获取状态栏高度:

decorView是window中的最顶层view,可以从window中获取到decorView,然后decorView有个getWindowVisibleDisplayFrame方法可以获取到程序显示的区域,包括标题栏,但不包括状态栏。

于是可以算出状态栏的高度了。

Rectframe=newRect();

getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

intstatusBarHeight=frame.top;

2.获取标题栏高度:

getWindow().findViewById(Window.ID_ANDROID_CONTENT)这个方法获取到的view就是程序不包括标题栏的部分,然后就可以知道标题栏的高度了。

intcontentTop=getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();

//statusBarHeight是上面所求的状态栏的高度

inttitleBarHeight=contentTop-statusBarHeight;

---------------------------------------------------------------------------------------------

让EditTextView不能输入的方法

etNearbySearch.setFilters(newInputFilter[]{newInputFilter(){

@Override

publicCharSequencefilter(CharSequencesource,intstart,

intend,Spanneddest,intdstart,intdend){

returnsource.length()<1?

dest.subSequence(dstart,dend):

"";

}

}});

---------------------------------------------------------------------------------------------

全屏问题,在程序一加载的时候就让它是全屏

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

AndroidManifest.xml中这样写

name=".TextButtonActivity"android:

label="@string/app_name"

android:

theme="@android:

style/Theme.NoTitleBar.Fullscreen">

---------------------------------------------------------------------------------------------

设置控件是否显示的属性设置

android属性android:

visibility

此属性意思是此视图是否显示,例如RelativeLayout中android:

visibility="gone"

其有三个属性:

visible显示;invisible显示黑背景条;gone不显示

在类中,可以设置其显示与否,setVisibility(View.GONE);不显示

setVisibility(View.VISIBLE);显示

---------------------------------------------------------------------------------------------

为了避免软键盘挡上输入框,可以试试设置

windowSoftInputMode="stateVisible|adjustResize">或者

windowSoftInputMode="stateVisible|adjustPan">

---------------------------------------------------------------------------------------------

设置控件的宽高,用setlayoutparams()方法

---------------------------------------------------------------------------------------------

做应用时,可能会需要动态改变控件的背景图片,如果仅仅是简单的点击,选中之类的事件,如果靠程序中写监听的代码就显得太麻烦了,在这种情况下,你可以使用selector动态改变控件背景

1、在res/drawable目录下建一个handlebackground.xml文件,根据需要,不同的状态下建立不同的item,并对应相应的图片

xmlversion="1.0"encoding="UTF-8"?

>

android="

state_pressed="true"

android:

drawable="@drawable/pressed_application_background_static"/>

state_focused="true"android:

state_enabled="true"

android:

state_window_focused="true"

android:

drawable="@drawable/focused_application_background_static"/>

android:

drawable="@android:

color/transparent"/>

2、在构造layout是引用这个xml

android:

id="@+id/imagebutton"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

src="@drawable/normalbackground"

android:

background="@drawable/handlebackground">

normalbackground.png为你要显示的前景图片

pressed_application_background_static.png为点击时的背景(系统默认为淡黄色)

focused_application_background_static.png为焦点停留时的背景(系统默认为橙色)

---------------------------------------------------------------------------------------------

取得androidsdk的版本,并设置动画效果

intversion=Integer.valueOf(android.os.Build.VERSION.SDK);

if(version>=5){

overridePendingTransition(R.anim.zoomin,R.anim.zoomout);//此为自定义的动画效果,下面两个为系统的

两个自定义的动画效果XML文件,存放位置为:

res/anim/

1,动画进入效果:

zoomin.xml

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

interpolator="@android:

anim/decelerate_interpolator">

fromXScale="2.0"android:

toXScale="1.0"

android:

fromYScale="2.0"android:

toYScale="1.0"

android:

pivotX="50%p"android:

pivotY="50%p"

android:

duration="@android:

integer/config_mediumAnimTime"/>

复制代码2,动画退出效果:

zoomout.xml

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

interpolator="@android:

anim/decelerate_interpolator"

android:

zAdjustment="top">

fromXScale="1.0"android:

toXScale=".5"

android:

fromYScale="1.0"android:

toYScale=".5"

android:

pivotX="50%p"android:

pivotY="50%p"

android:

duration="@android:

integer/config_mediumAnimTime"/>

fromAlpha="1.0"android:

toAlpha="0"

android:

duration="@android:

integer/config_mediumAnimTime"/>

---------------------------------------------------------------------------------------------

获得控件在屏幕或窗口内的坐标

int[]location=newint[2];

view.getLocationInWindow(location);//获取在当前窗口内的绝对坐标

view.getLocationOnScreen(location);//获取在整个屏幕内的绝对坐标

location[0]--->x坐标,location[1]--->y坐标

---------------------------------------------------------------------------------------------

控件对齐那点事

android:

gravity 属性是对该view内容的限定.比如一个button上面的text.你可以设置该text在view的靠左,靠右等位置.该属性就干了这个.

android:

layout_gravity是用来设置该view相对与起父view的位置.比如一个button在linearlayout里,你想把该button放在靠左  靠右等位置就可以通过该属性设置.

---------------------------------------------------------------------------------------------

几种Menu的使用说明

1.OptionsMenu.

通过按下手机上的MENU键,可以打开OptionsMenu。

OptionsMenu最多只能在屏幕最下面显示6个菜单项,称为IconMenu.如果添加了多于6个的菜单项,将通过"More"菜单项显示,这种称为ExpandedMenu.当OptionsMenu第一次被打开时,系统将调用Activity的onCreateOptionsMenu(Menumenu)方法,在这个方法中可以通过配置一个XML文件,或者调用Menu的add()方法来添加你想要的Menu。

Menu的add()方法将返回一个MenuItem对象,你用通过这个对象来配置一些其他属性。

比如:

icon,shortcut,intent。

Menu的add()方法很多,要注意的是itemId这个参数,它的唯一性。

当选择了一个OptionsMenu时,会调用onOptionsItemSelected(MenuItemitem)方法。

如果想修改OptionsMenu,需要重写onPrepareOptionsMenu()方法。

方式一,用add()添加:

/*Createsthemenuitems*/

publicbooleanonCreateOptionsMenu(Menumenu){

menu.add(0,MENU_NEW_GAME,0,"NewGame");

menu.add(0,MENU_QUIT,0,"Quit").setIcon(R.drawable.menu_quit_icon);

returntrue;

}

/*Handlesitemselections*/

publicbooleanonOptionsItemSelected(MenuItemitem){

switch(item.getItemId()){

caseMENU_NEW_GAME:

newGame();

returntrue;

caseMENU_QUIT:

quit();

returntrue;

}

returnfalse;

}

方式二,通过XML文件添加:

在res/menu/下新建options_menu.xml文件:

Xml代码

android="

id="@+id/new_game"

android:

title="NewGame"/>

id="@+id/quit"

android:

title="Quit"/>

在onCreateOptionsMenu()方法中,我们这样写:

Java代码

publicbooleanonCreateOptionsMenu(Menumenu){

MenuInflaterinflater=getMenuInflater();

inflater.inflate(R.menu.options_menu,menu);

returntrue;

}

2.ContextMenu.

它需要注册到某个View对象上,当长按这个View大概2秒时间,会出现这个ContextMenu。

为某个View注册ContextMenu是用registerForContextMenu(Viewview)方法来实现。

ContextMenu在显示前都会调用onCreateContextMenu来生成menu。

onContextItemSelected用来处理选中的菜单项。

注意:

ContextMenu不支持icons和shortcutkeys.

Java代码

publicvoidonCreateContextMenu(ContextMenumenu,Viewv,

ContextMenuInfomenuInfo){

super.onCreateContextMenu(menu,v,menuInfo);

menu.add(0,EDIT_ID,0,"Edit");

menu.add(0,DELETE_ID,0,"Delete");

}

publicbooleanonContextItemSelected(MenuItemitem){

AdapterContextMenuInfoinfo=(AdapterContextMenuInfo)item.getMenuInfo();

switch(item.getItemId()){

caseEDIT_ID:

editNote(info.id);

returntrue;

caseDELETE_ID:

deleteNote(info.id);

returntrue;

default:

returnsuper.onContextItemSelected(item);

}

}

注册这个ContextMenu:

registerForContextMenu(getListView());

这里的getListView()是ListActivity的方法,它将返回一个ListView.记住任何一个View对象都可以注册一个ContextMenu。

3.Submenus

publicbooleanonCreateOptionsMenu(Menumenu){

booleanresult=super.onCreateOptionsMenu(menu);

SubMenufileMenu=menu.addSubMenu("File");

SubMenueditMenu=menu.addSubMenu("Edit");

fileMenu.add(0,NEW_ID,0,"new");

fileMenu.add(0,OPEN_ID,0,"open");

fileMenu.add(0,SAV_ID,0,"save");

editMenu.add(0,UNDO_ID,0,"undo");

editMenu.add(0,REDO_ID,0,"redo");

returnresult;

}

4.MenuFeatures

Menugroups菜单项分组的功能:

在用Menu的add()方法时,通过参数groupId的设置可以实现分组。

setGroupVisible()用来显示或者隐藏你的菜单组。

setGroupEnabled()用来启用或者禁用你的菜单组。

setGroupCheckable()用来设置你的菜单项是否可选。

setGroupCheckable()方法有三个参数intgroup,booleancheckable,booleanexclusive。

第一个参数是要设置的组,第二个参数是否可选,第三个参数为true表示单选,false表示多选。

Shortcutkeys快捷键:

我们还可以设置快捷键。

使用setAlphabeticShortcut(char),setNumericShortcut(int),setShortcut(char,int)可以设置字符,数字,组合(字符+数字)键。

注意:

ContextMenu不能添加快捷键。

---------------------------------------------------------------------------------------------

点击查看活动规则TextView及其子类,当字符内容太长显示不下时可以省略号代替未显示的字符;省略号可以在显示区域的起始,中间,结束位置,或者以跑马灯的方式显示文字(textview的状态为被选中)。

其实现只需在xml中对textview的ellipsize属性做相应的设置即可。

android:

ellipsize="start"省略号在开头

android:

ellipsize="middle"省略号在中间

android:

ellipsize="end"省略号在结尾

android:

ellipsize="marquee"跑马灯显示

---------------------------------------------------------------------------------------------

设置RadioButton的前面的图片,可定制的呵呵

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

orientation="vertical"

android:

checkedButton="@+id/lunch"

android:

id="@+id/menu">

android:

text="@string/radio_group_1_breakfast"

android:

id="@+id/breakfast"

android:

button="@null"//自己找一个图片就可以

/>

RadioButton是可以自己定义button的显示的,我给的例子就不显示button

---------------------------------------------------------------------------------------------

androidlistview的分割线可以改变颜色

android:

id="@+id/android:

list"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

divider="#FFCC00"

android:

dividerHeight="4px"/>

---------------------------------------------------------------------------------------------

保持屏幕唤醒状态

方法1:

usePowerManagerandWakeLockAndroidManifest.xml权限:

name="android.permission.WAKE_LOCK"/>

程序中的代码:

PowerManagerpm=(PowerManager)getSystemService(Con

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1