標籤:安卓 布局 activity 更新 layout
Android布局中,同樣可以與網頁的div布局,用百分比來指定寬度。同樣也可以像vb,c#,或者網頁中的absolute布局一樣,利用style為主題對話方塊的Activity整出模態視窗。比如,如所示的布局,在現在各類的安卓應用中很常見的。同時,設定在這個安卓程式一開始就彈出一個更新軟體的Activity。那該如何完成呢?
1、首先,先到res\values\string.xml中布置好各個組件的字型,當然你可以邊做安卓工程邊添加。命名的時候有一定的規則,指明這個字型,是在哪個Activity,然後指明這個字型是哪個組件的。我也想直接把字型放在各個布局檔案當中,但那樣Eclipse會提出警告,同時也不利於以後國際化軟體需要。可以從這個檔案看到,這個程式有兩個Activity,一個是mainActivity,裡面有6個按鈕,一個是updateActivity就是那個更新提醒的對話方塊。
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">布局</string> <string name="updateActivity_name">更新提醒</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="mainActivity_button1">功能1</string> <string name="mainActivity_button2">功能2</string> <string name="mainActivity_button3">功能3</string> <string name="mainActivity_button4">功能4</string> <string name="mainActivity_button5">功能5</string> <string name="mainActivity_button6">功能6</string><string name="updateActivity_textView1">發現新版本,是否更新?</string><string name="updateActivity_button1">更新</string><string name="updateActivity_button2">取消</string></resources>
2、之後先對MainActivity布局,修改res\layout\activity_main.xml檔案,如下的代碼:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" > <TableRow android:layout_weight="1" > <LinearLayout android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" > <Button android:id="@+id/button1" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:text="@string/mainActivity_button1" /> <Button android:id="@+id/button2" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:text="@string/mainActivity_button2" /> </LinearLayout> <LinearLayout android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" > <Button android:id="@+id/button3" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:text="@string/mainActivity_button3" /> </LinearLayout> </TableRow> <TableRow android:layout_width="match_parent" android:layout_weight="1" > <Button android:id="@+id/button4" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:text="@string/mainActivity_button4" /> <Button android:id="@+id/button5" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:text="@string/mainActivity_button5" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_weight="1" > <Button android:id="@+id/button6" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="@string/mainActivity_button6" /> </TableRow></TableLayout>
這裡布局思想如下:
設定一個鋪滿螢幕的Table布局,Table布局預設是不佔據一行的,你設定其match_parent也沒有什麼用。
只要對每一行指定其android:layout_weight="1"。
這裡的1不是100%的意思。是其所佔據的權重。如果有兩個及兩個組件以上,則根據這個權重來劃分整行
比如在第一行的兩個線性布局,皆設定android:layout_width="0dip"之後配合android:layout_weight="1"。則兩個布局在表格布局的第一行的所佔據的寬度,皆為這行的1/(1+1)=50%。也就是說,如果你要讓組件或者布局平分這行,可以為各個組件在設定android:layout_width="0dip"之後,再用android:layout_weight設定權重。
第一行,其實也可以不要線性布局,在三個按鈕都設定了android:layout_width="0dip"之後,按鈕1的android:layout_weight="1",按鈕2的android:layout_weight="1",按鈕3的android:layout_weight="2"皆能做出上面的效果。
之後兩個同理。
3、接著,如同《【Android】多個Activity之間利用bundle傳遞數值》(點擊開啟連結)一樣,建立一個Activity的類名為UpdateActivity.java,其布局檔案是activity_update.xml,安卓規定布局檔案只能是a-z,0-9,_不能有其它符號,也不能有大寫,很討厭的。主要其害怕有其它符號,在R檔案給你註冊不了。在工程包中弄好了Activity類,在res\layout中弄好相應的布局檔案,先在AndroidManifest.xml的application節點註冊這個Activity,指定實作類別,同時指定其主題為主題對話方塊,這樣,這個Activity開啟就不會覆蓋整個螢幕,以模態視窗的形勢開啟,整個AndroidManifest.xml修改之後如下:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.layouttest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.layouttest.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 註冊UpdateActivity --> <activity android:name="com.example.layouttest.UpdateActivity" android:label="@string/updateActivity_name" android:theme="@android:style/Theme.Dialog" > </activity> </application></manifest>
4、在activity_update.xml用相對布局為更新提醒這個UpdateActivity布好局,代碼如下:設定好各個組件的字型,高度與寬度皆是包裹好內容,即可。之後,通過layout_alignRight指定Button2對其TextView1的右端,通過layout_below把Button2放著在TextView1的下方。其中,TextView1是提醒更新的文字,Button2是取消按鈕。之後的Button1則同樣放置在TextView1的下方,但是放在Button2的左方。這樣,就成功對更新提醒這個UpdateActivity布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/updateActivity_textView1" android:textSize="24sp" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/textView1" android:layout_below="@+id/textView1" android:text="@string/updateActivity_button2" android:textSize="18sp" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_toLeftOf="@+id/button2" android:text="@string/updateActivity_button1" android:textSize="18sp" /></RelativeLayout>
5、修改UpdateActivity.java的代碼,為“取消”按鈕指定事件,就是關閉這個Activity,如下:
package com.example.layouttest;import android.app.Activity;import android.os.Bundle;import android.widget.Button;import android.view.View;import android.view.View.OnClickListener;public class UpdateActivity extends Activity {private Button button2;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_update);button2 = (Button) findViewById(R.id.button2);button2.setOnClickListener(new OnClickListener() {// 為button2添加點擊事件@Overridepublic void onClick(View v) {finish();// 關閉此Activity }});}}6、最後修改MainActivity.java的代碼,就一句,程式一啟動,就開啟更新提醒Activity的UpdateActivity。一般情況下,是在服務端設定一個版本號碼,在app程式設定一個版本號碼,開始先判斷是否相同,再彈出這個對話方塊。
package com.example.layouttest;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Intent intent = new Intent(MainActivity.this, UpdateActivity.class);startActivity(intent);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}至此,整個app開發結束。
【Android】利用相對布局布置更新軟體的style為主題對話方塊的Activity,利用layout_weight屬性對錶格布局的行劃分