android开发分享Android超详细讲解组件LinearLayout的使用

概述linearlayout是线性布局组件,放置在其中的组件按列或者按行(就是垂直或者水平)的方式排序分布。常用xml配置属性(1)android:orientation设置linearlayout容

上述就是android开发分享Android超详细讲解组件LinearLayout的使用的全部内容,如果对大家有所用处且需要了解更多关于Android学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

概述

linearlayout是线性布局组件,放置在其中的组件按列或者按行(就是垂直或者水平)的方式排序分布。

常用xml配置属性

(1) android:orientation

设置linearlayout容器布局组件的方式:只能取值:horizontal(水平的),vertical(垂直的)

(2) android:gravity

设置布局在linearlayout容器内的组件对齐方式。

取值包括top, bottom, left, right, center, start, end(上,下,左,右,中,开始,结束)

(3) view中继承来的属性

(包括android:background ,android:visibility等。还有一些改善美观的放置组件的间隔)

1. android:layout_width和android:layout_height (match_parent/wrap_content)

2 .android:layout_gravity   设置组件在容器中的布局 

3. android:layout_weight  设置组件占用空间的空余显示空间的比列

4. android:layout_margin  ,android:layout_margintop  ,android:layout_marginbottom  ,android:layout_marginleft  ,android:layout_marginright 设置组件的外边界,类似我们搞网页设计html/css中margin用法。

代码举例

activity_main.xml

Android超详细讲解组件LinearLayout的使用

Android超详细讲解组件LinearLayout的使用

<?xml version="1.0" encoding="utf-8"?>  <linearlayout xmlns:android="https://schemas.android.com/apk/res/android"      xmlns:app="https://schemas.android.com/apk/res-auto"      xmlns:tools="https://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:orientation="vertical">      <radiogroup          android:id="@+id/orientation"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:orientation="horizontal"          android:padding="10dp"          >          <radiobutton              android:id="@+id/horizontal"              android:layout_width="wrap_content"              android:layout_height="match_parent"              android:text="水平"              android:textsize="30dp"              />          <radiobutton              android:id="@+id/vertical"              android:layout_width="wrap_content"              android:layout_height="match_parent"              android:text="垂直"              android:textsize="30dp"              />      </radiogroup>         <radiogroup          android:id="@+id/gravity"          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:orientation="vertical"          android:padding="10dp"          >          <radiobutton              android:id="@+id/left"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text="居左"              android:textsize="30dp"              />          <radiobutton              android:id="@+id/center"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text="居中"              android:textsize="30dp"              />          <radiobutton              android:id="@+id/right"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text="居右"              android:textsize="30dp"              />               </radiogroup>     </linearlayout>

mainactivity.java

Android超详细讲解组件LinearLayout的使用

package com.example.android_demo02;     import androidx.appcompat.app.appcompatactivity;     import android.os.bundle;  import android.view.gravity;  import android.widget.linearlayout;  import android.widget.radiogroup;     public class mainactivity extends appcompatactivity implements radiogroup.oncheckedchangelistener {      private radiogroup orientation;      private radiogroup gravity;      @override      protected void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);          setcontentview(r.layout.activity_main);          orientation=(radiogroup) findviewbyid(r.id.orientation);          orientation.setoncheckedchangelistener(this);          gravity=(radiogroup) findviewbyid(r.id.gravity);          gravity.setoncheckedchangelistener(this);      }         @override      public void oncheckedchanged(radiogroup radiogroup, int i) {          switch (i){              case r.id.horizontal:                  orientation.setorientation(linearlayout.horizontal);                  break;              case r.id.vertical:                  orientation.setorientation(linearlayout.vertical);                  break;              case r.id.left:                  gravity.setgravity(gravity.start);                  break;              case r.id.center:                  gravity.setgravity(gravity.center_horizontal);                  break;              case r.id.right:                  gravity.setgravity(gravity.end);                  break;          }      }     }

实现效果:

Android超详细讲解组件LinearLayout的使用

Android超详细讲解组件LinearLayout的使用

到此这篇关于android超详细讲解组件linearlayout的使用的文章就介绍到这了,更多相关android linearlayout内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/addevelopment/1069957.html

(0)
上一篇 2022年3月31日
下一篇 2022年3月31日

精彩推荐