关于FrameLayout布局的位置问题

 

1.首先来看看android:layout_gravity和android:gravity的使用区别。

android:gravity:

这个是针对控件里的元素来说的,用来控制元素在该控件里的显示位置。例如,在一个Button按钮控件中设置如下两个属性,

android:gravity="left"和android:text="提交",这时Button上的文字“提交”将会位于Button的左部。

 

android:layout_gravity:

这个是针对控件本身而言,用来控制该控件在包含该控件的父控件中的位置。同样,当我们在Button按钮控件中设置android:layout_gravity="left"属性时,表示该Button按钮将位于界面的左部。

关于FrameLayout布局的位置问题_第1张图片

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical">

 

    <Button

       android:layout_width="120dp"

       android:layout_height="120dp"

       android:gravity="right"

       android:background="#123456"

       android:text="Button1"/>

    <Button

       android:layout_width="120dp"

       android:layout_height="120dp"

       android:layout_gravity="right"

       android:background="#123456"

       android:text="Button2" />

 

LinearLayout>

3.特殊情况

当我们采用LinearLayout布局时,有以下特殊情况需要我们注意:

(1)当android:orientation="vertical"  时,android:layout_gravity只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal是生效的。

(2)当android:orientation="horizontal" 时,android:layout_gravity只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。

 

接下来讲一下framelayout,FrameLayout是最简单的布局了,所有放在布局里的控件,都按照层次堆叠在屏幕的左上角。后加进来的控件覆盖前面的控件。

  关于FrameLayout布局的位置问题_第2张图片

<?xml version="1.0"encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent">

 

        <Button

            android:layout_width="180dp"

            android:layout_height="180dp"

            android:background="#ffff00"

            android:text="Button1"/>

 

        <Button

            android:layout_width="120dp"

            android:layout_height="120dp"

            android:background="#123456"

            android:text="Button2"/>

        <Button

            android:layout_width="60dp"

            android:layout_height="60dp"

            android:background="#ff0000"

            android:text="Button3"/>

    FrameLayout>

如何让这三个button不是重叠在一起的而是错开的?

我们来添加一个属性android:layout_gravity="bottom"android:gravity="right"

 

关于FrameLayout布局的位置问题_第3张图片

<Button

        android:layout_width="120dp"

        android:layout_height="120dp"

        android:background="#123456"

        android:gravity="right"

        android:text="Button2"/>

 

    <Button

        android:layout_width="60dp"

        android:layout_height="60dp"

        android:layout_gravity="bottom"

        android:background="#ff0000"

        android:text="Button3"/>

 

这里我的做出了两个效果,一个是字Button在控件的内部位置变化,一个是相对于父控件的位置变化的红色Button

android:layout_gravity:

这个是针对控件本身而言,用来控制该控件在包含该控件的父控件中的位置。

android:gravity:

这个是针对控件里的元素来说的,用来控制元素在该控件里的显示位置

那么如何让红色button即在下面,又居中呢?

还是通过

android:layout_gravity="center_horizontal|bottom"

 

关于FrameLayout布局的位置问题_第4张图片

当你需要自己写多个View的时候,在View里面已经完成了你的布局,那么这些View只需要一个容器放置,就可以使用FrameLayout了。虽然用其他的布局也可以,但是用最简单的不是更省系统资源么。

举个例子

Viewpager的时候可能需要制作带有小圆点的滑动引导界面,那么小圆点该如何布置在

在图片或者说是button上呢?

关于FrameLayout布局的位置问题_第5张图片

    <FrameLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent">

 

        <android.support.v4.view.ViewPager

            android:id="@+id/viewPager"

            android:layout_width="match_parent"

            android:layout_height="match_parent"/>

 

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="35dip"

           

            android:layout_gravity="bottom"

            android:gravity="center"

             >

                <View

                    android:id="@+id/dot_1"

                    android:layout_width="10dip"

                    android:layout_height="10dip"

                    android:layout_marginLeft="2dip"

                    android:layout_marginRight="2dip"

                    android:background="@drawable/dot_normal"/>

 

                <View

                    android:id="@+id/dot_2"

                    android:layout_width="10dip"

                    android:layout_height="10dip"

                    android:layout_marginLeft="2dip"

                    android:layout_marginRight="2dip"

                    android:background="@drawable/dot_normal"/>

 

                <View

                    android:id="@+id/dot_3"

                    android:layout_width="10dip"

                    android:layout_height="10dip"

                    android:layout_marginLeft="2dip"

                    android:layout_marginRight="2dip"

                    android:background="@drawable/dot_normal"/>

            LinearLayout>

 

    FrameLayout>

 

 

 

 

更多相关文章

  1. 【Android开发学习25】界面布局之相对布局RelativeLayout
  2. Android布局优化(二)优雅获取界面布局耗时
  3. RelativeLayout布局的对齐属性
  4. android控件的对齐方式详解
  5. Android中以JAR形式封装控件 或者类库
  6. Android布局优化(五)绘制优化—避免过度绘制
  7. Android 线性布局详解
  8. Android的相对布局属性的解释

随机推荐

  1. Android(安卓)Studio升级后编辑local pat
  2. Android-Storage-System
  3. Android(安卓)studio中logcat时间与真实
  4. android从fragment进入activity再返回实
  5. android基础知识06:intent和intentfilter
  6. Android(安卓)众多的布局属性详解
  7. 基于Appium的Android自动化测试(一)
  8. kotlin配置
  9. 3G手机 。Android(安卓)。Broncho
  10. Android(安卓)N中不再支持“Crypto”的解