温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中FrameLayout的示例分析

发布时间:2022-03-31 09:01:00 来源:亿速云 阅读:185 作者:小新 栏目:开发技术

这篇文章将为大家详细讲解有关Android中FrameLayout的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

概述

       FrameLayout以层叠的方式布局组件:每次只能显示其中的一个。与扑克牌类似,当叠加在一起时只能看到最上面的那张。FrameLayout为布局在其中的组件提供了一个XML配置属性:Android:layout_gravity。通过这个属性,布局在FrameLayout中的组件可以指定自己在容器中的重心位置,例如,靠左,靠右等, 所有控件都默认显示在屏幕左上角。

FrameLayout全局定义的属性

Android中FrameLayout的示例分析

练习一

实现下面布局

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>

练习二

实现鼠标点击图片,然后图片切换的效果(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;
        }
    }
}

关于“Android中FrameLayout的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI