閱讀616 返回首頁    go 技術社區[雲棲]


手機衛士04-自定義顯示圖片

首先,先和大家說個對不起先,昨天太趕啦,一時沒說清楚昨天的內容,其實昨天還有一些UI的知識點還沒講的,而且那個代碼也是有一點問題的,所以今天把它補回來

我們先講一下,昨天忘記說了的內容


其實昨天忘記說了的就是那個自定義標題欄那裏啦

自定義標題欄,其實就是把原來的標題欄隱藏掉,然後再自己寫一個TextView這些的控件,把它放上去的而已,一說就很簡單的啦

但我們這裏有一個知識點的

那就是自定義圖片,其實上麵的那個手機防盜這些文字外麵的那個框,就是用到啦自定義圖片的啦

要自定義圖片,也很簡單,我們要先在drawable的目錄下麵,新建一個xml文件

下麵是我自己建的那個文字的背景text_background.xml

  1. <font color="#333333"><font face="Arial"><?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:andro
  3.     android:shape="rectangle" ><!-- 這裏指定了我現在要畫的是一個矩形 -->

  4.     <stroke
  5.         android:width="1dip"
  6.         android:color="#ff333333" /><!-- 這個就是一個畫筆啦,現在指定了畫筆的大小,還有顏色 -->

  7.     <corners android:radius="12dip" /><!-- 現在這裏指定我這個矩形是圓角的,12是那個圓角的值 -->

  8.     <solid android:color="@color/background" /><!-- 邊框顏色 -->

  9.     <padding
  10.         android:bottom="2dip"
  11.         android:left="8dip"
  12.         android:right="8dip"
  13.         android:top="2dip" /><!-- 這個是內距 -->

  14. </shape></font></font>
複製代碼
接下來,就可以在我們的main_item,xml的布局文件裏麵引用它啦
  1. <font color="#333333"><font face="Arial"><?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:andro
  3.     android:layout_width="150dip"
  4.     android:layout_height="150dip"
  5.     android:gravity="center_horizontal"
  6.     android:orientation="vertical" >
  7.    
  8.     <ImageView
  9.         android:
  10.         android:layout_width="120dip"
  11.         android:layout_height="120dip"
  12.         android:scaleType="fitXY"
  13.         android:src="@drawable/app"
  14.         android:contentDescription="@string/hello_world"/>
  15.    
  16.     <TextView
  17.         android:
  18.         android:layout_width="wrap_content"
  19.         android:layout_height="wrap_content"
  20.         android:layout_marginTop="5dip"
  21.         android:textSize="18sp"
  22.         android:textColor="@android:color/black"
  23.         android:text="@string/main"
  24.         android:background="@drawable/text_background"/><!-- 引用剛剛定義的圖片,像一般圖片一樣使用就行的啦,很方便的  -->

  25. </LinearLayout>
  26. </font></font>
複製代碼
好,說完自定義圖片之後,我說一下那個自定義標題欄那裏啦,我剛剛說過啦,就是隱藏原來的,那麼如何隱藏呢,其實隻要在AndroidMainfest文件裏麵,聲明那個類的時候給它加上一個Theme屬性就行的啦
  1. <activity
  2.             android:theme="@android:style/Theme.NoTitleBar"
  3.             android:label="@string/main"
  4.             android:name="com.xiaobin.security.ui.MainActivity" />
複製代碼
然後再給我們的布局文件加上一個TextView
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:andro
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="@android:color/white"
  6.     android:orientation="vertical" >

  7.     <LinearLayout
  8.         android:layout_width="match_parent"
  9.         android:layout_height="40dip"
  10.         android:background="@drawable/title_background"
  11.         android:gravity="center_vertical|center_horizontal"
  12.         android:orientation="vertical" >

  13.         <TextView
  14.             android:layout_width="wrap_content"
  15.             android:layout_height="wrap_content"
  16.             android:text="@string/main"
  17.             android:textColor="@android:color/white"
  18.             android:textSize="22sp" />
  19.     </LinearLayout>

  20.     <GridView
  21.         android:
  22.         android:layout_width="match_parent"
  23.         android:layout_height="match_parent"
  24.         android:numColumns="2"
  25.         android:verticalSpacing="8dip" />

  26. </LinearLayout>
複製代碼
其實在那個背景裏麵,我也是用到了自定義圖片的,各位也可以用一下的,多點用,才能記得嘛 好啦,現在說一下那個最重要的啦,就是我是在那裏找到這些的,是怎樣知道這些東西的其實這些,在我們的android的文檔裏麵有的 就是這樣的啦,其實api文檔還有很多東西的,有空的話可以看看,當然我們這個項目還是會參與很多api裏麵的東西的好啦,今天就到這裏啦,各位可以自己玩一玩那個自定義圖片的功能,先預告一下明天的內容,明天次會都我們自定義對話框的,還有給手機防盜加個登錄密碼  Security_04自定義圖片.rar(139.29 KB, 下載次數: 250)

最後更新:2017-04-03 14:54:32

  上一篇:go eclipse啟動失敗的是java vm失敗解決方法
  下一篇:go Java類集--Map接口、HashMap、IdentityHashMap、SortedMap