日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android帧布局(Frame Layout)

發布時間:2025/4/16 Android 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android帧布局(Frame Layout) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android幀布局(Frame Layout)

?

FrameLayout是最簡單的一個布局管理器。FrameLayout為每個加入其中的組件創建一個空白區域(一幀),這些組件根據layout_gravity執行自動對齊,如果組件layout_gravity的值一樣,那么后面添加的組件會覆蓋之前的。如果沒有指定layout_gravity,默認為固定在屏幕的左上角。下面以幾個例子來說明。

?

1.?????根據layout_gravity的值來控制組件的位置


圖1

Xml代碼如下:

<FrameLayout 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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.example.framelayout.MainActivity"><TextViewandroid:id="@+id/view1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="top"android:width="20px"android:height="20px"android:background="#f00"/><TextViewandroid:id="@+id/view2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom"android:width="40px"android:height="40px"android:background="#0f0"/><TextViewandroid:id="@+id/view3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="left|center"android:width="60px"android:height="60px"android:background="#00f"/><TextViewandroid:id="@+id/view4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="right"android:width="80px"android:height="80px"android:background="#ff0"/><TextViewandroid:id="@+id/view5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="right|center"android:width="100px"android:height="100px"android:background="#f0f"/><TextViewandroid:id="@+id/view6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom|right"android:width="120px"android:height="120px"android:background="#0ff"/><TextViewandroid:id="@+id/view7"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:width="140px"android:height="140px"android:background="#123"/></FrameLayout>

?

2.?????霓虹燈圖


圖2

Xml代碼如下:

<FrameLayout 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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.example.framelayout.MainActivity"><TextViewandroid:id="@+id/view1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:width="320px"android:height="320px"android:background="#f00"/><TextViewandroid:id="@+id/view2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:width="280px"android:height="280px"android:background="#0f0"/><TextViewandroid:id="@+id/view3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:width="240px"android:height="240px"android:background="#00f"/><TextViewandroid:id="@+id/view4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:width="200px"android:height="200px"android:background="#ff0"/><TextViewandroid:id="@+id/view5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:width="160px"android:height="160px"android:background="#f0f"/><TextViewandroid:id="@+id/view6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:width="120px"android:height="120px"android:background="#0ff"/></FrameLayout>

如果只是通過上面xml代碼來控制,這是靜態的霓虹燈效果,可以通過代碼控制實現動態的霓虹燈效果,實現原理是一個Timer定時調用TimerTask類run(),run()發送消息,然后handler從消息隊列中讀取出消息,并動態刷新這6個view的背景色,這樣就可以實現了動態霓虹燈效果了,代碼如下:

package com.example.framelayout;import java.util.Timer; import java.util.TimerTask; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.os.Message; import android.os.Handler; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView;public classMainActivity extends ActionBarActivity {private int currentColor = 0;protected static final String ACTIVITY_TAG="FramLayout";final int[] colors = new int[] {R.color.color1,R.color.color2,R.color.color3,R.color.color4,R.color.color5,R.color.color6};final int[] names = new int[] {R.id.view1,R.id.view2,R.id.view3,R.id.view4,R.id.view5,R.id.view6};TextView[]views= newTextView[names.length];//創建一個匿名內部類,此類繼承父類Handler并重寫handleMessage方法。并立即創建此類的一個實例,創建后由handler指向此對象 Handlerhandler= new Handler(){@Overridepublic void handleMessage(Messagemsg){if(msg.what == 0x123){for(int i = 0; i < names.length; i++){views[i].setBackgroundResource(colors[(i + currentColor) % names.length]);}currentColor++;//Log.d(tag,message),把debug級別的日志打印到LogCat中Log.d(ACTIVITY_TAG,"handleMessage()"+currentColor);}super.handleMessage(msg);//這里的super就是Handler}};@Overrideprotected void onCreate(BundlesavedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);for(int i = 0; i < names.length; i++){views[i] = (TextView)findViewById(names[i]);}//1.創建一個匿名內部類,此類繼承父類TimerTask并重寫run方法。//2.創建一個Timer對象并調用schedule()來啟動定時器new Timer().schedule(new TimerTask(){@Overridepublic void run(){handler.sendEmptyMessage(0x123);}},0,200);}}

?

實現此代碼過程中,說明幾點:

(1)增加color資源文件

右鍵values,選擇New--->Android Xml file,如下圖:


圖3

生成color.xml文件,增加內容后如下:

<?xml version="1.0"encoding="utf-8"?> <resources><color name="color1">#f00</color><color name="color2">#0f0</color><color name="color3">#00f</color><color name="color4">#ff0</color><color name="color5">#f0f</color><color name="color6">#0ff</color> </resources>

(2)匿名內部類

匿名內部類適合創建那種只需要一次使用的類,創建匿名內部類時會立即創建一個該類實例,這個類定義立即消失,匿名類不能重復使用,定義匿名內部類格式如下:

new 父類構造器(實參列表)|實現接口()

{

?????? //匿名內部類的類體部分。

}

從上面定義可看出,匿名內部類必須繼承一個父類,或實現一個接口,但最多只能繼承一個父類或實現一個接口。

?

(3)定時器啟動定時器方法schedule()

timer.schedule(newMyTask(),long time1,long timer2);

?

第一個參數,是 TimerTask 類,在包:import java.util.TimerTask .使用者要繼承該類,并實現public void run() 方法,因為 TimerTask 類實現了 Runnable 接口。

第二個參數的意思是,當你調用該方法后,該方法必然會調用 TimerTask 類 TimerTask 類中的 run()方法,這個參數就是這兩者之間的差值,轉換成漢語的意思就是說,用戶調用 schedule() 方法后,要等待這么長的時間才可以第一次執行run() 方法。

第三個參數的意思就是,第一次調用之后,從第二次開始每隔多長的時間調用一次 run() 方法。

?

?

(4)定位類所在的包

比如Handler在哪個包呢,在elipse中選擇Handler,彈出下面的內容:


圖4

選著紅色矩形框選擇的內容就會自動在java文件多了下面的內容:

import android.os.Handler;

總結

以上是生活随笔為你收集整理的Android帧布局(Frame Layout)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。