Android 第七课 4种基本布局之FrameLayout和百分比布局

FrameLayout(帧布局),她没有方便的定位方式,所有的控件都会默认摆放在布局的左上角。

修改activity_main.xml中的代码,如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.uilayouttest.MainActivity">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is TextView"/>
   <ImageView
       android:id="@+id/image_view"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:src="@mipmap/ic_launcher"/>


</FrameLayout>

运行程序之后,可以看到文字和图片都是位于布局的左上角,由于ImageView是在TextView之后添加的因此图片压在了文字的上面。这是一种默认效果,除了这种情况之外,我们还可以使用layout_gravity属性来指定控件在布局中的对齐方式,这和LinearLayout中的用法是相似的,修改activity_main.xml中的代码,如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.uilayouttest.MainActivity">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:text="This is TextView"/>
   <ImageView
       android:id="@+id/image_view"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="right"
       android:src="@mipmap/ic_launcher"/>


</FrameLayout>

总的来说,FrameLayout定位方式欠缺,所以应用场景也少。


  • 百分百布局

可以发现只有LinearLayout支持使用layout_weight属性来实现按比例指定控件大小的功能,其他两种不支持。

百分百布局中,我们不再使用wrap_content、match_parent等方式来指定控件的大小,而是允许直接指定控件在布局中所占的百分比,这样的话就可以轻松实现评分布局甚至是任意比例分割布局的效果了。

LinearLayout本身已经支持按比例指定控件的大小了,因此百分比布局只为了FrameLayout和RelativeLayout进行了功能扩展,提供了PercentFrameLayout和PercentRelative这两个全新的布局。

不同于前三种,百分比布局属于新增布局,Android 团队将百分比布局定义在了support库当中,我们只需要在项目的build。gradle中添加百分比布局库的依赖。打开app/build.gradle文件,在dependencies闭包中添加了如下内容:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    compile 'com.android.support:percent:24.2.1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

接下来修改activity_main.xml中的代码,如下:


<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:text="Button 1"
        android:layout_gravity="left|top"
        app:layout_widthPercent = "%50"
        app:layout_heightPercent = "%50"/>

    <Button
        android:id="@+id/button2"
        android:text="Button 2"
        android:layout_gravity="right|top"
        app:layout_widthPercent = "%50"
        app:layout_heightPercent = "%50"/>

    <Button
        android:id="@+id/button3"
        android:text="Button 3"
        android:layout_gravity="left|bottom"
        app:layout_widthPercent = "%50"
        app:layout_heightPercent = "%50"/>

    <Button
        android:id="@+id/button4"
        android:text="Button 4"
        android:layout_gravity="left|bottom"
        app:layout_widthPercent = "%50"
        app:layout_heightPercent = "%50"/>



</android.support.percent.PercentFrameLayout>




最外层我们使用了PercentFrameLayout,由于百分比布局并不是内置在系统SDK当中的,所有需要把完整的包路径写出来。然后还必须定义一个app的命名空间,这样才能使用 百分比布局的自定义属性。

在PercentFrameLayout中我们定义了4个按钮,使用app:layout_widthPercent属性将各按钮的宽度指定为布局的50%,

使用app:layout_heightPercent属性将各按钮的高度指定为布局的50%,不过PercentFrameLayout还是会继承FrameLayout的特性,即所有的控件默认摆放在布局的左上角。为了不让布局重叠,我们还是借助了layout_gravity来分别将这4个按钮放置在布局的左上,右上,左下,右下4个位置。

运行程序如图:

(待续。。。)

PercenFrameLayout就介绍到这里,还有一个PercentRelativeLayout的用法也是非常相似的。它继承了RelativeLayout等布局。






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<h3>回答1:</h3><br/>Android中的FrameLayout布局是一种简单的布局方式,它允许在同一屏幕上放置多个控件,这些控件可以重叠在一起。FrameLayout布局的特点是:控件可以放置在任何位置,但是它们的大小和位置都是相对于FrameLayout的大小和位置而言的。在FrameLayout中,后添加的控件会覆盖先添加的控件。因此,FrameLayout常用于实现一些简单的界面效果,如图片轮播、广告展示等。 <h3>回答2:</h3><br/>Android中的Framelayout布局是从根据子视图的层次结构绘制学习图像布局。与其他布局不同,它允许多个子视图重叠在同一个位置上。Framelayout布局通常用于包含多个视图的复杂用户界面,其中每个视图叠加到下一个视图上。一个常见的例子是使用Framelayout布局创建引导屏幕和体验界面。 Framelayout布局还可以在屏幕的不同部分显示不同的内容。例如,如果你想在一个页面上显示一个照片和一个文本框,你可以使用Framelayout布局将照片放在顶部,然后将文本框放在底部,从而将两者分别放在屏幕的顶部和底部。 Framelayout布局非常适合在Android应用中使用,因为它允许不同的视图叠加在一起,从而创建优美且动态的用户界面。它也非常灵活,可以与其他布局组合使用,例如linearlayout和relativelayout,以创建更复杂的UI。 使用Framelayout布局时,我们需要注意重叠的顺序,确保每个子视图的可见性,以及确保每个子视图都有正确的大小和位置。我非常喜欢使用Framelayout布局来构建Android应用的复杂用户界面,它为我们提供了一个强大而灵活的工具,使我们能够创建动态而美丽的UI。 <h3>回答3:</h3><br/>Android布局通常由LinearLayout,RelativeLayout,GridLayout和FrameLayout组成。其中,FrameLayout是最常用的布局之一。FrameLayout是一个布局类,允许您将多个子视图放置在一个单独的视图中,并使用简单的轻量级方式来控制它们的位置和大小。 FrameLayout是一个非常简单的布局,它只是在屏幕上按照子视图排列,没有其他布局,它的根元素是FrameLayoutFrameLayout的特点: 1. FrameLayout可以让我们在同一个屏幕上用多个视图叠放在一起。 2. FrameLayout只会使用最上层的视图,也就是覆盖上面的视图。 3. FrameLayout的每一个子视图都是按照原始大小排列在屏幕上的,并且它们的位置是相对于FrameLayout的左上角而言的。 4. FrameLayout适用于一些小组件布局,比如“确定”和“取消”按钮、气泡等。 FrameLayout的使用方法: 1. 在Activity的XML文件中,建立一个FrameLayout标签。 2. 在FrameLayout标签中添加子视图。 3. 使用android:layout_gravity属性设置子视图的位置和对齐方式。 例如:如果您想让一个ImageView占据整个屏幕并覆盖其他视图,则可以使用如下代码实现: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/background_image" /> </FrameLayout> 上述代码创建了一个FrameLayout,并在其中包含了一个ImageView视图。ImageView的布局宽度和高度都设置为match_parent,这意味着它将占据整个屏幕的宽度和高度。因此,既可以显示“background_image”图片,也可以覆盖其他视图。 总之,FrameLayout是一种非常常用的布局,在Android应用程序中可以发挥重要作用。您可以使用它的轻量级方式进行布局,以使您的应用程序更加高效、灵活和简单。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值