广告
返回顶部
首页 > 资讯 > 移动开发 >Android中LinearLayout布局的常用属性总结
  • 776
分享到

Android中LinearLayout布局的常用属性总结

属性Android 2022-06-06 08:06:42 776人浏览 独家记忆
摘要

基本属性要求 <LinearLayout Android:layout_width="match_parent" android:layout_height=

基本属性要求


<LinearLayout
  Android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
 </LinearLayout>
android:orientation 决定是水平排列或是垂直排列 vertical 垂直排列 horizontal 水平排列

垂直排列 Button


<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1" />
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 2" />
</LinearLayout>

水平排列 Button


<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1" />
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 2" />
</LinearLayout>

重心设定


<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="left">
</LinearLayout>
android:gravity 设定框架的内容的放置方向 center 水平垂直皆置中 center_vertical 垂直置中 center_horizontal 水平置中 top 置顶 left 置左 bottom 置底 right 置右

水平、垂直置中


<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="center_vertical">
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1" />
</LinearLayout>
<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="center_horizontal">
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1" />
</LinearLayout>
<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="center">
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1" />
</LinearLayout>

透过 OR 运算子组合重心


<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="top|right">
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1" />
</LinearLayout>
<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="bottom|left">
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1" />
</LinearLayout>
<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="center_vertical|center_horizontal">
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1" />
</LinearLayout>

比例分配


<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal">
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1"
    android:layout_weight="1"/>
</LinearLayout>
android:layout_weight 子元件或子框架的比重。 LinearLayout 下的子元件或子框架,才能设定这项属性。

等比例分配


<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal">
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1"
    android:layout_weight="1"/>
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 2"
    android:layout_weight="1"/>
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 3"
    android:layout_weight="1"/>
</LinearLayout>

比重都是 1,所以大小相同。


非等比例分配


<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal">
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1"
    android:layout_weight=".10"/>
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 2"
    android:layout_weight=".20"/>
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 3"
    android:layout_weight=".70"/>
</LinearLayout>

.10 代表 0.10
.20 代表 0.20
.70 代表 0.70
合起来刚好是 1 ,作 100% 分配。      

您可能感兴趣的文章:android动态加载布局文件示例android Activity相对布局的使用方法Android制作漂亮自适布局键盘的方法Android动态添加设置布局与控件的方法Android属性动画实现布局的下拉展开效果android动态布局之动态加入TextView和ListView的方法Android最简单的状态切换布局实现教程


--结束END--

本文标题: Android中LinearLayout布局的常用属性总结

本文链接: https://www.lsjlt.com/news/25286.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作