溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android中FrameLayout的示例分析

發(fā)布時(shí)間:2022-03-31 09:01:00 來(lái)源:億速云 閱讀:202 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)Android中FrameLayout的示例分析,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

概述

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

FrameLayout全局定義的屬性

Android中FrameLayout的示例分析

練習(xí)一

實(shí)現(xiàn)下面布局

Android中FrameLayout的示例分析

代碼:

<?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張圖片自己選擇)

Android中FrameLayout的示例分析

代碼:

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中FrameLayout的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI