Android開(kāi)發(fā)知識(shí)點(diǎn)總結(jié)_第1頁(yè)
Android開(kāi)發(fā)知識(shí)點(diǎn)總結(jié)_第2頁(yè)
Android開(kāi)發(fā)知識(shí)點(diǎn)總結(jié)_第3頁(yè)
Android開(kāi)發(fā)知識(shí)點(diǎn)總結(jié)_第4頁(yè)
Android開(kāi)發(fā)知識(shí)點(diǎn)總結(jié)_第5頁(yè)
已閱讀5頁(yè),還剩49頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

怎樣獲取標(biāo)題欄和狀態(tài)欄高度1.獲取狀態(tài)欄高度:decorView是window中最頂層view,能夠從window中獲取到decorView,然后decorView有個(gè)getWindowVisibleDisplayFrame方法能夠獲取到程序顯示區(qū)域,包含標(biāo)題欄,但不包含狀態(tài)欄。于是能夠算出狀態(tài)欄高度了。Rectframe=newRect();getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);intstatusBarHeight=frame.top;2.獲取標(biāo)題欄高度:getWindow().findViewById(Window.ID_ANDROID_CONTENT)這個(gè)方法獲取到view就是程序不包含標(biāo)題欄部分,然后就能夠知道標(biāo)題欄高度了。intcontentTop=getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();//statusBarHeight是上面所求狀態(tài)欄高度inttitleBarHeight=contentTop-statusBarHeight;---------------------------------------------------------------------------------------------讓EditTextView不能輸入方法etNearbySearch.setFilters(newInputFilter[]{newInputFilter(){@OverridepublicCharSequencefilter(CharSequencesource,intstart,intend,Spanneddest,intdstart,intdend){returnsource.length()<1?dest.subSequence(dstart,dend):"";}}});---------------------------------------------------------------------------------------------全屏問(wèn)題,在程序一加載時(shí)候就讓它是全屏this.requestWindowFeature(Window.FEATURE_NO_TITLE);this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);AndroidManifest.xml中這么寫(xiě)<activityandroid:name=".TextButtonActivity"android:label="@string/app_name"android:theme="@android:style/Theme.NoTitleBar.Fullscreen">---------------------------------------------------------------------------------------------設(shè)置控件是否顯示屬性設(shè)置android屬性android:visibility此屬性意思是此視圖是否顯示,比如RelativeLayout中android:visibility="gone"其有三個(gè)屬性:visible顯示;invisible顯示黑背景條;gone不顯示在類(lèi)中,能夠設(shè)置其顯示是否,setVisibility(View.GONE);不顯示setVisibility(View.VISIBLE);顯示---------------------------------------------------------------------------------------------為了防止軟鍵盤(pán)擋上輸入框,能夠試試設(shè)置<activityandroid:windowSoftInputMode="stateVisible|adjustResize">或者<activityandroid:windowSoftInputMode="stateVisible|adjustPan">---------------------------------------------------------------------------------------------設(shè)置控件寬高,用setlayoutparams()方法---------------------------------------------------------------------------------------------做應(yīng)用時(shí),可能會(huì)需要?jiǎng)討B(tài)改變控件背景圖片,假如僅僅是簡(jiǎn)單點(diǎn)擊,選中之類(lèi)事件,假如靠程序中寫(xiě)監(jiān)聽(tīng)代碼就顯得太麻煩了,在這種情況下,你能夠使用selector動(dòng)態(tài)改變控件背景1、在res/drawable目錄下建一個(gè)handlebackground.xml文件,依照需要,不一樣狀態(tài)下建立不一樣item,并對(duì)應(yīng)對(duì)應(yīng)圖片<?xmlversion="1.0"encoding="UTF-8"?><selectorxmlns:android=""><itemandroid:state_pressed="true"android:drawable="@drawable/pressed_application_background_static"/><itemandroid:state_focused="true"android:state_enabled="true"android:state_window_focused="true"android:drawable="@drawable/focused_application_background_static"/><itemandroid:drawable="@android:color/transparent"/></selector>2、在結(jié)構(gòu)layout是引用這個(gè)xml<ImageButtonandroid:id="@+id/imagebutton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/normalbackground"android:background="@drawable/handlebackground"></ImageButton>normalbackground.png為你要顯示前景圖片pressed_application_background_static.png為點(diǎn)擊時(shí)背景(系統(tǒng)默認(rèn)為淡黃色)focused_application_background_static.png為焦點(diǎn)停留時(shí)背景(系統(tǒng)默認(rèn)為橙色)---------------------------------------------------------------------------------------------取得androidsdk版本,并設(shè)置動(dòng)畫(huà)效果intversion=Integer.valueOf(android.os.Build.VERSION.SDK);if(version>=5){overridePendingTransition(R.anim.zoomin,R.anim.zoomout);//此為自定義動(dòng)畫(huà)效果,下面兩個(gè)為系統(tǒng)兩個(gè)自定義動(dòng)畫(huà)效果XML文件,存放位置為:res/anim/1,動(dòng)畫(huà)進(jìn)入效果:zoomin.xml<?xmlversion="1.0"encoding="utf-8"?><setxmlns:android=""android:interpolator="@android:anim/decelerate_interpolator"><scaleandroid: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"/></set>復(fù)制代碼2,動(dòng)畫(huà)退出效果:zoomout.xml<?xmlversion="1.0"encoding="utf-8"?><setxmlns:android=""android:interpolator="@android:anim/decelerate_interpolator"android:zAdjustment="top"><scaleandroid: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"/><alphaandroid:fromAlpha="1.0"android:toAlpha="0"android:duration="@android:integer/config_mediumAnimTime"/></set>---------------------------------------------------------------------------------------------取得控件在屏幕或窗口內(nèi)坐標(biāo)int[]location=newint[2];view.getLocationInWindow(location);//獲取在當(dāng)前窗口內(nèi)絕對(duì)坐標(biāo)view.getLocationOnScreen(location);//獲取在整個(gè)屏幕內(nèi)絕對(duì)坐標(biāo)location[0]--->x坐標(biāo),location[1]--->y坐標(biāo)---------------------------------------------------------------------------------------------控件對(duì)齊那點(diǎn)事android:gravity屬性是對(duì)該view內(nèi)容限定.比如一個(gè)button上面text.你能夠設(shè)置該text在view靠左,靠右等位置.該屬性就干了這個(gè).a(chǎn)ndroid:layout_gravity是用來(lái)設(shè)置該view相對(duì)與起父view位置.比如一個(gè)button在linearlayout里,你想把該button放在靠左靠右等位置就能夠經(jīng)過(guò)該屬性設(shè)置.---------------------------------------------------------------------------------------------幾個(gè)Menu使用說(shuō)明1.OptionsMenu.經(jīng)過(guò)按下手機(jī)上MENU鍵,能夠打開(kāi)OptionsMenu。OptionsMenu最多只能在屏幕最下面顯示6個(gè)菜單項(xiàng),稱(chēng)為IconMenu.假如添加了多于6個(gè)菜單項(xiàng),將經(jīng)過(guò)"More"菜單項(xiàng)顯示,這種稱(chēng)為ExpandedMenu.當(dāng)OptionsMenu第一次被打開(kāi)時(shí),系統(tǒng)將調(diào)用ActivityonCreateOptionsMenu(Menumenu)方法,在這個(gè)方法中能夠經(jīng)過(guò)配置一個(gè)XML文件,或者調(diào)用Menuadd()方法來(lái)添加你想要Menu。Menuadd()方法將返回一個(gè)MenuItem對(duì)象,你用經(jīng)過(guò)這個(gè)對(duì)象來(lái)配置一些其余屬性。比如:icon,shortcut,intent。Menuadd()方法很多,要注意是itemId這個(gè)參數(shù),它唯一性。當(dāng)選擇了一個(gè)OptionsMenu時(shí),會(huì)調(diào)用onOptionsItemSelected(MenuItemitem)方法。假如想修改OptionsMenu,需要重寫(xiě)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;}方式二,經(jīng)過(guò)XML文件添加:在res/menu/下新建options_menu.xml文件:Xml代碼<menuxmlns:android=""><itemandroid:id="@+id/new_game"android:title="NewGame"/><itemandroid:id="@+id/quit"android:title="Quit"/></menu>在onCreateOptionsMenu()方法中,我們這么寫(xiě):Java代碼publicbooleanonCreateOptionsMenu(Menumenu){MenuInflaterinflater=getMenuInflater();inflater.inflate(R.menu.options_menu,menu);returntrue;}2.ContextMenu.它需要注冊(cè)到某個(gè)View對(duì)象上,當(dāng)長(zhǎng)按這個(gè)View大約2秒時(shí)間,會(huì)出現(xiàn)這個(gè)ContextMenu。為某個(gè)View注冊(cè)ContextMenu是用registerForContextMenu(Viewview)方法來(lái)實(shí)現(xiàn)。ContextMenu在顯示前都會(huì)調(diào)用onCreateContextMenu來(lái)生成menu。onContextItemSelected用來(lái)處理選中菜單項(xiàng)。注意: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);}}注冊(cè)這個(gè)ContextMenu:registerForContextMenu(getListView());這里getListView()是ListActivity方法,它將返回一個(gè)ListView.記住任何一個(gè)View對(duì)象都能夠注冊(cè)一個(gè)ContextMenu。3.SubmenuspublicbooleanonCreateOptionsMenu(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.MenuFeaturesMenugroups菜單項(xiàng)分組功效:在用Menuadd()方法時(shí),經(jīng)過(guò)參數(shù)groupId設(shè)置能夠?qū)崿F(xiàn)分組。setGroupVisible()用來(lái)顯示或者隱藏你菜單組。setGroupEnabled()用來(lái)啟用或者禁用你菜單組。setGroupCheckable()用來(lái)設(shè)置你菜單項(xiàng)是否可選。setGroupCheckable()方法有三個(gè)參數(shù)intgroup,booleancheckable,booleanexclusive。第一個(gè)參數(shù)是要設(shè)置組,第二個(gè)參數(shù)是否可選,第三個(gè)參數(shù)為true表示單項(xiàng)選擇,false表示多項(xiàng)選擇。Shortcutkeys快捷鍵:我們還能夠設(shè)置快捷鍵。使用setAlphabeticShortcut(char),setNumericShortcut(int),setShortcut(char,int)能夠設(shè)置字符,數(shù)字,組合(字符+數(shù)字)鍵。注意:ContextMenu不能添加緊捷鍵。---------------------------------------------------------------------------------------------點(diǎn)擊查看活動(dòng)規(guī)則TextView及其子類(lèi),當(dāng)字符內(nèi)容太長(zhǎng)顯示不下時(shí)能夠省略號(hào)代替未顯示字符;省略號(hào)能夠在顯示區(qū)域起始,中間,結(jié)束位置,或者以跑馬燈方式顯示文字(textview狀態(tài)為被選中)。其實(shí)現(xiàn)只需在xml中對(duì)textviewellipsize屬性做對(duì)應(yīng)設(shè)置即可。android:ellipsize="start"省略號(hào)在開(kāi)頭android:ellipsize="middle"省略號(hào)在中間android:ellipsize="end"省略號(hào)在結(jié)尾android:ellipsize="marquee"跑馬燈顯示---------------------------------------------------------------------------------------------設(shè)置RadioButton前面圖片,可定制呵呵<RadioGroupandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:checkedButton="@+id/lunch"android:id="@+id/menu"><RadioButtonandroid:text="@string/radio_group_1_breakfast"android:id="@+id/breakfast"android:button="@null"http://自己找一個(gè)圖片就能夠/>RadioButton是能夠自己定義button顯示,我給例子就不顯示button---------------------------------------------------------------------------------------------androidlistview分割線能夠改變顏色<ListViewandroid:id="@+id/android:list"android:layout_width="wrap_content"android:layout_height="wrap_content"android:divider="#FFCC00"android:dividerHeight="4px"/>---------------------------------------------------------------------------------------------保持屏幕喚醒狀態(tài)方法1:usePowerManagerandWakeLockAndroidManifest.xml權(quán)限:<uses-permissionandroid:name="android.permission.WAKE_LOCK"/>程序中代碼:PowerManagerpm=(PowerManager)getSystemService(Context.POWER_SERVICE);PowerManager.WakeLockmWakeLock=pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK,"MyTag");//inonResume()callmWakeLock.acquire();...//inonPause()callmWakeLock.release();方法2:usethewindowflagFLAG_KEEP_SCREEN_ON把下面代碼加入到程序onCreate方法中:@OverrideprotectedvoidonCreate(Bundleicicle){super.onCreate(icicle);//SetkeepscreenongetWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);}---------------------------------------------------------------------------------------------提醒用戶輸入有問(wèn)題直接上代碼Animationanimation=AnimationUtils.loadAnimation(this,R.anim.shake);EditTexteditText=(EditText)findViewById(R.id.edittext);editText.startAnimation(animation);里面面fromXDelta,toXDelta是表示x軸方向移動(dòng)距離shake.xml:<translatexmlns:android=""android:fromXDelta="0"android:toXDelta="30"android:duration="1000"android:interpolator="@anim/cycle_interpolator"/>cycleInterpolator.xml文件:<cycleInterpolatorxmlns:android=""android:cycles="3"/>抖動(dòng)3次---------------------------------------------------------------------------------------------TextView動(dòng)態(tài)設(shè)置背景色et.setBackgroundResource(R.color.white);---------------------------------------------------------------------------------------------預(yù)防ListView滾動(dòng)時(shí)候背景閃爍,需加上android:cacheColorHint="#00000000"假如設(shè)置了listviewandroid:background="@drawable/background"就要加上android:cacheColorHint="#00000000"---------------------------------------------------------------------------------------------經(jīng)過(guò)添加一個(gè)id為android:emptyTextView,當(dāng)ListView里面沒(méi)有data時(shí)候,就會(huì)顯示TextView內(nèi)容。<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android=""android:layout_width="wrap_content"android:layout_height="wrap_content"><ListViewandroid:id="@id/android:list"android:layout_width="fill_parent"android:layout_height="fill_parent"/><TextViewandroid:id="@id/android:empty"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="對(duì)不起,沒(méi)有數(shù)據(jù)顯示"/></LinearLayout>需要注意是,ListViewid,使用是"@id/android:list",即系統(tǒng)默認(rèn)id,而緊接其后TextView,使用id也是系統(tǒng)默認(rèn),即"@id/android:empty"。---------------------------------------------------------------------------------------------自定義控件單行文本跑馬燈控件importandroid.content.Context;importandroid.graphics.Canvas;importandroid.graphics.Paint;importandroid.os.Parcel;importandroid.os.Parcelable;importandroid.util.AttributeSet;importandroid.view.Display;importandroid.view.View;importandroid.view.WindowManager;importandroid.view.View.OnClickListener;importandroid.widget.TextView;/***//****TODO單行文本跑馬燈控件**@authortianlu*@version1.0*CreateAt:-2-16下午09:35:03*/publicclassAutoScrollTextViewextendsTextViewimplementsOnClickListener{publicfinalstaticStringTAG=AutoScrollTextView.class.getSimpleName();privatefloattextLength=0f;//文本長(zhǎng)度privatefloatviewWidth=0f;privatefloatstep=0f;//文字橫坐標(biāo)privatefloaty=0f;//文字縱坐標(biāo)privatefloattemp_view_plus_text_length=0.0f;//用于計(jì)算暫時(shí)變量privatefloattemp_view_plus_two_text_length=0.0f;//用于計(jì)算暫時(shí)變量publicbooleanisStarting=false;//是否開(kāi)始滾動(dòng)privatePaintpaint=null;//繪圖樣式privateStringtext="";//文本內(nèi)容publicAutoScrollTextView(Contextcontext){super(context);initView();}publicAutoScrollTextView(Contextcontext,AttributeSetattrs){super(context,attrs);initView();}publicAutoScrollTextView(Contextcontext,AttributeSetattrs,intdefStyle){super(context,attrs,defStyle);initView();}/***//***初始化控件*/privatevoidinitView(){setOnClickListener(this);}/***//***文本初始化,每次更改文本內(nèi)容或者文本效果等之后都需要重新初始化一下*/publicvoidinit(WindowManagerwindowManager){paint=getPaint();text=getText().toString();textLength=paint.measureText(text);viewWidth=getWidth();if(viewWidth==0){if(windowManager!=null){Displaydisplay=windowManager.getDefaultDisplay();viewWidth=display.getWidth();}}step=textLength;temp_view_plus_text_length=viewWidth+textLength;temp_view_plus_two_text_length=viewWidth+textLength*2;y=getTextSize()+getPaddingTop();}@OverridepublicParcelableonSaveInstanceState(){ParcelablesuperState=super.onSaveInstanceState();SavedStatess=newSavedState(superState);ss.step=step;ss.isStarting=isStarting;returnss;}@OverridepublicvoidonRestoreInstanceState(Parcelablestate){if(!(stateinstanceofSavedState)){super.onRestoreInstanceState(state);return;}SavedStatess=(SavedState)state;super.onRestoreInstanceState(ss.getSuperState());step=ss.step;isStarting=ss.isStarting;}publicstaticclassSavedStateextendsBaseSavedState{publicbooleanisStarting=false;publicfloatstep=0.0f;SavedState(ParcelablesuperState){super(superState);}@OverridepublicvoidwriteToParcel(Parcelout,intflags){super.writeToParcel(out,flags);out.writeBooleanArray(newboolean[]{isStarting});out.writeFloat(step);}publicstaticfinalParcelable.Creator<SavedState>CREATOR=newParcelable.Creator<SavedState>(){publicSavedState[]newArray(intsize){returnnewSavedState[size];}@OverridepublicSavedStatecreateFromParcel(Parcelin){returnnewSavedState(in);}};privateSavedState(Parcelin){super(in);boolean[]b=null;in.readBooleanArray(b);if(b!=null&&b.length>0)isStarting=b[0];step=in.readFloat();}}/***//***開(kāi)始滾動(dòng)*/publicvoidstartScroll(){isStarting=true;invalidate();}/***//***停頓滾動(dòng)*/publicvoidstopScroll(){isStarting=false;invalidate();}@OverridepublicvoidonDraw(Canvascanvas){canvas.drawText(text,temp_view_plus_text_length-step,y,paint);if(!isStarting){return;}step+=0.5;if(step>temp_view_plus_two_text_length)step=textLength;invalidate();}@OverridepublicvoidonClick(Viewv){if(isStarting)stopScroll();elsestartScroll();}}實(shí)現(xiàn)了寬度判斷,文本自動(dòng)滾動(dòng)及開(kāi)始和停頓滾動(dòng)等功效。在UIxml中配置以下:<cn.tigertian.ui.AutoScrollTextViewandroid:id="@+id/TextViewNotice"android:layout_height="30px"android:layout_width="fill_parent"android:text="@string/test_notice_1"android:textColor="#000"android:inputType="text"android:background="#EEE"android:textSize="20px"></cn.tigertian.ui.AutoScrollTextView>在Activity中使用方法以下://開(kāi)啟公告滾動(dòng)條autoScrollTextView=(AutoScrollTextView)findViewById(R.id.TextViewNotice);autoScrollTextView.init(getWindowManager());autoScrollTextView.startScroll();假如想改變跑馬燈文字內(nèi)容或者文字效果,則在調(diào)用完setText方法之后,需要再調(diào)用一下init(WindowManagerwindowManager)方法,重新進(jìn)行初始化和相關(guān)參數(shù)計(jì)算。---------------------------------------------------------------------------------------------大約意思是。@和?都是用來(lái)引用其余已經(jīng)定義好資源。@引用是之前定義好資源當(dāng)前項(xiàng)目或者androidframework里。而?則是引用當(dāng)前加載樣式文件里。意思就是說(shuō)你在xml里某行定義了一個(gè)資源,在下面某行需要引用這個(gè)資源時(shí)用?即可---------------------------------------------------------------------------------------------TabHost兩種實(shí)現(xiàn)方式文章分類(lèi):Java編程第一個(gè):繼承TabActivity,從TabActivity中用getTabHost()方法獲取TabHost。只要定義詳細(xì)Tab內(nèi)容布局就行了.xml布局:Java代碼<FrameLayoutxmlns:android=""android:layout_width="fill_parent"android:layout_height="fill_parent"><LinearLayoutandroid:id="@+id/widget_layout_Blue"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><EditTextandroid:id="@+id/widget34"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="EditText"android:textSize="18sp"></EditText><Buttonandroid:id="@+id/widget30"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button"></Button></LinearLayout><LinearLayoutandroid:id="@+id/widget_layout_red"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><AnalogClockandroid:id="@+id/widget36"android:layout_width="wrap_content"android:layout_height="wrap_content"></AnalogClock></LinearLayout><LinearLayoutandroid:id="@+id/widget_layout_green"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><RadioGroupandroid:id="@+id/widget43"android:layout_width="166px"android:layout_height="98px"android:orientation="vertical"><RadioButtonandroid:id="@+id/widget44"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="RadioButton"></RadioButton><RadioButtonandroid:id="@+id/widget45"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="RadioButton"></RadioButton></RadioGroup></LinearLayout></FrameLayout>java代碼:super.onCreate(savedInstanceState);myTabhost=this.getTabHost();//getTabhostLayoutInflater.from(this).inflate(R.layout.main,myTabhost.getTabContentView(),true);myTabhost.setBackgroundColor(Color.argb(150,22,70,150));myTabhost.addTab(myTabhost.newTabSpec("One")//makeanewTab.setIndicator("A")//settheTitleandIcon.setContent(R.id.widget_layout_Blue));//setthelayoutmyTabhost.addTab(myTabhost.newTabSpec("Two")//makeanewTab.setIndicator("B",getResources().getDrawable(R.drawable.mumule))//settheTitleandIcon.setContent(R.id.widget_layout_green));//setthelayoutmyTabhost.addTab(myTabhost.newTabSpec("Three")//makeanewTab.setIndicator("C",getResources().getDrawable(R.drawable.notepad))//settheTitleandIcon.setContent(R.id.widget_layout_red));<FrameLayoutxmlns:android="" android:layout_width="fill_parent"android:layout_height="fill_parent"> <LinearLayoutandroid:id="@+id/widget_layout_Blue" android:layout_width="fill_parent"android:layout_height="fill_parent" android:orientation="vertical"> <EditTextandroid:id="@+id/widget34"android:layout_width="fill_parent" android:layout_height="wrap_content"android:text="EditText" android:textSize="18sp"> </EditText> <Buttonandroid:id="@+id/widget30"android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="Button"> </Button> </LinearLayout> <LinearLayoutandroid:id="@+id/widget_layout_red" android:layout_width="fill_parent"android:layout_height="fill_parent" android:orientation="vertical"> <AnalogClockandroid:id="@+id/widget36" android:layout_width="wrap_content"android:layout_height="wrap_content"> </AnalogClock> </LinearLayout> <LinearLayoutandroid:id="@+id/widget_layout_green" android:layout_width="fill_parent"android:layout_height="fill_parent" android:orientation="vertical"> <RadioGroupandroid:id="@+id/widget43" android:layout_width="166px"android:layout_height="98px" android:orientation="vertical"> <RadioButtonandroid:id="@+id/widget44" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="RadioButton"> </RadioButton> <RadioButtonandroid:id="@+id/widget45" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="RadioButton"> </RadioButton> </RadioGroup> </LinearLayout></FrameLayout>java代碼:super.onCreate(savedInstanceState); myTabhost=this.getTabHost(); //getTabhost LayoutInflater.from(this).inflate(R.layout.main,myTabhost.getTabContentView(),true); myTabhost.setBackgroundColor(Color.argb(150,22,70,150)); myTabhost .addTab(myTabhost.newTabSpec("One")//makeanewTab .setIndicator("A") //settheTitleandIcon .setContent(R.id.widget_layout_Blue)); //setthelayout myTabhost .addTab(myTabhost.newTabSpec("Two")//makeanewTab .setIndicator("B", getResources().getDrawable(R.drawable.mumule)) //settheTitleandIcon .setContent(R.id.widget_layout_green)); //setthelayout myTabhost .addTab(myTabhost.newTabSpec("Three")//makeanewTab .setIndicator("C", getResources().getDrawable(R.drawable.notepad)) //settheTitleandIcon .setContent(R.id.widget_layout_red));第二種:不用繼承TabActivity,在布局文件中定義TabHost即可,不過(guò)TabWidgetid必須是@android:id/tabs,F(xiàn)rameLayoutid必須是@android:id/tabcontent。TabHostid能夠自定義.xml布局:Java代碼<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android=""android:id="@+id/hometabs"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TabHostandroid:id="@+id/tabhost"android:layout_width="wrap_content"android:layout_height="wrap_content"><LinearLayoutandroid:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TabWidgetandroid:id="@android:id/tabs"android:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"></TabWidget><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="wrap_content"android:layout_height="wrap_content"><TextViewandroid:id="@+id/view1"android:layout_width="fill_parent"android:layout_height="fill_parent"/><TextViewandroid:id="@+id/view2"android:layout_width="fill_parent"android:layout_height="fill_parent"/><TextViewandroid:id="@+id/view3"android:layout_width="fill_parent"android:layout_height="fill_parent"/></FrameLayout></LinearLayout></TabHost></LinearLayout>java代碼:protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.hometabs);TabHosttabHost=(TabHost)findViewById(R.id.tabhost);tabHost.setup();TabWidgettabWidget=tabHost.getTabWidget();tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1",getResources().getDrawable(R.drawable.mumule)).setContent(R.id.view1));tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3").setContent(R.id.view3));tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2").setContent(R.id.view2));finalinttabs=tabWidget.getChildCount();Log.i(TAG,"***tabWidget.getChildCount():"+tabs);finalinttabWidth=90;finalinttabHeight=45;for(inti=0;i<tabs;i++){/*finalViewview=tabWidget.getChildAt(i);view.getLayoutParams().width=tabWidth;view.getLayoutParams().height=tabHeight;finalTextViewtv=(TextView)view.findViewById(android.R.id.title);tv.setTextColor(this.getResources().getColorStateList(android.R.color.black));MarginLayoutParamstvMLP=(MarginLayoutParams)tv.getLayoutParams();tvMLP.bottomMargin=8;*/}}---------------------------------------------------------------------------------------------TextView顯示圖片和文字TextViewtext=(

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論