In this tutorial we’ll dive into Android FrameLayout and Android AbsoluteLayout. This is the second tutorial in the layout series, earlier we looked into Android LinearLayout and RelativeLayout examples.

在本教程中,我们将深入探讨Android FrameLayoutAndroid AbsoluteLayout 。 这是布局系列的第二篇教程,之前我们研究了Android LinearLayout和RelativeLayout示例。

Android FrameLayout (Android FrameLayout)

Android FrameLayout is one of the useful layouts provided by android system, which allows User Interface widgets to be overlapped with each other. It’s used in cases such as placing a TextView over an ImageView. This becomes quite difficult to implement using LinearLayout or RelativeLayout since they place widgets adjacent to each other.

Android FrameLayout是android系统提供的有用布局之一,它允许用户界面小部件彼此重叠。 它用于将TextView放在ImageView上的情况。 使用LinearLayout或RelativeLayout很难实现,因为它们将小部件彼此相邻放置。

FrameLayout is designed to display a single item at a time. We can have multiple elements within a FrameLayout but each element will be positioned based on the top left of the screen. In FrameLayout, all the child views added are placed like stack. The most recent added are shown on top. Hence the order of elements in the layout is of importance.

FrameLayout旨在一次显示一个项目。 我们可以在FrameLayout包含多个元素,但是每个元素都将基于屏幕的左上角定位。 在FrameLayout中 ,所有添加的子视图都像stack一样放置。 最新添加的内容显示在顶部。 因此,布局中元素的顺序很重要。

FrameLayout属性 (FrameLayout Attributes)

Following are the most important attributes used in this layout:

以下是此布局中使用的最重要的属性:

  • android:id : This is the ID which uniquely identifies the layoutandroid:id :这是唯一标识布局的ID
  • android:foreground : This defines the drawable to draw over the content and possible values may be a color value, in the form of “#rgb”, “#argb”, “#rrggbb”, or “#aarrggbb”android:foreground :它定义了绘制内容的绘制对象,可能的值可能是颜色值,形式为“ #rgb”,“#argb”,“#rrggbb”或“ #aarrggbb”
  • android:foregroundGravity : Defines the gravity to apply to the foreground drawable. The gravity defaults to fill. Possible values are top, bottom, left, right, center, center_vertical, center_horizontal etcandroid:foregroundGravity :定义要应用于前景可绘制对象的重力。 重力默认填充。 可能的值是上,下,左,右,中心,center_vertical,center_horizo​​ntal等
  • android:measureAllChildren : Determines whether to measure all children or just those in the VISIBLE or INVISIBLE state when measuring. Defaults to falseandroid:measureAllChildren :确定在测量时是测量所有子项还是仅测量VISIBLE或INVISIBLE状态的子项。 默认为false

Some important points to note:

需要注意的一些重要点:

  • FrameLayout can become more useful when elements are hidden and displayed programmatically当元素以编程方式隐藏和显示时,FrameLayout会变得更加有用
  • If the gravity is not set then the text would have appeared at the top left of the screen如果未设置重力,则文本将出现在屏幕的左上方

The xml layout is given below:

xml布局如下:

layout_frame.xml

layout_frame.xml

<FrameLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="https://schemas.android.com/apk/res/android"><ImageViewandroid:src="@android:drawable/alert_dark_frame"android:scaleType="fitCenter"android:layout_height="fill_parent"android:layout_width="fill_parent"/><TextViewandroid:text="JournalDev.com"android:textSize="24sp"android:textColor="#ffff"android:layout_height="fill_parent"android:layout_width="fill_parent"android:gravity="center"/>
</FrameLayout>

Here we’ve placed a TextView over an ImageView. Isn’t it simple when compared to a RelativeLayout!

在这里,我们在TextView上放置了TextView。 与RelativeLayout相比,这不是很简单!

Android AbsoluteLayout (Android AbsoluteLayout)

Android AbsoluteLayout is used when the UI components on screen are positioned at their absolute positions with respect to the origin at the top left corner of the layout. We need to specify the x and y coordinate position of each component on the screen. This is not recommend since it makes the UI inflexible, in fact AbsoluteLayout is deprecated now. The xml layout code below shows an AbsoluteLayout implementation.

当屏幕上的UI组件相对于布局左上角的原点位于其绝对位置时,将使用Android AbsoluteLayout 。 我们需要在屏幕上指定每个组件的x和y坐标位置。 不建议这样做,因为它会使UI变得不灵活,实际上AbsoluteLayout现在已被弃用。 下面的xml布局代码显示了AbsoluteLayout实现。

layout_absolute.xml

layout_absolute.xml

<AbsoluteLayout xmlns:android="https://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"><Buttonandroid:id="@+id/next"android:text="Next"android:layout_x="10px"android:layout_y="5px"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:layout_x="19dp"android:layout_y="74dp"android:text="First Name"android:textSize="18sp"android:layout_width="wrap_content"android:layout_height="wrap_content" /><EditTextandroid:layout_x="140dp"android:layout_y="54dp"android:width="300px"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:layout_x="22dp"android:layout_y="137dp"android:text="Last Name"android:textSize="18sp"android:layout_width="wrap_content"android:layout_height="wrap_content" /><EditTextandroid:layout_x="143dp"android:layout_y="117dp"android:width="300px"android:layout_width="wrap_content"android:layout_height="wrap_content" />
</AbsoluteLayout>

Here layout_x and layout_y attributes specify the absolute positions of the components. width attribute is used to specify the EditText width.

在这里, layout_xlayout_y属性指定组件的绝对位置。 width属性用于指定EditText的宽度。

项目结构 (Project Structure)

Android FrameLayout AbsoluteLayout代码 (Android FrameLayout AbsoluteLayout Code)

