Android?實(shí)例代碼帶你掌握FrameLayout
概述
FrameLayout以層疊的方式布局組件:每次只能顯示其中的一個(gè)。與撲克牌類似,當(dāng)疊加在一起時(shí)只能看到最上面的那張。FrameLayout為布局在其中的組件提供了一個(gè)XML配置屬性:Android:layout_gravity。通過(guò)這個(gè)屬性,布局在FrameLayout中的組件可以指定自己在容器中的重心位置,例如,靠左,靠右等, 所有控件都默認(rèn)顯示在屏幕左上角。
FrameLayout全局定義的屬性

練習(xí)一
實(shí)現(xiàn)下面布局

代碼:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="@mipmap/ic_launcher"
android:foregroundGravity="left">
<Button
android:layout_width="340dp"
android:layout_height="570dp"
android:text="按鈕1"
android:background="#A0230E"
/>
<Button
android:layout_width="250dp"
android:layout_height="220dp"
android:text="按鈕2"
android:background="#0A6188"
/>
</FrameLayout>練習(xí)二
實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊圖片,然后圖片切換的效果(4張圖片自己選擇)

代碼:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/p1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/p1"
android:scaleType="fitCenter"
android:visibility="gone"
/>
<ImageView
android:id="@+id/p2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/p2"
android:scaleType="fitCenter"
android:visibility="gone"
/>
<ImageView
android:id="@+id/p3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/p3"
android:scaleType="fitCenter"
android:visibility="gone"
/>
<ImageView
android:id="@+id/p4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/p4"
android:scaleType="fitCenter"
android:visibility="visible"
/>
</FrameLayout>MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toolbar;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ImageView p1,p2,p3,p4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
p1=(ImageView)this.findViewById(R.id.p1);
p1.setOnClickListener(this);
p2=(ImageView)this.findViewById(R.id.p2);
p2.setOnClickListener(this);
p3=(ImageView)this.findViewById(R.id.p3);
p3.setOnClickListener(this);
p4=(ImageView)this.findViewById(R.id.p4);
p4.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id= view.getId();
switch (id){
case R.id.p1:
p1.setVisibility(View.GONE);
p2.setVisibility(View.VISIBLE);
break;
case R.id.p2:
p2.setVisibility(View.GONE);
p3.setVisibility(View.VISIBLE);
break;
case R.id.p3:
p3.setVisibility(View.GONE);
p4.setVisibility(View.VISIBLE);
break;
case R.id.p4:
p4.setVisibility(View.GONE);
p1.setVisibility(View.VISIBLE);
break;
}
}
}到此這篇關(guān)于Android 實(shí)例代碼帶你掌握FrameLayout的文章就介紹到這了,更多相關(guān)Android FrameLayout內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android spinner下垃菜單用法實(shí)例詳解
這篇文章主要介紹了Android spinner下垃菜單用法,詳細(xì)分析了spinner下垃菜單的定義、布局及功能實(shí)現(xiàn)相關(guān)技巧,需要的朋友可以參考下2016-07-07
Android中傳遞對(duì)象的三種方法的實(shí)現(xiàn)
本篇文章主要介紹了Android中傳遞對(duì)象的三種方法的實(shí)現(xiàn),可以通過(guò)Bundle、Intent或者JSON字符串,有興趣的可以了解一下。2017-02-02
Android Imageloader的配置的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android Imageloader的配置的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-07-07
Android項(xiàng)目實(shí)現(xiàn)黑名單攔截效果
本篇文章主要介紹了Android項(xiàng)目實(shí)現(xiàn)黑名單攔截效果,可以根據(jù)白名單或者黑名單攔截,測(cè)試可以攔截電話,有需要的朋友可以了解一下。2016-10-10
Android Dialog 對(duì)話框詳解及示例代碼
本文主要介紹Android Dialog,這里詳細(xì)介紹Android Dialog的基本使用方法,并提供了示例代碼和實(shí)現(xiàn)效果圖,有需要的小伙伴可以參考下2016-08-08
詳細(xì)分析Android中onTouch事件傳遞機(jī)制
相信不少朋友在剛開(kāi)始學(xué)習(xí)Android的時(shí)候,對(duì)于onTouch相關(guān)的事件一頭霧水。分不清onTouch(),onTouchEvent()和OnClick()之間的關(guān)系和先后順序,所以覺(jué)得有必要搞清onTouch事件傳遞的原理。經(jīng)過(guò)一段時(shí)間的琢磨以及相關(guān)博客的介紹,這篇文章就給大家詳細(xì)的分析介紹下。2016-10-10
Android中使用OkHttp包處理HTTP的get和post請(qǐng)求的方法
OkHttp包為安卓開(kāi)發(fā)中的HTTP協(xié)議網(wǎng)絡(luò)編程帶來(lái)了很大的便利,這里我們就來(lái)看一下最基本的、Android中使用OkHttp包處理HTTP的get和post請(qǐng)求的方法:2016-07-07

