安卓帧式布局

博客介绍了Android帧式布局的相关内容。帧式布局是层叠式布局,后添加控件会层叠在先添加的控件上。还给出了常用属性,并通过新建文件、修改布局资源文件、输入代码等步骤进行案例演示,展示了运行代码后的效果。

目录

布局特点

常用属性

案例演示


布局特点

帧式布局是一种层叠式的布局,后添加的控件会层叠在先添加的控件上。

常用属性

属性含义
scrollbars滚动条(none、horizontal、vertical)
layout_marginTop上边距
layout_marginBottom下边距
layout_marginLeft左边距
layout_marginRight右内边距
paddingLeft左内边距
paddingRighe右内边距
paddingTop上内边距
paddingBottom下内边距
background背景

案例演示

新建一个文件,打开主布局资源文件activity_main改为线性布局并在里面添加各种控件

 

 

具体代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        >
        <TextView
            android:id="@+id/tv_bottom"
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:layout_gravity="center"
            android:background="#ff0000"
            android:textColor="#ffff00"
            android:text="@string/bottom"

            android:textSize="30sp"/>
        <TextView
            android:id="@+id/tv_middle"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_gravity="center"
            android:background="#00ff00"
            android:textColor="#ffff00"
            android:text="@string/middle"

            android:textSize="30sp"/>
        <TextView
            android:id="@+id/tv_top"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:background="#0000ff"
            android:textColor="#ffff00"
            android:text="@string/top"

            android:textSize="30sp"/>


    </FrameLayout>
    <Button
        android:id="@+id/but_switch_color"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:onClick="doSwitchColor"
        android:textSize="20sp"
        android:text="@string/switch_color"
        />





</LinearLayout>

打开字符串资源文件strings.xml输入:

 具体代码:

<resources>
    <string name="app_name">帧式</string>
    <string name="top">顶层</string>
    <string name="middle">中层</string>
    <string name="bottom">底层</string>
    <string name="switch_color">切换颜色</string>

</resources>

打开主界面MainActivity文件

 输入:

 具体代码:

package net.zyt.switchcolor;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    protected TextView tvTop;
    protected TextView tvMiddle;
    protected TextView tvButtom;
    protected int[] colors;//颜色数组

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //利用布局资源文件设置用户界面
        setContentView(R.layout.activity_main);
        //利用资源标识符获取控件实例
        tvTop=findViewById(R.id.tv_top);
        tvMiddle=findViewById(R.id.tv_middle);
        tvButtom=findViewById(R.id.tv_bottom);
        //初始化颜色数组
        colors=new int[]{Color.RED,Color.GREEN,Color.BLUE};
    }
    /**切换颜色单击事件处理方法
    @param view
    **/
    public void doSwitchColor(View view){

        //通过颜色数组切换颜色
        int temp=colors[0];
        for(int i=0;i<colors.length-1;i++){
            colors[i]=colors[i+1];

        }
        colors[colors.length-1]=temp;

        //根据颜色方案设置三层标签颜色
        tvButtom.setBackgroundColor(colors[0]);
        tvMiddle.setBackgroundColor(colors[1]);
        tvTop.setBackgroundColor(colors[2]);

    }

}

运行代码最后效果:

 

### Android FrameLayout 的使用方法及示例 #### 基础概念 `FrameLayout` 是一种特殊的布局管理器,在其中所有的子控件都默认放置在其左上角位置。这种布局非常适合用于创建叠加层的效果,因为多个 `View` 可以堆叠在一起显示在一个区域内[^1]。 #### 创建基本的 FrameLayout 为了定义一个简单的 `FrameLayout` ,可以在 XML 文件中按照如下方声明: ```xml <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/frame_layout" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> ``` 这段代码建立了一个占据整个屏幕宽度和高度的基础框架布局。 #### 添加子 View 并控制其行为 向 `FrameLayout` 中添加子元素非常简单,只需像往常一样嵌套标签即可。值得注意的是,默认情况下所有新加入的组件都会定位到父级容器的顶部左侧角落处;如果希望改变它们的位置或者调整层次顺序,则可以通过设置相应的属性来达成目的,比如通过 `android:layout_gravity` 来指定相对位置以及利用 `bringToFront()` 方法让特定视图处于最前层[^3]。 #### 实现动态效果的例子——模拟墨迹天气应用中的拖拽功能 考虑到实际应用场景的需求,有时开发者可能想要模仿某些流行应用程序里的交互特性。例如,“墨迹天气”的界面设计就巧妙运用了 `FrameLayout` 来构建可滑动切换不同卡片的预报信息展示区。具体做法是在同一个 `FrameLayout` 下面布置多张图片或其他类型的 `ViewGroup` 组件,并且预先设定好各自的初始状态(如透明度、偏移量)。随着用户的触摸操作触发事件监听机制后,程序会相应更新这些参数从而达到平滑过渡的目的[^4]。 #### 自定义 FrameLayout 示例 对于更复杂的应用场景来说,还可以继承自 `FrameLayout` 类来自行扩展新的逻辑处理能力。下面给出了一段关于如何注册自定义类并在资源文件里实例化的示范片段[^2]: ```xml <com.example.CustomFrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 子View的具体配置 --> </com.example.CustomFrameLayout> ``` 以上便是有关于 `Android FrameLayout` 使用指南及其典型实践案例介绍的内容总结。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值