The MainActivity displays the absolute layout which consists of a Button which is used to launch the SecondActivity via intent and display a FrameLayout. There codes are given below and are self explanatory:

MainActivity显示由一个Button组成的绝对布局,该Button用于通过意图启动SecondActivity并显示一个FrameLayout。 下面给出了代码,这些代码是自解释的:

MainActivity.java

MainActivity.java

package com.journaldev.layoutsparttwo;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class MainActivity extends Activity {Button next;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.layout_absolute);next=(Button)findViewById(R.id.next);next.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent= new Intent(MainActivity.this,SecondActivity.class);startActivity(intent);}});}@Overridepublic void onBackPressed() {finish();}
}

The MainActivity overrides the onBackPressed() method to finish the activity when back is pressed.

MainActivity覆盖onBackPressed()方法以在按下后退键时完成活动。

SecondActivity.java

SecondActivity.java

package com.journaldev.layoutsparttwo;import android.app.Activity;
import android.os.Bundle;public class SecondActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.layout_frame);}@Overridepublic void onBackPressed() {super.onBackPressed();}
}

In the above code onBackPressed takes us back to the most recent activity present in the stack i.e. MainActivity.

在上面的代码中, onBackPressed将我们带回到堆栈中存在的最新活动,即MainActivity

Below is our application in action in android emulator. We have displayed an absolute layout with a predefined width of EditText and then a frame layout with a TextView placed over a drawable ImageView.

以下是我们在android模拟器中运行的应用程序。 我们已经显示了具有预定义宽度的EditText的绝对布局,然后显示了具有TextView的框架布局以及可绘制的ImageView。

This brings an end to this tutorial. You can download the Android FrameLayout AbsoluteLayout Project from the below link.

本教程到此结束。 您可以从下面的链接下载Android FrameLayout AbsoluteLayout项目

Download Android FrameLayout AbsoluteLayout Example Project下载Android FrameLayout AbsoluteLayout示例项目

翻译自: https://www.journaldev.com/9525/android-framelayout-absolutelayout-example-tutorial

Android FrameLayout和AbsoluteLayout示例教程相关推荐

  1. Android自定义操作栏示例教程

    In this tutorial we will create an app that consists of Android Custom Action Bar with a custom layo ...

  2. Android SeekBar和RatingBar示例教程

    In this tutorial we'll implement a SeekBar and a RatingBar in our android application. Before we jum ...

  3. Android基础教程之五大布局对象------FrameLayout,LinearLayout,AbsoluteLayout,RelativeLayout,TableLayout...

    2019独角兽企业重金招聘Python工程师标准>>> 大家好,我们这一节讲一下Android对用五大布局对象,它们分别是FrameLayout(框架布局:不知道是不是这么翻译的), ...

  4. Android基础教程(二)之五大布局对象---FrameLayout,LinearLayout ,AbsoluteLayout,RelativeLayout,TableLayout.

    大家好,我们这一节讲一下Android对用五大布局对象,它们分别是FrameLayout(框架布局:不知道是不是这么翻译的),LinearLayout (线性布局),AbsoluteLayout(绝对 ...

  5. android 导航抽屉_Android导航抽屉示例教程

    android 导航抽屉 In this tutorial we'll implement a Navigation Drawer in our android application. Androi ...

  6. Android ActionBar示例教程

    Today we will look into Android ActionBar. Action Bar is one of the important part of any applicatio ...

  7. Android WebView示例教程

    Android WebView is used to display HTML in an android app. We can use android WebView to load HTML p ...

  8. Android ViewPager示例教程

    ViewPager in Android allows the user to flip left and right through pages of data. In our android Vi ...

  9. android实例教程_改造Android示例教程

    android实例教程 Welcome to Retrofit Android Example Tutorial. Today we'll use the Retrofit library devel ...

最新文章

  1. 猫哥教你写爬虫 046--协程-实践-吃什么不会胖
  2. Vivado中用于时钟操作的几个Tcl命令
  3. ultraMaskedEdit使用心得
  4. 从零开始单排学设计模式「策略模式」黑铁 II
  5. python封装类在当前文件中使用_name_下调用不了_学python中对于类怎么也不明白,对了是看到简明教程11章,类与变量的对象....想要通俗易懂的答案....
  6. ubuntu安装搜狗输入法-全面版
  7. LeetCode Algorithm 118. 杨辉三角
  8. Android如何回调编码后的音视频数据
  9. python程序可以在任何安装了解释器_Python解释器新手安装教程
  10. 相机模型与标定(十二)--opencv圆形标志点检测算法
  11. 使用反射创建实例/对象的两种方法
  12. 使用jemalloc优化java_C++性能优化(十) —— JeMalloc
  13. cif t t操作流程图_操作示例:T/T+CIF+海运
  14. Phonetic symbol 单元音 - 长元音 -- ɜː (新) / ə: (旧) 与 ɔː
  15. python基础篇11-文件操作
  16. 我的2016—遇见自己,遇见未来
  17. 2020-08-15
  18. 【连载】听程序员部落酋长畅谈关于软件的人和事-节选3
  19. js实现精确统计网站访问量的代码分享
  20. web前端能做到多少岁

热门文章

  1. 什么是BI(Business Intelligence
  2. [转载] python多重继承初始化_关于python多重继承初始化问题
  3. [转载] Visual Studio 2017 VC项目设置 printf 输出到 Console 窗口调试
  4. [转载] LeetCode题解(面试16.22):兰顿蚂蚁(Python)
  5. Educational Codeforces Round 10 B. z-sort
  6. 深入理解 Linux 的 RCU 机制
  7. VC双缓冲画图技术介绍
  8. Ubuntu图标变成问号
  9. [Architecture Pattern] Entity Expansion
  10. AppServ 介绍