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"> <TextViewandroid:id="@+id/text_view"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="This is TextView"/><ImageViewandroid: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"><TextViewandroid:id="@+id/text_view"android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_gravity="left"android:text="This is TextView"/><ImageViewandroid: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.PercentFrameLayoutxmlns: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"> <Buttonandroid:id="@+id/button1"android:text="Button 1"android:layout_gravity="left|top"app:layout_widthPercent = "%50"app:layout_heightPercent = "%50"/><Buttonandroid:id="@+id/button2"android:text="Button 2"android:layout_gravity="right|top"app:layout_widthPercent = "%50"app:layout_heightPercent = "%50"/><Buttonandroid:id="@+id/button3"android:text="Button 3"android:layout_gravity="left|bottom"app:layout_widthPercent = "%50"app:layout_heightPercent = "%50"/><Buttonandroid: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等布局。






本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/274122.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mongodb 群集图_群集和重叠条形图

mongodb 群集图为什么和如何 (Why & How) 1.- Clustered Bar Charts1.- 集群条形图 AKA: grouped, side-by-side, multiset [bar charts, bar graphs, column charts]AKA &#xff1a;分组&#xff0c;并排&#xff0c;多组[条形图&#xff0c;条形图&#xff0c;柱形图] …

第一次写python

这是一个在BJDP上学习Coding Kata的时候用到的一个练习&#xff0c;原来打算用Java写的&#xff0c;但是一想正好是学习的好机会。 就用Python了。第一次&#xff0c;写的有些复杂。 这个题目是关于购买图书的打折信息的。 题目来源&#xff1a; http://codingdojo.org/cgi-bin…

Android 第八课 创建自定义控件

常用控件和布局的继承结构&#xff0c;如下图&#xff1a; &#xff08;待续。。。。&#xff09; 所有的控件都是直接或间接继承自View的&#xff0c;所用的所有布局都是直接或间接继承自ViewGroup的&#xff0c;View是Android中最基本的一种UI组件&#xff0c;它可以在屏幕上…

figma下载_搬到Figma对我意味着什么

figma下载A couple of years ago, amidst the boom of new design and prototyping software, I was pretty reluctant to fight on the Figma/Sketch cold war. I was working on a relatively small design team and, after years helping to design products, well sold on …

解决IE中img.onload失效的方法

解决IE中img.onload失效的方法 - CoffeeCats IT Blog - IT博客http://www.cnitblog.com/CoffeeCat/archive/2008/02/01/39533.htmlFirefox、Google Chrome不存在问题&#xff01;为什么onload没有被IE调用呢&#xff1f;因为IE会缓存图片&#xff0c;第2次加载的图片&#xff0…

Android 第九课 常用控件-------ListView

ListView允许用户通过手指上下滑动的方式将屏幕外的数据滚动到屏幕内&#xff0c;同时屏幕上原有数据将会滚动出屏幕。 1、ListView简单用法 如何将ListView将你要显示的大量内容关联起来呢&#xff1f;这是个很重要的问题。 1、首先我们必须先将数据提供好&#xff0c;因为你的…

Singleton patterns 单件(创建型模式)

1、模式分类 1.1 从目的来看&#xff1a; • – 创建型&#xff08;Creational&#xff09;模式&#xff1a;负责对象创建。 • – 结构型&#xff08;Structural&#xff09;模式&#xff1a;处理类与对象间的组合。 • – 行为型&#xff08;Behavioral&…

Android 第十一课 SQlite 数据库存储

Android 为了让我们能够更加方便的管理数据库&#xff0c;特意提供了一个SQLiteOpenHelper帮助类&#xff0c;通过借助这个类就可以非常简单的对数据库进行创建和升级。 SQLiteOpenHelper是一个抽象类&#xff0c;我们要创建一个自己的帮助类去继承它。SQLiteOpenHelper有两个抽…

浅析SQL Server 2005中的主动式通知机制

一、引言 在开发多人同时访问的Web应用程序&#xff08;其实不只这类程序&#xff09;时&#xff0c;开发人员往往会在缓存策略的设计上狠下功夫。这是因为&#xff0c;如果将这种环境下不常变更的数据临时存放在应用程序服务器或是用户机器上的话&#xff0c;可以避免频繁地往…

