Android超详细讲解组件LinearLayout的使用
作者:小皮猪 发布时间:2023-11-01 01:04:27
标签:Android,LinearLayout
概述
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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://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
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;
}
}
}
实现效果:
来源:https://blog.csdn.net/m0_56233309/article/details/123491720


猜你喜欢
- cookie和session的比较一、对于cookie:①cookie是创建于服务器端②cookie保存在浏览器端③cookie的生命周期可
- 上移动端的测试课,老师和同学们用的都是eclipse, 只有我一个人用的是idea(用了两款软件之后觉得IDEA更好),真的太难了,配置
- 工厂方法模式,往往是设计模式初学者入门的模式,的确,有人称之为最为典型最具启发效果的模式。android中用到了太多的工厂类,其中有用工厂方
- 概述Spring针对Java Transaction API (JTA)、JDBC、Hibernate和Java Persistence A
- GradientTextViewGithub点我一个非常好用的库,使用kotlin实现,用于设置TexView的字体 渐变颜色、渐变方向 和
- 什么是依赖注入首先,某个类的成员变量称为依赖,如若此变量想要实例化引用其类的方法,可以通过构造函数传参或者通过某个方法获取对象,此等通过外部
- 事件的声明和使用与代理有很密切的关系,事件其实是一个或多个方法的代理,当对象的某个状态发生了变化,代理会被自动调用,从而代理的方法就被自动执
- 前言作为一个后端程序员,网络连接这块是一个绕不过的砍,当你在做服务器优化的时候,网络优化也是其中一环,那么作为网络连接中最基础的部分-TCP
- 1、导入资源2、JSP代码<div class="page-container">  
- java进行时间转换成unix timestamp的具体代码,供大家参考,具体内容如下import java.text.DateFormat
- 我们在C#编程中常见的信息提示框(MessageBox)是微软NET自带的一个用于弹出警告、错误或者讯息一类的“模式”对话框。此类对话框一旦
- @Transactional是我们在用Spring时候几乎逃不掉的一个注解,该注解主要用来声明事务。它的实现原理是通过Spring AOP在
- 曾经有一个朋友问过我一个问题, 一张512*512 150KB PNG格式图片和一张512*512 100KB 压缩比是8的JP
- 首先声明本文是基于GitHub上"baoyongzhang"的SwipeMenuListView修改而来,该项目地址:h
- 本文实例讲述了java简单列出文件夹下所有文件的方法。分享给大家供大家参考,具体如下:import Java.io.*;public cla
- 通过VideoView播放视频的步骤:1、在界面布局文件中定义VideoView组件,或在程序中创建VideoView组件2、调用Video
- 本次主要分享的是3个免费的二维码接口的对接代码和测试得出的注意点及区别,有更好处理方式多多交流,相互促进进步;最近在学习JavsScript
- 首先引入依赖 implementation 'com.github.bumptech.glide:glid
- 本文实例为大家分享了Unity3D Shader实现扫描显示的具体代码,供大家参考,具体内容如下通过Shader实现,从左向右的扫描显示,可
- 一、二维数组进入正题之前.首先为了便于大家理解,我画了一个图:xx枪战游戏中, 我是一个刚刚注册账号的小白,系统送了我两把枪,此时,我的武器