转载请注明出处amoscxy的博客:https://mp.youkuaiyun.com/mdeditor/80149067
基本布局之 - FrameLayout
FrameLayout又称作帧布局,这种布局没有方便的定位方式,所有控件都会默认放在布局左上角
1.1 默认效果
最终视图
实例目录
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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之后添加的,因此图片压在了文字的上面
1.2 使用layout_gravity属性来指定控件在布局中的对齐方式
我们还可以使用layout_gravity属性来指定控件在布局中的对齐方式,这和LinearLayout中的用法是相似的
最终效果
修改activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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>
我们指定TextView在FrameLayout中居左对齐,指定ImageView在FrameLayout中居右对齐
总的来讲,FrameLayout由于定位方式的缺失,导致它的应用场景比较少
转载请注明出处amoscxy的博客:https://mp.youkuaiyun.com/mdeditor/80149067