[Android]介面控制項

來源:互聯網
上載者:User

標籤:android   c   style   class   code   java   

 

1. 引用系統內建樣式字型大小

對於能夠顯示文字的控制項(如TextView EditText RadioButton Button CheckBox Chronometer等等),你有時需要控制字型的大小。Android平台定義了三種字型大小。

 "?android:attr/textAppearanceLarge" "?android:attr/textAppearanceMedium" "?android:attr/textAppearanceSmall" 


使用方法為:

 android:textAppearance="?android:attr/textAppearanceLarge" android:textAppearance="?android:attr/textAppearanceMedium" android:textAppearance="?android:attr/textAppearanceSmall" 


 style="?android:attr/textAppearanceLarge" style="?android:attr/textAppearanceMedium" style="?android:attr/textAppearanceSmall" 

 

字型顏色
 android:textColor="?android:attr/textColorPrimary" android:textColor="?android:attr/textColorSecondary" android:textColor="?android:attr/textColorTertiary" android:textColor="?android:attr/textColorPrimaryInverse" android:textColor="?android:attr/textColorSecondaryInverse" 

 

ProgressBar
 style="?android:attr/progressBarStyleHorizontal" style="?android:attr/progressBarStyleLarge" style="?android:attr/progressBarStyleSmall" style="?android:attr/progressBarStyleSmallTitle" 

 

分隔字元

橫向:

 <View android:layout_width="fill_parent" android:layout_height="1dip" android:background="?android:attr/listDivider" /> 


縱向:

 <View android:layout_width="1dip" android:layout_height="fill_parent" android:background="?android:attr/listDivider" /> 

 

CheckBox
 style="?android:attr/starStyle" 

 

類似標題列效果的TextView
 style="?android:attr/listSeparatorTextViewStyle" 

 

其它有用的樣式
 android:layout_height="?android:attr/listPreferredItemHeight" android:paddingRight="?android:attr/scrollbarSize" style="?android:attr/windowTitleBackgroundStyle" style="?android:attr/windowTitleStyle" android:layout_height="?android:attr/windowTitleSize" android:background="?android:attr/windowBackground" 


更多細節可參考SDK文檔的R.attr類。

2.ImageView.scaleType屬性

ImageView的屬性android:scaleType,即 ImageView.setScaleType(ImageView.ScaleType)。android:scaleType是控製圖片如何 resized/moved來匹對ImageView的size。ImageView.ScaleType /
android:scaleType值的意義區別:
CENTER /center 按圖片的原來size置中顯示,當圖片長/寬超過View的長/寬,則截取圖片的置中部分顯示
CENTER_CROP / centerCrop 按比例擴大圖片的size置中顯示,使得圖片長 (寬)等於或大於View的長(寬)
CENTER_INSIDE / centerInside 將圖片的內容完整置中顯示,通過按比例縮小或原來的size使得圖片長/寬等於或小於View的長/寬
FIT_CENTER / fitCenter 把圖片按比例擴大/縮小到View的寬度,置中顯示
FIT_END / fitEnd 把圖片按比例擴大/縮小到View的寬度,顯示在View的下部分位置
FIT_START / fitStart 把圖片按比例擴大/縮小到View的寬度,顯示在View的上部分位置
FIT_XY / fitXY 把圖片 不按比例 擴大/縮小到View的大小顯示
MATRIX / matrix 用矩陣來繪製

3.screenOrientation橫豎屏設定(在XML設定檔中對應Activity設定)

注意:android只支援270度旋轉。

 <activity android:name=".MyActivity" android:label="@string/app_name" android:screenOrientation="portrait"> 


android:screenOrientation設定該活動的方向, 該值可以是任何一個下面的字串:

"unspecified"
- 預設值. 由系統選擇顯示方向. 在不同的裝置可能會有所不同.

"landscape"
- 橫向

"portrait"
- 縱向

"user"
- 用戶當前的首選方向

"behind"
- 與在活動堆棧下的活動相同方向

"sensor"
- 根據物理方向感應器確定方向. 取決於用戶手持的方向, 當用戶轉動裝置, 它跟隨改變.

"nosensor"
- 不經物理方向感應器確定方向. 該感應器被忽略, 所以當用戶轉動裝置, 顯示不會跟隨改變. 除了這個區別,系統選擇使用相同的政策取向對於“未指定”設定. 系統根據“未指定”("unspecified")設定選擇相同顯示方向.

3.1 橫豎屏切換代碼

有的程式適合從豎屏切換到橫屏,或者反過來,這個時候怎麼辦呢?可以在配置Activity的地方進行如下的配置android:screenOrientation="portrait"。這樣就可以保證是豎屏總是豎屏了,或者landscape橫向。 而有的程式是適合橫豎屏切換的。如何處理呢?首先要在配置Activity的時候進行如下的配置:android:configChanges="keyboardHidden|orientation",另外需要重寫Activity的onConfigurationChanged方法。實現方式如下,不需要做太多的內容:

// 轉屏時,記住當前的配置,以至於每次轉屏時不會重新啟動新的Activity,會繼續顯示當前的視頻

 @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){  // landscape do nothing is ok  }else if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){  // portrait do nothing is ok } }

 

4.布局分布

一:不同的layout Android手機螢幕大小不一,有480x320, 640x360, 800x480.怎樣才能讓App自動適應不同的螢幕呢? 其實很簡單,只需要在res目錄下建立不同的layout檔案夾,比如layout-640x360,layout-800x480,所有的layout檔案在編譯之後都會寫入R.Java裡,而系統會根據螢幕的大小自己選擇合適的layout進行使用。

二:hdpi、mdpi、ldpi 在之前的版本中,只有一個drawable,而2.1版本中有drawable-mdpi、drawable-ldpi、drawable-hdpi三個,這三個主要是為了支援多解析度。 drawable- hdpi、drawable- mdpi、drawable-ldpi的區別: (1)drawable-hdpi裡面存放高解析度的圖片,如WVGA (480x800),FWVGA (480x854) (2)drawable-mdpi裡面存放中等解析度的圖片,如HVGA (320x480) (3)drawable-ldpi裡面存放低解析度的圖片,如QVGA (240x320) 系統會根據機器的解析度來分別到這幾個檔案夾裡面去找對應的圖片。

5. 播放視頻時,不會變暗

Android
禁止系統休眠,使螢幕不變暗,保持遊戲中螢幕高亮實現這一功能的方法有兩種,一種是在Manifest.xml檔案裡面聲明,一種是在代碼裡面修改LayoutParams的標誌位。具體如下:

1、在Manifest.xml檔案裡面用user-permission聲明。代碼如下:

   <uses-permission android:name="android.permission.WAKE_LOCK"> </uses-permission> 


這種方法,在安裝apk時,系統會提示安裝人是否允許使用禁止休眠功能。

2、在程式中用代碼實現。代碼如下:

 getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 


把這段代碼加在setContentView(R.layout.main)之前即可
這種方法,安裝時,不會提示安裝人是否允許使用禁止休眠功能

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.