Android FrameLayout和AbsoluteLayout示例教程

Android FrameLayout和AbsoluteLayout示例教程Inthistutorialwe’lldiveintoAndroidFrameLayoutandAndroidAbsoluteLayout.Thisisthesecondtutorialinthelayoutseries,earlierwelookedintoAndroidLinearLayoutandRelativeLayoutexampl…

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 layout

    android: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 etc

    android: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 false

    android: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

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="https://schemas.android.com/apk/res/android">
    <ImageView
        android:src="@android:drawable/alert_dark_frame"
        android:scaleType="fitCenter"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"/>
    <TextView
        android: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">
    <Button
        android:id="@+id/next"
        android:text="Next"
        android:layout_x="10px"
        android:layout_y="5px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_x="19dp"
        android:layout_y="74dp"
        android:text="First Name"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_x="140dp"
        android:layout_y="54dp"
        android:width="300px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_x="22dp"
        android:layout_y="137dp"
        android:text="Last Name"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android: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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_absolute);

        next=(Button)findViewById(R.id.next);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent= new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent);
            }
        });

    }
    
    @Override
    public 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 {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_frame);

    }

    @Override
    public 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-framelayout-example

以下是我们在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项目

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/37880.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注