Android FrameLayout子view居中(左居中,右居中)等

简介: Android的布局FrameLayout默认是把布局内的子view堆砌在左上角,但是,可以通过设置子view的:android:layout_gravity此参数控制子view的布局位置,实现FrameLayou...

Android的布局FrameLayout默认是把布局内的子view堆砌在左上角,但是,可以通过设置子view的:

android:layout_gravity

此参数控制子view的布局位置,实现FrameLayout的各种子view布局位置,如左居中,右居中,居中等等,要点在android:layout_gravity的参数设置中组合使用“center”、“left”、“right”等,现在给出XML代码实例布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/black"
        android:text="左上角"
        android:textColor="@android:color/white" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center"
        android:background="@android:color/black"
        android:text="左居中"
        android:textColor="@android:color/white" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:background="@android:color/black"
        android:text="右上角"
        android:textColor="@android:color/white" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center"
        android:background="@android:color/black"
        android:text="右居中"
        android:textColor="@android:color/white" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@android:color/black"
        android:text="居中"
        android:textColor="@android:color/white" />

</FrameLayout>

运行结果如图所示:

相关文章
|
3月前
|
Android开发 容器
Android UI设计: 什么是View和ViewGroup?
Android UI设计: 什么是View和ViewGroup?
36 0
|
4月前
|
XML API Android开发
Android 自定义View 之 圆环进度条
Android 自定义View 之 圆环进度条
|
6月前
|
SQL 人工智能 移动开发
Android 遍历界面所有的View
本文讲述如何遍历获取页面中所有的view,并输出对应的id,textview文本内容,imageview实际大小及设置的图片大小。 可用于检测android应用中的大图。
|
4月前
|
XML API Android开发
Android 自定义View 之 Dialog弹窗
Android 自定义View 之 Dialog弹窗
|
4月前
|
XML API Android开发
Android 自定义View 之 饼状进度条
Android 自定义View 之 饼状进度条
|
4月前
|
XML API Android开发
Android 自定义View 之 简易输入框
Android 自定义View 之 简易输入框
|
4月前
|
XML API Android开发
Android 自定义View 之 计时文字
Android 自定义View 之 计时文字
|
4月前
|
XML Android开发 数据格式
Android 自定义View 之 Mac地址输入框(下)
Android 自定义View 之 Mac地址输入框(下)
|
4月前
|
XML Java Android开发
Android 自定义View 之 Mac地址输入框(上)
Android 自定义View 之 Mac地址输入框(上)
|
4月前
|
Android开发 容器
[Android]View的事件分发机制(源码解析)
[Android]View的事件分发机制(源码解析)
36 0