
?
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:text="Up" android:id="@+id/Button03" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:text="left" android:id="@+id/Button01" android:width="120px" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="right" android:id="@+id/Button02" android:width="120px" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> </LinearLayout>二、TableLayout

?
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TableLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableRow android:gravity="center"> <Button android:text="Text1" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> </TableRow> <TableRow android:gravity="center"> <TextView android:text="第一行第0列" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <TextView android:text="第一行第1列" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <Button android:text="Text2" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> </TableRow> <TableRow android:gravity="center"> <TextView android:text="第二行第0列" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <TextView android:text="第二行第1列" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> </TableRow> </TableLayout>?
三、RelativeLayout
不用像LinearLayout一樣在xml把控件按照順序寫下,當然按照順序寫下方便查看,如果是自己選的的順序,直接寫下也可以的。
?相對布局可以理解為某一個元素為參照物,來定位的布局方式。
android:layout_方向 = id 表示 在這個id對應的控件的方向上(上|下)
android:layout_align方向 = id 表示和這個控件的(上下左右)對齊
android: layout_to方向Of = id 表示在這個控件的 左或者右
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/btnmiddle" android:text="Middle" android:layout_width="200px" android:layout_height="wrap_content" android:layout_centerInParent="true"> </Button> <Button android:id="@+id/btnup" android:text="Up" android:layout_width="100px" android:layout_height="wrap_content" android:layout_above="@id/btnmiddle" android:layout_alignLeft="@id/btnmiddle"> </Button> <Button android:id="@+id/btndown" android:text="down" android:layout_width="100px" android:layout_height="wrap_content" android:layout_below="@id/btnmiddle" android:layout_alignRight="@id/btnmiddle"> </Button> <Button android:id="@+id/btnfirst" android:text="first" android:layout_width="100px" android:layout_height="wrap_content" android:layout_above="@id/btnup" android:layout_alignLeft="@id/btnmiddle"> </Button> </RelativeLayout>
四、AbsoluteLayout
AbsoluteLayout 這個布局方式很簡單,主要屬性就兩個 layout_x 和 layout_y 分別定義 這個組件的絕對位置。 即,以屏幕左上角為(0,0)的坐標軸的x,y值,
當向下或向右移動時,坐標值將變大。AbsoluteLayout 沒有頁邊框,允許元素之間互相重疊(盡管不推薦)。
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:text="UserName:" android:layout_height="wrap_content" android:id="@+id/tvName" android:layout_y="20dip" android:layout_x="50dip"> </TextView> <TextView android:layout_width="wrap_content" android:text="Password:" android:layout_height="wrap_content" android:id="@+id/tvPassword" android:layout_y="100dip" android:layout_x="55dip"> </TextView> <EditText android:layout_width="150px" android:layout_height="wrap_content" android:id="@+id/tvPassword" android:layout_y="10dip" android:layout_x="120dip"> </EditText> <EditText android:layout_width="150px" android:layout_height="wrap_content" android:id="@+id/tvPassword" android:layout_y="90dip" android:layout_x="120dip"> </EditText> </AbsoluteLayout>
五、FrameLayout
它被定制為你屏幕上的一個空白備用區域,之后你可以在其中填充一個單一對象 — 比如,一張你要發布的圖片。所有的子元素將會固定在屏幕的左上角;你不能為FrameLayout中的一個子元素指定一個位置。后一個子元素將會直接在前一個子元素之上進行覆蓋填充,把它們部份或全部擋住(除非后一個子元素是透明的)
用一個例子解決
通過點擊來更換圖片
package com.example.linearlayouttext; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; public class MainActivity extends Activity { private String TAG = "FramLayoutTestActivity"; private ImageView image1; private ImageView image2; private ImageView image3; private List<ImageView> list; private int count=0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { //關聯 super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image1=(ImageView)findViewById(R.id.image1); image2=(ImageView)findViewById(R.id.image2); image3=(ImageView)findViewById(R.id.image3); //建了一個list用來存儲圖片 list=new ArrayList<ImageView>(); list.add(image1); list.add(image2); list.add(image3); } //監聽 用來更換圖片 @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub if(event.getAction()==MotionEvent.ACTION_DOWN) { showImage(); } return super.onTouchEvent(event); } private void showImage() { image1.setVisibility(View.VISIBLE); count=count%3; for(ImageView i:list) { i.setVisibility(View.INVISIBLE); } list.get(count).setVisibility(View.VISIBLE); count++; } }
?
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#897753" > <ImageView android:id="@+id/image1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="invisible" android:src="@drawable/flower"/> <ImageView android:id="@+id/image2" android:visibility="invisible" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/f2"/> <ImageView android:id="@+id/image3" android:visibility="invisible" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/f3"/> </FrameLayout>
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