Android 第十二课 使用LitePal操作数据库(记得阅读最后面的注意事项哦)

一、LitePal简介 1、(新建项目LitePalTest)正式接触第一个开源库---LitePalLitePal是一款开源的Android 数据库框架&#xff0c;它采用了对象关系映射&#xff08;ORM&#xff09;的模式。2、配置LitePal&#xff0c;编辑app/build.gradle文件&#xff0c;在dependencies闭包中…

listview频繁刷新报错

在Android编程中使用Adapter时&#xff0c;偶尔会出现如下错误&#xff1a;The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI t…

Android 第十三课 SharedPreferences存储

SharedPreferences是使用键值对的方式来存储数据的。当保存一条数据时&#xff0c;需要给这条数据提供一个对应的键&#xff0c;这样在读取数据的时候就可以通过这个键把相应的值取出来。而且支SharedPreferences还支持多种不同的数据类型存储&#xff0c;例如&#xff1a;如果…

DSP的Gel作用

转自&#xff1a;http://blog.csdn.net/azhgul/article/details/6660960最近刚在研究Davinci系&#xff0c;特此MARK下&#xff0c;以资后续学习之用。 DSP的Gel作用 1 GEL文件基本作用 当CCSStudio启动时&#xff0c;GEL文件加载到PC机的内存中&#xff0c;如果定义了StartUp(…

解决关于登录校园网显示不在IP段的问题方案(要看注意事项哦!)

有时&#xff0c;登录校园网&#xff0c;账号和密码都显示正确&#xff0c;但是却显示出“账号只能在指定IP段登录”的问题。 那我们就提供了一个解决方案&#xff1a; 使用WinR,并在输入框&#xff0c;输入cmd命令&#xff1a;&#xff08;如下&#xff09;接着输入&#xff1…

jquery插件编写

jQuery为开发插件提拱了两个方法&#xff0c;分别是&#xff1a; jQuery.fn.extend(object); jQuery.extend(object); jQuery.extend(object); 为扩展jQuery类本身.为类添加新的方法。可以理解为添加静态方法。是全局的&#xff08;位于jQuery命名空间内部的函数&#xff09;…

gtk/Glade编程 编译命令不成功 解决方法

摘自&#xff1a;http://blog.chinaunix.net/uid-26746982-id-3433656.html 当我们编写gtk/glade程序&#xff0c;gcc编译时&#xff0c;用如下命令&#xff1a; #gcc -o server server.c pkg-config --cflags --libs gtk-2.0 报错&#xff1a;/tmp/ccoXadAd.o: In function …

Android 第十五课 如何使用LitePal从SQLite数据库中删除数据(十四课用来保留讲解如何向SQLite数据库中存入数据)

使用LitePal删除数据的方式主要有两种&#xff0c;第一种就是直接调用已存对象的delete()方法&#xff0c;所谓已存储对象就是调用过save()方法的对象&#xff0c;或者说是通过LitePal提供的查询API查出来的对象&#xff0c;都是可以直接使用delete方法来删除对象的。这是比较简…

页面返回顶部(方法比较)

下面就说下简单的返回顶部效果的代码实现&#xff0c;附注释说明。 1. 最简单的静态返回顶部&#xff0c;点击直接跳转页面顶部&#xff0c;常见于固定放置在页面底部返回顶部功能 方法一&#xff1a;用命名锚点击返回到顶部预设的id为top的元素 html代码 <a href"#top…

Android 第十六课 使用LitePal查询数据

LitePal在查询API方面做了非常多的优化&#xff0c;基本上可以满足绝大多数场景的查询需求&#xff0c;并且代码也十分整洁。 例如我们需要查询表中的所有数据&#xff1a; List<books> DataSupport.findAll(Book.class); 没有冗长的参数列表&#xff0c;只需要调用一下…

linux创建桌面图标,和开始菜单栏图标

转自&#xff1a;http://blog.csdn.net/qq_25773973/article/details/50514767 ###环境&#xff1a;Mint17&#xff0c;&#xff08;其他类似的linux系统是一样的&#xff09; 如果开始菜单有图标&#xff0c;创建桌面图标很简单&#xff0c;右键添加到桌面即可。 如果没有&am…