Android 控制項(button)對齊方法實現詳解

來源:互聯網
上載者:User

1,android:orientation
布局方向。horizontal是讓所有的子項目按水平方向從左至右排列, vertical是讓所有的子項目按豎直方向從上到下排列。

2,android:gravity 與 android:layout_gravity的區別android:gravity是指定本元素的子項目相對它的對齊。

android:layout_gravity是指定本元素相對它的父元素的對齊。
例如:
下面這裡的linearlayout的android:gravity設為right,有兩個子項目Button01和Button02。
java代碼: 複製代碼 代碼如下:<?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”
android:gravity=”right”
>
<Button android:text=”button01″ android:id=”@+id/Button01″ android:layout_width=”wrap_content” android:layout_height=”wrap_content”></Button>
<Button android:text=”button02″ android:id=”@+id/Button02″ android:layout_width=”wrap_content” android:layout_height=”wrap_content”></Button>
</LinearLayout>

這個main.xml裡的LinearLayout也是有兩個子項目Button01和Button02。Button01的android:layout_gravity設為”left”,Button02的 android:layout_gravity設為”right”
java代碼: 複製代碼 代碼如下:<?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:layout_gravity=”left”
android:text=”button01″
android:id=”@+id/Button01″
android:layout_width=”wrap_content” a
ndroid:layout_height=”wrap_content”></Button>
<Button
android:layout_gravity=”right”
android:text=”button02″
android:id=”@+id/Button02″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</Button>
</LinearLayout>

FameLayout布局
FrameLayout是最簡單的一個布局對象。它被定製為你螢幕上的一個空白備用地區,之後你可以在其中填充一個單一對象—比如,一張你要發布的圖片。所有的子項目將會固定在螢幕的左上方;你不能為FrameLayout中的一個子項目指定一個位置。後一個子項目將會直接在前一個子項目之上進行覆蓋填充,把它們部份或全部擋住(除非後 一個子項目是透明的)。

xml屬性
1,用xml檔案來定義介面,然後Activity的setContentView方法根據xml檔案裡的定義來建立真正的控制項對象。好比說xml檔案是設計圖紙,setContentView是生產機器,它依照圖紙生產出各種各樣的杯具和洗具。
2,FrameLayout的xml屬性來自三個地方:繼承下來的,嵌套類定義的,自身類定義的。
3,具的屬性可查閱官方文檔。下面是剛才遇到的幾個屬性。
java代碼: 複製代碼 代碼如下:android:id
//這個xml屬性是繼承自android.view類的xml屬性。它為framelayout提供一個唯一識別符號,之後,程式要用到它時可以用View.findViewById() 或Activity.findViewById()來找到它。
android:layout_width: 布局寬
android:layout_height: 布局高
//它們的取值是fill_parent或wrap_content。
fill_parent :在x軸或則y軸上充滿父容器的空間。
wrap_content :framelayout裡的元素有多少寬高就多少寬高,
//這兩個屬性都是定義在android.widget.FrameLayout的嵌套類android.widget.FrameLayout.LayoutParams裡。
android:background:背景
android:foreground :前景

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.