XML布局技巧

來源:互聯網
上載者:User

   postInvalidate();                            介面重新整理
 

一個list元素可能預設為48px
android:visibility="invisible">                  設定組件顯示visible 不顯示invisible 消失gone
android:background="@null"                       設定組件無背景
android:layout_gravity="right|center_vertical"   向右並垂直置中
  <Button
   
     android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableTop="@drawable/star"         將圖片設定在按鈕的上方
    android:text="按鈕1"                            
    />
      
 
  <include
            android:id="@+id/vip_include_top"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            layout="@layout/top_1" />                  包含一個layout
 <SlidingDrawer
android:id="@+id/sliding"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:content="@+id/allApps"
android:handle="@+id/imageViewIcon" a
ndroid:orientation="vertical"
>                                                抽屜式設計必須設定content(類容控制項)    handle(把手類容控制項)
android:layout_alignParentBottom="true"          總是在父元素的底部
android:scrollbars="none"                        不要捲軸
android:gravity:       是對view控制項本身來說的,是用來設定view本身的文本應該顯示在view的什麼位置,預設值是左側

android:layout_gravity:是相對於包含改元素的父元素來說的,設定該元素在父元素的什麼位置
<Button android:layout_width="120dip"
   android:layout_height="wrap_content"
   android:text="切換程式"
   style="@android:style/ButtonBar"  設定按鈕為按鈕條風格
   android:id="@+id/btn7"
   android:layout_weight="1" />        
 

requestWindowFeature(Window.FEATURE_NO_TITLE);   設定無標題
在application 標籤中添加android:theme=”@android:style/Theme.NoTitleBar” 去掉所有Activity介面的標題列
修改AndroidManifest.xml
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);       設定全屏
<resources>
 <style name="tab_btn"> 
     <item name="android:textAppearance">@style/tab_item_text_style</item>  
     <item name="android:gravity">center_horizontal</item>
     <item name="android:background">@drawable/trans</item>
     <item name="android:layout_width">0.0dip</item> 
     <item name="android:layout_height">wrap_content</item> 
     <item name="android:button">@null</item> 
     <item name="android:layout_weight">1.0</item>   自訂按鈕的Style屬性
 </style>
</resources>

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_enabled="false" android:drawable="@drawable/input_normal" />
  <item android:state_pressed="true" android:drawable="@drawable/input_normal" />
  <item android:state_focused="true" android:drawable="@drawable/input_over" />
  </selector>                                              定義組件得到焦點,點擊,啟用的狀態

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">  
<gradient     
android:startColor="#FFFFFF"     
android:endColor="#000000"     
android:angle="270" />  </shape>                             設定Drawable漸層
setBackgroundResource(R.drawable.bg);                        調用       

   //設定 Gallery樣式在BaseAdapter的getView中

TypedArray typedArray = context.obtainStyledAttributes(R.styleable.Gallery);
imageView.setBackgroundResource(typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0));
return imageView;
 android:spacing="10dp"  //Gallery間隔
 
第一類:屬性值為true或false
 

android:layout_centerHrizontal? 水平置中
android:layout_centerVertical?? 垂直置中
android:layout_centerInparent???相對於父元素完全置中
android:layout_alignParentBottom 貼緊父元素的下邊緣
android:layout_alignParentLeft?? 貼緊父元素的左邊緣
android:layout_alignParentRight? 貼緊父元素的右邊緣
android:layout_alignParentTop??? 貼緊父元素的上邊緣
android:layout_alignWithParentIfMissing? 如果對應的兄弟元素找不到的話就以父元素做參照物
第二類:屬性值必須為id的引用名”
android:layout_below????? 在某元素的下方
android:layout_above????? 在某元素的的上方
android:layout_toLeftOf?? 在某元素的左邊
android:layout_toRightOf? 在某元素的右邊
android:layout_alignTop?? 本元素的上邊緣和某元素的的上邊緣對齊
android:layout_alignLeft? 本元素的左邊緣和某元素的的左邊緣對齊
android:layout_alignBottom 本元素的下邊緣和某元素的的下邊緣對齊
android:layout_alignRight? 本元素的右邊緣和某元素的的右邊緣對齊
第三類:屬性值為具體的像素值,如30dip,40px
android:layout_marginBottom?????? 離某元素底邊緣的距離
android:layout_marginLeft???????? 離某元素左邊緣的距離
android:layout_marginRight??????? 離某元素右邊緣的距離
android:layout_marginTop????????? 離某元素上邊緣的距離
 
android: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 用矩陣來繪製,動態縮小放大圖片來顯示。
** 要注意一點,Drawable檔案夾裡面的圖片命名是不能大寫的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.