app:
與Android:
有什麼區別在上面的文件結構中,res目錄、src目錄、AndroidManifest.xml文件是Android項目必需的。其餘目錄、文件都是可選的。html
一、自動生成的R.java:java
/* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */ public final class R { public static final class attr { } public static final class drawable { public static final int ic\_launcher=0x7f020000; } public static final class id { public static final int show=0x7f050000; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int app_name=0x7f040000; } }
R.java文件是由AAPT工具根據應用中的資源文件自動生成的,所以能夠把R.java理解成Android應用的資源字典。android
二、Android應用的清單文件AndroidManifest.xml:正則表達式
<?xml version="1.0" encoding="utf-8"?> <!\--指定該Android應用的包名,該包名可用於惟一地標識該應用--> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.diweijia.HelloWorld" android:versionCode="1" android:versionName="1.0"> <!\--指定Android應用標籤、圖標--> <application android:label="@string/app_name" android:icon="@drawable/ic_launcher"> <!\--定義Android應用的一個組件:Activity,該Activity的類爲HelloWorld,並指定該Activity的標籤--> <activity android:name="HelloWorld" android:label="@string/app_name"> <intent-filter> <!\--指定該Activity是程序的入口--> <action android:name="android.intent.action.MAIN" /> <!\--指定加載該應用時運行該Activity--> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
三、應用程序權限說明:算法
<!\--聲明該應用自己須要的權限(如發短信)--> <uses-permission android:name="android.permission.SEND_SMS">
權限 | 說明 |
---|---|
ACCESS_NETWORK_STATE | 容許應用程序獲取網絡狀態信息的權限 |
ACCESS_WIFI_STATE | 容許應用程序獲取WI-FI網絡狀態信息的權限 |
BATTERY_STATES | 容許應用程序獲取電池狀態信息的權限 |
BLUETOOTH | 容許應用程序連接匹配的藍牙設備的權限 |
BLUETOOTH_ADMIN | 容許應用程序發現匹配的藍牙設備的權限 |
BROADCAST_SMS | 容許應用程序廣播收到短信提醒的權限 |
CALL_PHONE | 容許應用程序撥打電話的權限 |
CAMERA | 容許應用程序使用照相機的權限 |
CHANGE_NETWORK_STATE | 容許應用程序改變網絡鏈接狀態的權限 |
CHANGE_WIFI_STATE | 容許應用程序改變WIFI網絡鏈接狀態的權限 |
DELETE_CACHE_FILES | 容許應用程序刪除緩存文件的權限 |
DELETE_PACKAGES | 容許應用程序刪除安裝包的權限 |
FLASHLIGHT | 容許應用程序訪問閃光燈的權限 |
INTERNET | 容許應用程序打開網絡Socket的權限 |
MODIFY_AUDIO_SETTINGS | 容許應用程序修改全局聲音設置的權限 |
PROCESS_OUTGOING_CALLS | 容許應用程序監聽、控制、取消呼出電話的權限 |
READ_CONTACTS | 容許應用程序讀取用戶的聯繫人數據的權限 |
READ_HISTORY_BOOKMARKS | 容許應用程序讀取歷史書籤的權限 |
READ_OWNER_DATA | 容許應用程序讀取用戶數據的權限 |
READ_PHONE_STATE | 容許應用程序讀取電話狀態的權限 |
READ_PHONE_SMS | 容許應用程序讀取短信的權限 |
REBOOT | 容許應用程序重啓系統的權限 |
RECEIVE_MMS | 容許應用程序接受、監控、處理彩信的權限 |
RECEIVE_SMS | 容許應用程序接受、監控、處理短信的權限 |
RECORD_AUDIO | 容許應用程序錄音的權限 |
SEND_SMS | 容許應用程序發送短信的權限 |
SET_ORIENTATION | 容許應用程序旋轉屏幕的權限 |
SET_TIME | 容許應用程序設置時間的權限 |
SET_TIME_ZONE | 容許應用程序設置時區的權限 |
SET_WALLPAPER | 容許應用程序設置桌面壁紙的權限 |
VIBRATE | 容許應用程序控制震動器的權限 |
WRITE_CONTACTS | 容許應用程序寫入用戶聯繫人的權限 |
WRITE_HISTORY_BOOKMARKS | 容許應用程序寫入歷史書籤的權限 |
WRITE_OWNER_DATA | 容許應用程序寫入用戶數據的權限 |
WRITE_SMS | 容許應用程序寫短信的權限 |
爲了更好地管理Android應用的用戶界面裏的各組件,Android提供了佈局管理器。經過使用佈局管理器,Android應用的圖形用戶界面具備良好的平臺無關性。與Swing界面編程不一樣的是,Android的佈局管理器自己就是一個UI組件,全部的佈局管理器都是ViewGroup的子類。下面將介紹以ViewGroup爲基類派生的佈局管理器。數據庫
一、線性佈局(LinearLayout):編程
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity= "bottom|center_horizontal"> <Button android:id="@+id/bn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bn1"/> <Button android:id="@+id/bn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bn2"/> </LinearLayout>
獲得的界面以下:數組
若是將android:gravity的屬性"bottom|center_horizontal"
(底部,居中)改成right|center_vertical
(水平右對齊,垂直居中),則獲得如下界面:緩存
二、表格佈局(TableLayout):網絡
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 定義第一個表格佈局,指定第2列容許收縮,第3列容許拉伸 --> <TableLayout android:id="@+id/TableLayout01" android:layout_width="match_parent" android:layout_height="wrap_content" android:shrinkColumns="1" android:stretchColumns="2"> <!-- 直接添加按鈕,它本身會佔一行 --> <Button android:id="@+id/ok1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="獨自一行的按鈕"/> <!-- 添加一個表格行 --> <TableRow> <!-- 爲該表格行添加三個按鈕 --> <Button android:id="@+id/ok2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按鈕"/> <Button android:id="@+id/ok3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="收縮的按鈕"/> <Button android:id="@+id/ok4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拉伸的按鈕"/> </TableRow> </TableLayout> <!-- 定義第2個表格佈局 ,指定第2列隱藏--> <TableLayout android:id="@+id/TableLayout02" android:layout_width="match_parent" android:layout_height="wrap_content" android:collapseColumns="1"> <!-- 直接添加按鈕,它本身會佔一行 --> <Button android:id="@+id/ok5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="獨自一行的按鈕"/> <!-- 添加一個表格行 --> <TableRow> <!-- 爲該表格行添加三個按鈕 --> <Button android:id="@+id/ok6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按鈕1"/> <Button android:id="@+id/ok7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按鈕2"/> <Button android:id="@+id/ok8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按鈕3"/> </TableRow> </TableLayout> <!-- 定義第3個表格佈局,指定第2列和第3列能夠被拉伸--> <TableLayout android:id="@+id/TableLayout03" android:layout_width="match_parent" android:layout_height="wrap_content" android:stretchColumns="1,2"> <!-- 直接添加按鈕,它本身會佔一行 --> <Button android:id="@+id/ok9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="獨自一行的按鈕" /> <!--定義一個表格行--> <TableRow> <!-- 爲該表格行添加三個按鈕 --> <Button android:id="@+id/ok10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按鈕" /> <Button android:id="@+id/ok11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拉伸的按鈕" /> <Button android:id="@+id/ok12" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拉伸的按鈕" /> </TableRow> <!--定義一個表格行--> <TableRow> <!-- 爲該表格行添加兩個按鈕 --> <Button android:id="@+id/ok13" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按鈕" /> <Button android:id="@+id/ok14" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拉伸的按鈕" /> </TableRow> </TableLayout> </LinearLayout>
使用Activity顯示上面的佈局,將會看到以下界面:
三、幀佈局(FrameLayout):
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 依次定義6個TextView,先定義的TextView位於底層 後定義的TextView位於上層 --> <TextView android:id="@+id/view03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="120pt" android:height="120pt" android:background="#00f"/> <TextView android:id="@+id/view04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="100pt" android:height="100pt" android:background="#ff0"/> <TextView android:id="@+id/view05" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="80pt" android:height="80pt" android:background="#f0f"/> <TextView android:id="@+id/view06" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="60pt" android:height="60pt" android:background="#0ff"/> </FrameLayout>
使用Activity顯示上面的佈局,將會看到以下界面:
四、相對佈局(RelativeLayout):
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 定義該組件位於父容器中間 --> <TextView android:id="@+id/view01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/leaf" android:layout_centerInParent="true"/> <!-- 定義該組件位於view01組件的上方 --> <TextView android:id="@+id/view02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/leaf" android:layout_above="@id/view01" android:layout_alignLeft="@id/view01"/> <!-- 定義該組件位於view01組件的下方 --> <TextView android:id="@+id/view03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/leaf" android:layout_below="@id/view01" android:layout_alignLeft="@id/view01"/> <!-- 定義該組件位於view01組件的左邊 --> <TextView android:id="@+id/view04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/leaf" android:layout_toLeftOf="@id/view01" android:layout_alignTop="@id/view01"/> <!-- 定義該組件位於view01組件的右邊 --> <TextView android:id="@+id/view05" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/leaf" android:layout_toRightOf="@id/view01" android:layout_alignTop="@id/view01"/> </RelativeLayout>
上面程序中先將第一個組件放在父容器中央,再將接下來定義的4個組件依次環繞在第一個組件周圍。使用Activity顯示上面的佈局,將會看到以下界面:
五、網格佈局(GridLayout):
<?xml version="1.0" encoding="utf-8" ?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:rowCount="6" android:columnCount="4" android:id="@+id/root"> <!-- 定義一個橫跨4列的文本框, 並設置該文本框的前景色、背景色等屬性 --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_columnSpan="4" android:textSize="50sp" android:layout_marginLeft="2pt" android:layout_marginRight="2pt" android:padding="3pt" android:layout_gravity="right" android:background="#eee" android:textColor="#000" android:text="0"/> <!-- 定義一個橫跨4列的按鈕 --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_columnSpan="4" android:text="清除"/> </GridLayout>
public class MainActivity extends Activity { GridLayout gridLayout; // 定義16個按鈕的文本 String[] chars = new String[] { "7" , "8" , "9" , "÷", "4" , "5" , "6" , "×", "1" , "2" , "3" , "-", "." , "0" , "=" , "+" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gridLayout = (GridLayout) findViewById(R.id.root); for(int i = 0 ; i < chars.length ; i++) { Button bn = new Button(this); bn.setText(chars[i]); // 設置該按鈕的字號大小 bn.setTextSize(40); // 設置按鈕四周的空白區域 bn.setPadding(5 , 35 , 5 , 35); // 指定該組件所在的行 GridLayout.Spec rowSpec = GridLayout.spec(i / 4 + 2); // 指定該組件所在的列 GridLayout.Spec columnSpec = GridLayout.spec(i % 4); GridLayout.LayoutParams params = new GridLayout.LayoutParams( rowSpec , columnSpec); // 指定該組件佔滿父容器 params.setGravity(Gravity.FILL); gridLayout.addView(bn , params); } } }
添加按鈕時指定了每一個按鈕所在的行號、列號,並指定這些按鈕將會自動填充單元格全部空間。運行程序,獲得如下界面:
六、絕對佈局(AbsoluteLayout):
『問題一』Android官方API文檔的獲取和使用
對於每個語言學習者而言,官方文檔是必看的。就像以前學習Java同樣,學習過程基本是以查詢API爲基礎。如何查看官方文檔是一種能力,我認爲這比單單記住幾個知識點更重要。
『問題二』如何設置Android中的顏色
在android中常常看到設置的顏色爲八位的十六進制的顏色值:
public static final class color { public static final int lightblue=0x7f040000; }
0xffff00ff是int類型的數據,分組一下0x|ff|ff00ff,0x表示顏色整數的標記,ff表示透明度,f00f表示色值,注意:0x後面ffff00ff必須是8位的顏色表示。
顏色和不透明度 (alpha) 值以十六進制表示法表示。任何一種顏色的值範圍都是 0到 255(00到 ff)。
對於 alpha,00表示徹底透明,ff表示徹底不透明。
表達式順序是「aabbggrr」,其中「aa=alpha」(00到ff);「bb=blue」(00到ff);「gg=green」(00到ff);「rr=red」(00到ff)。
tx.setTextColor(android.graphics.Color.RED);
tx.setTextColor(0xffff00f);
android:textColor="#F8F8FF00"
<?xml version="1.0" encoding="utf-8"?> <resources> <drawable name="dkgray">#80808FF0</drawable> <drawable name="yello">#F8F8FF00</drawable> <drawable name="white">#FFFFFF</drawable> <drawable name="darkgray">#938192</drawable> ...... </resources>
『問題三』Android Studio如何進行調試
Android Studio提供的調試版面以下:
因爲Android Studio是基於IntelliJ IDEA的一款開放工具,因此其調試方法與IDEA基本相同,能夠參考IDEA調試方法:使用IDEA編輯、編譯、運行、調試Java程序
Android開發的學習是我認爲很是必要且有意義的,不管作項目仍是參加競賽都能派上用場。正好趁暑假這個機會學習相關內容,但願能與個人隊友們自主開發出來一個(有趣的)APP。以前在一次Java實驗中接觸了Android開發的一點皮毛,現在開始系統地學習,對Android也不斷有了新的認識,當時困擾個人不少難題也就迎刃而解了。計算機實習期間因爲項目須要,自學了GUI編程,如今看來Android的界面編程在不少方面與其有殊途同歸之妙,這樣一來理解時也容易許多。不過Android使用XML佈局文件來控制視圖,不只簡單明瞭,也更能體現MVC模式。
不得不說,玩兒Android是一件挺有意思的事,死氣沉沉的代碼居然能將人的腦海中浮現的各類有趣的創意一一實現,實在是神奇。因此說,擁有高超編程能力的人,不只擁有縝密的思惟方式,也掌握着創造藝術品的本領:)
android:password="true"
,即指定了該文本框會用點來代替全部字符。android:autoLink="email|phone"
,即對郵件、電話增長超連接。(經過正則表達式判斷是否爲郵件或電話)android:inputType="numbewPassword"
代表只能接受數字密碼。經常使用的inputType參考:android EditText inputType說明android:hint="請填寫登陸帳號"
。效果爲:『問題一』關於API版本的疑問及解決
imageView.setImageAlpha(alpha);
是API 16以後纔有的方法;若是須要在API 15機上運行,須要使用imageView.setAlpha(alpha);
方法。『問題二』如何實現按鈕按下和鬆開時爲不一樣的圖片
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 指定按鈕按下時的圖片 --> <item android:state_pressed="true" android:drawable="@drawable/red" /> <!-- 指定按鈕鬆開時的圖片 --> <item android:state_pressed="false" android:drawable="@drawable/purple" /> </selector>
『問題三』關於如何實現文本域中灰色的提示文本的一點思考
前面提到,Android中可使用android:hint
顯示文本區域內的灰色提示文本,這讓我想起計算機實習時候婁老師也提出了相似的建議。若是用Java如何實現呢?查詢API發現沒有Android這樣直接的方法可供調用,因此須要本身編寫相關方法實現這種功能。
個人思考是,能夠設置循環檢測用戶輸入框的內容是否爲空(前提是焦點不在輸入框內,也就是鼠標的光標不在輸入框上),若是爲空則顯示提示文本,不然置空。好比這樣:
while (true) { if (userText.hasFocus() == false) { if (userText.getText().equals("")) { userText.setText("請填寫登陸帳號"); } } }
當按下鼠標左鍵時:(一個mouseClicked點擊過程分爲press按下和release釋放)
public void mousePressed(MouseEvent e) { if (userText.getText().equals("請填寫登陸帳號")) { userText.setText(""); } }
<input type="text" name="userName" id="userName" placeholder="請填寫登陸帳號" class="input-text input-long"/>
效果爲:
暑假實在是一個容易墮落的時期啊,空調房、冰淇淋,以及各類電影綜藝電視劇,都充滿了誘惑...有時候下午起牀安慰本身「我就看一下子電影」,不知不覺就到了晚上´Д`(這也是把博客拖延到如今的緣由┭┮﹏┭┮慚愧慚愧)老師常說「假期放鬆不放縱」,這個度還真很差把握,經常放鬆着放鬆着就回不到正軌上了(逃...
見了一個朋友A,目前在中原某211名校王牌專業讀書。爲了爭取國獎和保研資格,他一學期都沒有鬆懈過,期末考試某一掛科率高達40%的科目他居然拿到了90+的高分。到了暑假也不會放鬆本身,去了某國企實習。另一個在天津讀書的同窗B,暑假乾脆沒回家,去了北京學德語;還有一個朋友C,已經回學校作大創項目去了...
看到你們暑假都這麼忙碌,我就放心了(偷笑)。每次放鬆到瀕臨放縱時,就想一想,那麼多人都還在努力呢,要撐住吶...
AdapterView組件是一組重要的組件,AdapterView自己是一個抽象基類,它派生的子類在用法上十分類似,只是顯示界面有些不一樣。
從AdapterView派生出的三個子類:AdsListView、AdsSpinner、AdapterViewAnimator,這3個子類依然是抽象的,實際運用時須要它們的子類。
ProgressBar組件是很是重要的組件,ProgressBar自己表明着進度條組件。
『問題一』Android中xml的控件屬性app:
與Android:
有什麼區別
在設置menu_main時出現如下錯誤提示:
參考提示,將android:showAsAction="never"
改成app:showAsAction="never"
便可。那麼,app:
與Android:
這兩種控件屬性有什麼區別呢?
查詢資料瞭解到,這兩個是聲明的不一樣的命名空間,android的是系統的,app是自定義的。
android:命名空間android
用於 Android 系統定義的一些屬性。
app:命名空間app用於咱們應用自定義的一些屬性,這個與Android自定義屬性和系統控件擴展應該有關係,目前還在學習中...
『問題二』PNG、JPEG、GIF三種圖片格式的特性
這個問題是在設計logo以及APP中的各類圖標時想到的,但願瞭解各類格式的特性,以便達到品質和性能的最優化。查詢了有關資料,大體總結以下:
GIF圖形交換格式是一種位圖圖形文件格式,以8位色(即256種顏色)重現真彩色的圖像。它其實是一種壓縮文檔,採用LZW壓縮算法進行編碼,有效地減小了圖像文件在網絡上傳輸的時間。它是目前普遍應用於網絡傳輸的圖像格式之一。
便攜式網絡圖片(Portable Network Graphics),簡稱PNG,是一種無損數據壓縮位圖圖形文件格式。PNG格式是無損數據壓縮的,容許使用相似於GIF格式的調色板技術,支持真彩色圖像,並具有Alpha(半透明)等特性。如今有不少人使用PNG格式於互聯網及其餘方面上。
JPEG是一種針對相片影像而普遍使用的一種失真壓縮標準方法。JPEG的壓縮方式一般是破壞性資料壓縮(lossy compression),意即在壓縮過程當中圖像的品質會遭受到可見的破壞。
通常來講,GIF使用的壓縮算法使其在必定程度上保證圖像質量的同時將體積變得很小,同時可插入多幀,從而實現動畫效果。當圖像上顏色較少,而且主要以純色或者平滑的漸變色進行填充,或者圖像具有較大亮度差別以及強烈對比時,可使用PNG格式。而通常層次豐富顏色較多的圖像採用JPEG存儲。
『問題三』如何實現自動完成文本框
自動完成文本框(AutoCompleteTextView)用途很是普遍,它是一個文本編輯框,但多了一個功能:當用戶輸入必定字符後,自動完成文本框會顯示一個下拉菜單,供用戶從中選擇,當用戶選擇某個菜單項以後,AutoCompleteTextView按用戶選擇自動填寫該文本框。
XML屬性 | ** 相關方法 ** | 說明 |
---|---|---|
android:completionHint | setCompletionHint(CharSequence) | 設置出如今下拉菜單中的提示標題 |
android:completionThreshold | setThreshold(int) | 設置用戶至少輸入幾個字符纔會顯示提示 |
android:dropDownHeight | setDropDownHeight(int) | 設置下拉菜單的高度 |
android:dropDownHorizontalOffset | 設置下拉菜單與文本框之間的水平偏移,下拉菜單默認與文本框左對齊 | |
android:dropDownVerticalOffset | 設置下拉菜單與文本框之間的垂直偏移,下拉菜單默認緊跟文本框 | |
android:dropDownWidth | setdropDownWidth(int) | 設置下拉菜單的寬度 |
android:popupBackground | setDropDownBackgroundResource(int) | 設置下拉菜單的背景 |
計算機實習時期因爲時間緊迫,並無系統學習GUI編程,只是在須要某個組件的時候才查詢學習相關用法,因此相關知識的脈絡並非很清晰。吸收教訓,借暑假這個機會用了幾天時間總體學習Android的界面編程。學習過程當中發現界面編程的內容很是豐富,各類組件以及類之間的繼承關係都須要細細梳理。
事件處理也是Android開發的重要部分之一,因此接下來我會在繼續補充界面編程知識的同時,開始學習Android的事件處理機制。