LinearLayout中的layout

发布时间:2024-11-26 20:16

在Sketch中使用约束工具自动调整元素大小和位置:'Object' > 'Pin' > 'Auto Layout' #生活技巧# #数码产品使用技巧# #设计软件使用技巧#

最新推荐文章于 2022-06-12 19:38:05 发布

zhuhai__yizhi 于 2015-12-04 17:25:38 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

<RelativeLayout 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"

tools:context=".MainActivity">

<LinearLayout

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_centerInParent="true"

android:background="#999999"

android:orientation="horizontal">

<View

android:layout_width="50dp"

android:layout_height="50dp"

android:layout_gravity="center"

android:background="#ff0000" />

</LinearLayout>

</RelativeLayout>


可以看到我们这里的View并没有处在中央的位置,原因是因为LinearLayout中的horizontal属性的影响。

<RelativeLayout 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"

tools:context=".MainActivity">

<LinearLayout

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_centerInParent="true"

android:background="#999999"

android:orientation="horizontal">

<View

android:layout_width="match_parent"

android:layout_height="50dp"

android:layout_gravity="center"

android:background="#ff0000" />

</LinearLayout>

</RelativeLayout>

那么如果想在LinearLayout中的layout_gravity="center"子控件居中怎么办呢?

<RelativeLayout 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"

tools:context=".MainActivity">

<LinearLayout

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_centerInParent="true"

android:background="#999999"

android:orientation="vertical">

<LinearLayout

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:layout_gravity="center"

android:orientation="horizontal">

<View

android:layout_width="50dp"

android:layout_height="50dp"

android:layout_gravity="center"

android:background="#ff0000" />

</LinearLayout>

</LinearLayout>

</RelativeLayout>


总结:LinearLayout中的orientation会对layout_gravity="center"有影响。

vertical影响垂直居中;

horizontal影响水平居中;

也可以参考这里:

http://stackoverflow.com/questions/12149316/android-layout-gravity-center-does-not-center

网址:LinearLayout中的layout https://www.yuejiaxmz.com/news/view/282589

相关内容

Android 天气APP(五)天气预报、生活指数的数据请求与渲染
如何在android中实现阴影(环境光)背景?
基于Android studio 的个人健康管理系统
Android 组件的抽屉(初级)
ViewPager不能高度自适应?height=wrap
Android操作系统中的日程管理:用Java编写高效时间规划应用
高德地图教程
STM32智能家居项目设计
洗碗机的热风烘干 新风系统=?洗烘存一体好不好用?看完实测再下结论!
面向生活圈空间绩效的社区公共绿地布局 优化——基于上海中心城区的实证研究

随便看看