Android LinearLayout使用

2024-09-07 05:32
文章标签 android 使用 linearlayout

本文主要是介绍Android LinearLayout使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本例通过几个Button 来展示及操作LinearLayout的使用方法,LinearLayout线性布局 排列方式有“水平”和“垂直”,最大的特点是设定好排列方式后所包含的组件会自动安照指定排列方式排列,而不用对子组件设置座标参数,简单易用适合多个组件以水平或垂直方式排列的情况。


//LinearLayoutActivity.java

package shortcut.song.com.myapplication;import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Button;public class LinearLayoutActivity extends AppCompatActivity {LinearLayout mLinearLayout;Button mButtonH;Button mButtonV;Button mButtonLeft;Button mButtonRight;Button mButtonTop;Button mButtonBottom;Button mButtonCenter;@Override
    protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_linear_layout);mLinearLayout = (LinearLayout)findViewById(R.id.activity_linear_layout);mButtonH = (Button)findViewById(R.id.btn_horzontal);mButtonV = (Button)findViewById(R.id.btn_vertical);mButtonLeft = (Button)findViewById(R.id.linear_left);mButtonRight = (Button)findViewById(R.id.linear_right);mButtonTop = (Button)findViewById(R.id.linear_top);mButtonBottom = (Button)findViewById(R.id.linear_bottom);mButtonCenter = (Button)findViewById(R.id.linear_center);mButtonH.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {mLinearLayout.setOrientation(LinearLayout.HORIZONTAL);//水平}});mButtonV.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {mLinearLayout.setOrientation(LinearLayout.VERTICAL);//垂直}});mButtonLeft.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {mLinearLayout.setGravity(Gravity.LEFT);//靠左}});mButtonRight.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {mLinearLayout.setGravity(Gravity.RIGHT);//靠右}});mButtonTop.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {mLinearLayout.setGravity(Gravity.TOP);//顶部}});mButtonBottom.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {mLinearLayout.setGravity(Gravity.BOTTOM);//底部}});mButtonCenter.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {mLinearLayout.setGravity(Gravity.CENTER);//居中}});}
}
 
//Layout布局文件activity_linear_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="shortcut.song.com.myapplication.LinearLayoutActivity"><Button
                android:id="@+id/btn_horzontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Horzontal"
                /><Button
                android:id="@+id/btn_vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Vertical"
                /><Button
                android:id="@+id/linear_left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Left"
                /><Button
                android:id="@+id/linear_right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Right"
                /><Button
                android:id="@+id/linear_top"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Top"
                /><Button
                android:id="@+id/linear_bottom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Bottom"
                /><Button
                android:id="@+id/linear_center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Center"
             />
</LinearLayout>

 
 

这篇关于Android LinearLayout使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1144188

相关文章

Kotlin 枚举类使用举例

《Kotlin枚举类使用举例》枚举类(EnumClasses)是Kotlin中用于定义固定集合值的特殊类,它表示一组命名的常量,每个枚举常量都是该类的单例实例,接下来通过本文给大家介绍Kotl... 目录一、编程枚举类核心概念二、基础语法与特性1. 基本定义2. 带参数的枚举3. 实现接口4. 内置属性三、

Java List 使用举例(从入门到精通)

《JavaList使用举例(从入门到精通)》本文系统讲解JavaList,涵盖基础概念、核心特性、常用实现(如ArrayList、LinkedList)及性能对比,介绍创建、操作、遍历方法,结合实... 目录一、List 基础概念1.1 什么是 List?1.2 List 的核心特性1.3 List 家族成

Go语言使用Gin处理路由参数和查询参数

《Go语言使用Gin处理路由参数和查询参数》在WebAPI开发中,处理路由参数(PathParameter)和查询参数(QueryParameter)是非常常见的需求,下面我们就来看看Go语言... 目录一、路由参数 vs 查询参数二、Gin 获取路由参数和查询参数三、示例代码四、运行与测试1. 测试编程路

Python使用python-pptx自动化操作和生成PPT

《Python使用python-pptx自动化操作和生成PPT》这篇文章主要为大家详细介绍了如何使用python-pptx库实现PPT自动化,并提供实用的代码示例和应用场景,感兴趣的小伙伴可以跟随小编... 目录使用python-pptx操作PPT文档安装python-pptx基础概念创建新的PPT文档查看

C#和Unity中的中介者模式使用方式

《C#和Unity中的中介者模式使用方式》中介者模式通过中介者封装对象交互,降低耦合度,集中控制逻辑,适用于复杂系统组件交互场景,C#中可用事件、委托或MediatR实现,提升可维护性与灵活性... 目录C#中的中介者模式详解一、中介者模式的基本概念1. 定义2. 组成要素3. 模式结构二、中介者模式的特点

MySQL中优化CPU使用的详细指南

《MySQL中优化CPU使用的详细指南》优化MySQL的CPU使用可以显著提高数据库的性能和响应时间,本文为大家整理了一些优化CPU使用的方法,大家可以根据需要进行选择... 目录一、优化查询和索引1.1 优化查询语句1.2 创建和优化索引1.3 避免全表扫描二、调整mysql配置参数2.1 调整线程数2.

C#中SortedSet的具体使用

《C#中SortedSet的具体使用》SortedSet是.NETFramework4.0引入的一个泛型集合类,它实现了一个自动排序的集合,内部使用红黑树数据结构来维护元素的有序性,下面就来介绍一下如... 目录基础概念主要特性创建和初始化基本创建方式自定义比较器基本操作添加和删除元素查询操作范围查询集合运

C# Opacity 不透明度的具体使用

《C#Opacity不透明度的具体使用》本文主要介绍了C#Opacity不透明度的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录WinFormsOpacity以下是一些使用Opacity属性的示例:设置窗体的透明度:设置按钮的透

Go语言使用net/http构建一个RESTful API的示例代码

《Go语言使用net/http构建一个RESTfulAPI的示例代码》Go的标准库net/http提供了构建Web服务所需的强大功能,虽然众多第三方框架(如Gin、Echo)已经封装了很多功能,但... 目录引言一、什么是 RESTful API?二、实战目标:用户信息管理 API三、代码实现1. 用户数据

在ASP.NET项目中如何使用C#生成二维码

《在ASP.NET项目中如何使用C#生成二维码》二维码(QRCode)已广泛应用于网址分享,支付链接等场景,本文将以ASP.NET为示例,演示如何实现输入文本/URL,生成二维码,在线显示与下载的完整... 目录创建前端页面(Index.cshtml)后端二维码生成逻辑(Index.cshtml.cs)总结