Android 控件(button)對(duì)齊方法實(shí)現(xiàn)詳解
更新時(shí)間:2013年06月02日 16:17:23 作者:
horizontal是讓所有的子元素按水平方向從左到右排列,vertical是讓所有的子元素按豎直方向從上到下排列,下面為大家介紹下控件(button)的對(duì)齊方法
1,android:orientation
布局方向。horizontal是讓所有的子元素按水平方向從左到右排列, vertical是讓所有的子元素按豎直方向從上到下排列。
2,android:gravity 與 android:layout_gravity的區(qū)別android:gravity是指定本元素的子元素相對(duì)它的對(duì)齊方式。
android:layout_gravity是指定本元素相對(duì)它的父元素的對(duì)齊方式。
例如:
下面這里的linearlayout的android:gravity設(shè)為right,有兩個(gè)子元素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>
這個(gè)main.xml里的LinearLayout也是有兩個(gè)子元素Button01和Button02。Button01的android:layout_gravity設(shè)為”left”,Button02的 android:layout_gravity設(shè)為”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是最簡(jiǎn)單的一個(gè)布局對(duì)象。它被定制為你屏幕上的一個(gè)空白備用區(qū)域,之后你可以在其中填充一個(gè)單一對(duì)象—比如,一張你要發(fā)布的圖片。所有的子元素將會(huì)固定在屏幕的左上角;你不能為FrameLayout中的一個(gè)子元素指定一個(gè)位置。后一個(gè)子元素將會(huì)直接在前一個(gè)子元素之上進(jìn)行覆蓋填充,把它們部份或全部擋?。ǔ呛?一個(gè)子元素是透明的)。
xml屬性
1,用xml文件來(lái)定義界面,然后Activity的setContentView方法根據(jù)xml文件里的定義來(lái)創(chuàng)建真正的控件對(duì)象。好比說(shuō)xml文件是設(shè)計(jì)圖紙,setContentView是生產(chǎn)機(jī)器,它依照?qǐng)D紙生產(chǎn)出各種各樣的杯具和洗具。
2,F(xiàn)rameLayout的xml屬性來(lái)自三個(gè)地方:繼承下來(lái)的,嵌套類定義的,自身類定義的。
3,具的屬性可查閱官方文檔。下面是剛才遇到的幾個(gè)屬性。
java代碼:
android:id
//這個(gè)xml屬性是繼承自android.view類的xml屬性。它為framelayout提供一個(gè)唯一識(shí)別符號(hào),之后,程序要用到它時(shí)可以用View.findViewById() 或Activity.findViewById()來(lái)找到它。
android:layout_width: 布局寬
android:layout_height: 布局高
//它們的取值是fill_parent或wrap_content。
fill_parent :在x軸或則y軸上充滿父容器的空間。
wrap_content :framelayout里的元素有多少寬高就多少寬高,
//這兩個(gè)屬性都是定義在android.widget.FrameLayout的嵌套類android.widget.FrameLayout.LayoutParams里。
android:background:背景
android:foreground :前景
布局方向。horizontal是讓所有的子元素按水平方向從左到右排列, vertical是讓所有的子元素按豎直方向從上到下排列。
2,android:gravity 與 android:layout_gravity的區(qū)別android:gravity是指定本元素的子元素相對(duì)它的對(duì)齊方式。
android:layout_gravity是指定本元素相對(duì)它的父元素的對(duì)齊方式。
例如:
下面這里的linearlayout的android:gravity設(shè)為right,有兩個(gè)子元素Button01和Button02。
java代碼:
復(fù)制代碼 代碼如下:
<?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>
這個(gè)main.xml里的LinearLayout也是有兩個(gè)子元素Button01和Button02。Button01的android:layout_gravity設(shè)為”left”,Button02的 android:layout_gravity設(shè)為”right”
java代碼:
復(fù)制代碼 代碼如下:
<?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是最簡(jiǎn)單的一個(gè)布局對(duì)象。它被定制為你屏幕上的一個(gè)空白備用區(qū)域,之后你可以在其中填充一個(gè)單一對(duì)象—比如,一張你要發(fā)布的圖片。所有的子元素將會(huì)固定在屏幕的左上角;你不能為FrameLayout中的一個(gè)子元素指定一個(gè)位置。后一個(gè)子元素將會(huì)直接在前一個(gè)子元素之上進(jìn)行覆蓋填充,把它們部份或全部擋?。ǔ呛?一個(gè)子元素是透明的)。
xml屬性
1,用xml文件來(lái)定義界面,然后Activity的setContentView方法根據(jù)xml文件里的定義來(lái)創(chuàng)建真正的控件對(duì)象。好比說(shuō)xml文件是設(shè)計(jì)圖紙,setContentView是生產(chǎn)機(jī)器,它依照?qǐng)D紙生產(chǎn)出各種各樣的杯具和洗具。
2,F(xiàn)rameLayout的xml屬性來(lái)自三個(gè)地方:繼承下來(lái)的,嵌套類定義的,自身類定義的。
3,具的屬性可查閱官方文檔。下面是剛才遇到的幾個(gè)屬性。
java代碼:
復(fù)制代碼 代碼如下:
android:id
//這個(gè)xml屬性是繼承自android.view類的xml屬性。它為framelayout提供一個(gè)唯一識(shí)別符號(hào),之后,程序要用到它時(shí)可以用View.findViewById() 或Activity.findViewById()來(lái)找到它。
android:layout_width: 布局寬
android:layout_height: 布局高
//它們的取值是fill_parent或wrap_content。
fill_parent :在x軸或則y軸上充滿父容器的空間。
wrap_content :framelayout里的元素有多少寬高就多少寬高,
//這兩個(gè)屬性都是定義在android.widget.FrameLayout的嵌套類android.widget.FrameLayout.LayoutParams里。
android:background:背景
android:foreground :前景
相關(guān)文章
Android中使用SQLite3 命令行查看內(nèi)嵌數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Android中使用SQLite3 命令行查看內(nèi)嵌數(shù)據(jù)庫(kù)的方法的相關(guān)資料,需要的朋友可以參考下2015-12-12
android 仿微信demo——注冊(cè)功能實(shí)現(xiàn)(移動(dòng)端)
本篇文章主要介紹了微信小程序-閱讀小程序?qū)嵗╠emo),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧,希望能給你們提供幫助2021-06-06
Android點(diǎn)擊事件的實(shí)現(xiàn)方式
這篇文章主要為大家詳細(xì)介紹了Android點(diǎn)擊事件的實(shí)現(xiàn)方式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
Android實(shí)現(xiàn)指定時(shí)間定時(shí)觸發(fā)方法
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)指定時(shí)間定時(shí)觸發(fā)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
android調(diào)用WebService實(shí)例分析
這篇文章主要介紹了android調(diào)用WebService的方法,以實(shí)例形式較為詳細(xì)的分析了WebService的調(diào)用原理與具體使用方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
android中使用SharedPreferences進(jìn)行數(shù)據(jù)存儲(chǔ)的操作方法
本篇文章介紹了,在android中使用SharedPreferences進(jìn)行數(shù)據(jù)存儲(chǔ)的操作方法。需要的朋友參考下2013-04-04
Android使用CardView實(shí)現(xiàn)圓角對(duì)話框
這篇文章主要為大家詳細(xì)介紹了Android使用CardView實(shí)現(xiàn)圓角對(duì)話框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
android Activity線性布局和表格布局實(shí)例講解
在activity的布局中,線性布局和表格布局是最簡(jiǎn)單的,這次分別從線性布局,表格布局以及線性布局和表格混合布局做了實(shí)驗(yàn)2013-11-11

