標籤:
除了上面我們談過的控制項外,其實還有非常多其他的。我們談的基本都是非常基礎的,因此其他的控制項還需要學習者在後面製作中進行學習。除了這些組件外,我們還需要有一些東西把這些組件包含在內,這東西就是我們所說的布局。
Android中有四種基本的配置模式(Layout):LinearLayout, RelativeLayout, TableLayout, FrameLayout
———————————————————華麗麗的分割線——————————————————————
1. LinearLayout
首先,我們先來看LinearLayout。這個布局在Android的APP中非常常用。這種類型就像我們生活中排隊類似(假設被個人都是一個組件)。因此在排隊時,我們可以橫著排,也可以豎著排。接下來我們來分析一下LinearLayout中的屬性。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" ></LinearLayout>
LinearLayout中有非常多的屬性,我經常用的主要有android:layout_height,android:layout_width以及android:orientation。其中最後一個屬性我們上面沒有看到過。
android:orientation說明的是LinearLayout這個布局的方向問題。也就是我們是要各個組件橫著排隊還是豎著排隊。在這個屬性中,其提供了兩種選擇:vertical和horizontal,其對應著往哪個方向排隊。
除了以上這些在布局中設定的屬性外,我們還可以線上性布局中的各個空間中設定控制項在這個布局中的位置,設定這個屬性的屬性名稱為android:layout_gravity。top,bottom,center_vertical等等,具體需要什麼位置,在選項中自己進行選擇。
2. RelativeLayout
這個布局叫做相對布局。在這個布局中我們使用相對位置的屬性控制組件在布局中的相對位置以及組件與組件相對位置。首先我們先看一下組件相對於布局的位置。
組件相對於布局的相對位置:
其中的屬性有android:alignParentLeft, android:alignParentRight, android:alignParentTop, android:alignParentBottom, android:centerInParent這些屬性設定組件相對於這個布局的相對位置。
在具體的使用中,比如 android:alignParentRight="true",組件就會貼近父類的右邊緣。這裡的屬性可以聯合使用。
控制項的相對位置屬性:
其中包括android:toRightOf, android:layout_above, android:toLeftOf ,android:layout_below等等屬性來控制相對於一控制項的定位。與相對於布局的相對位置相似,這些屬性也能聯合使用,具體的設定如下所示:
比如:android:layout_below="@id/button2"
使用android:layout_alignLeft表示讓一個控制項的左邊緣與另一個控制項的左邊緣對齊,同理的屬性還有android:layout_alignRight等等。
3. FrameLayout
frame有架構的意思,那我們就稱它為架構布局吧。在《第一行代碼》中提到,這種類型的布局用的並不是很多的。其主要功能就是將一些組件進行覆蓋,後面添加的組件在前面組件的上面。在之後我們在製作分區(fragment)的時候會進一步用到,這裡只是簡要的說明下。
4. TableLayout
表格類型的布局。這種類型就像表格一樣,你需要將一個個組件放到表格的空格之中。我找了找,好像對於單個表格中的空格我們無法對其設定大小。對於每一列具有統一的大小。接下來我們看看布局檔案中是如何書寫的:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TableRow> <TextView android:layout_height="match_parent" android:text="Account:"/> <EditText android:id="@+id/account" android:layout_height="wrap_content" android:hint="Input"/> </TableRow> <TableRow> <TextView android:layout_height="match_parent" android:text="Password:"/> <EditText android:id="@+id/password" android:layout_height="wrap_content" android:inputType="textPassword"/> </TableRow> <TableRow> <Button android:id="@+id/login" android:layout_height="wrap_content" android:layout_span="2" android:text="Login"/> </TableRow></TableLayout>
首先我們建立表格版面配置,接著我們需要在表格版面配置中添加元素。我們第一個要添加的就是行元素,即<TableRow>,這裡添加的任何組件都會出現在同一行之中。每一個組件佔據一列。在這個布局中,每添加一個TableRow元素即是表示在表格中添加一行。TableRow並不不指定寬度。
因為不指定寬度,任何組件都是適合組件大小,這樣會出現一個問題,那就是無法出現滿屏,這樣會非常影響APP的美觀,因此在TableLayout中有一個屬性可以指定將某一列進行展開,即android:stretchColumn="1",這個屬性的意思是如果螢幕中沒有滿屏,則將第二個控制項拉長。
p.s. 在布局中,我們可以調控每個控制項在布局中的位移位置:
android:layout_marginXXX這個屬性控制一個控制項上下左右方向上的位移距離,詳細的
android:layout_marginLeft對於具體方向上的位移。
5. 自訂布局
除了上面介紹的四種布局外,還有很多的自訂布局。比如我們在布局之中添加子布局。因此我們先寫好子布局,然後在父布局中進行引用:
<include layout="@layout/external_layout"/>
我們在自訂布局中我們需要添加一些按鈕等需要反饋的組件,我們不可能就讓它們傻傻地放在布局上面不做反應吧。下面部分我們將對具體的布局進行自定設定。我們按照書中的例子對線性布局進行設定。
建立一個類型,繼承自一些基礎的4個布局的一種。
public class TitleLayout extends LinearLayout implements View.OnClickListener{ public TitleLayout(Context context, AttributeSet attrs) { super(context,attrs); LayoutInflater.from(context).inflate(R.layout.external_layout,this); Button titleBack = (Button) findViewById(R.id.title_back); Button titleEdit = (Button) findViewById(R.id.title_edit); titleBack.setOnClickListener(this); titleEdit.setOnClickListener(this); } @Override public void onClick(View v){ switch(v.getId()) { case R.id.title_back: ((Activity)getContext()).finish(); break; case R.id.title_edit: Toast.makeText(getContext(),"You click Edit button",Toast.LENGTH_SHORT).show(); break; default: } }}
在編寫完自訂的布局之後,我們還需要對其進行將其添加到父布局之中,APP才能進行顯示,具體的操作如下所示:
<com.carels.chapterthree.TitleLayout android:layout_height="wrap_content" android:layout_width="match_parent"/>
差不多布局就是這麼多了,能設計出好的布局的人都是天才啊。我們需要的是從別人好的設計中汲取靈感,設計出自己的好看的布局。
Android學習(四)——Android配置模式