简单解释一下android 中layout_gravity和gravity_华清远见
- 格式:docx
- 大小:36.86 KB
- 文档页数:3
Android组件---四⼤布局的属性详解【声明】欢迎转载,但请保留⽂章原始出处→_→Android常见布局有下⾯⼏种:LinearLayout:线性布局。
所有的控件都是串在⼀条线上的。
RelativeLayout:相对布局。
所有的控件的位置,都是相对于⽗控件的。
FrameLayout:帧布局。
FrameLayout布局中的控件都是⼀层⼀层的。
帧布局每次添加的控件都显⽰在最上⾯,最后显⽰在界⾯上的是最后添加的⼀个控件。
TableLayout:表格布局。
表格布局可以实现的.⼀般可以使⽤线性布局实现。
AbsoluteLayout:绝对布局。
已经是废弃的状态,很少⽤了。
orientation:属性是指定线性布局的排列⽅向。
horizontal ⽔平。
线性布局默认的朝向是⽔平的。
vertical 垂直例如:android:orientation="vertical"gravity:指定当前控件⾥⾯的内容容显⽰位置。
(四⼤layout中均可使⽤)left 左边right 右边top 上边bottom 底边例如:android:gravity="center"gravity中的属性可以组合使⽤。
例如:android:gravity="bottom|right"layout_gravity:指定当前控件在⽗元素的位置。
(只在 LinearLayout 和 FrameLayout 中有效)left 左边right 右边top 上边bottom 底边centercenter_horizontalcenter_vertical例如:android:layout_gravity="center"另外,需要提⽰的是,对于 LinearLayout :当 android:orientation="vertical" 时,只有⽔平⽅向的设置才起作⽤,垂直⽅向的设置不起作⽤。
1.gravity 这个英文单词是重心的意思,在这里就表示停靠位置的意思。
android:layout_gravity 和android:gravity 的区别从名字上可以看到,android:gravity是对元素本身说的,元素本身的文本显示在什么地方靠着换个属性设置,不过不设置默认是在左侧的。
android:layout_gravity是相对与它的父元素说的,说明元素显示在父元素的什么位置。
比如说button:android:layout_gravity 表示按钮在界面上的位置。
android:gravity表示button上的字在button上的位置。
可选值这两个属性可选的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。
而且这些属性是可以多选的,用“|”分开。
默认这个的值是:Gravity.LEFT对这些属性的描述:出自:/guide/topics/resources/drawable-res ource.html/reference/android/graphics/drawable /ClipDrawable.htmlValue Descriptiontop Put the object at the top of its container, not changing its size.将对象放在其容器的顶部,不改变其大小.bottom Put the object at the bottom of its container, not changing its size.将对象放在其容器的底部,不改变其大小.left Put the object at the left edge of its container, not changing its size.将对象放在其容器的左侧,不改变其大小.right Put the object at the right edge of its container, not changing its size.将对象放在其容器的右侧,不改变其大小.center_vertical Place object in the vertical center of its container, not changing its size.将对象纵向居中,不改变其大小.垂直对齐方式:垂直方向上居中对齐。
安卓期末考试知识总结第一章:Android基础入门习题总结1.Android系统采用分层架构,由高到低分为4层,主要为:应用程序层、应用程序框架层、核心类库、Linux内核2.在Android项目程序开发完成后中,必须__打包成_正式的Android安装文件,才能发布到互联网上让用户下载使用。
3.Android 工程中src目录下存放_java__文件重点知识1.应用层:应用层是核心应用程序的集合。
手机上所有的应用都属于这一层,程序,短信程序。
2.应用程序框架层:应用程序框架层主要提供构建应用程序时用到的各种API,例如:活动管理器、通知管理器、内容提供者3.核心类库:核心类库中包含了系统库及Android运行环境。
(1)系统库主要通过c/c++库为Android系统提供主要的特性支持(2)Android运行时库主要提供一些核心库,允许开发者使用java语言来编写Android应用程序4.Linux内核:为Android·设备的各种硬件提供底层的驱动,如显示驱动、音频驱动、蓝牙驱动、电源管理驱动Android工程中的assets目录主要存放一些文件资源,这些文件会被原封不动的打包到APK文件中更为细分的5层架构:应用程序层、应用程序框架层、核心类库、硬件抽象层 (HAL)、Linux内核硬件抽象层 (HAL) 提供标准界面,向更高级别的 Java API 框架显示设备硬件功能。
HAL 包含多个库模块,其中每个模块都为特定类型的硬件组件实现一个界面,例如相机或蓝牙模块。
当框架 API 要求访问设备硬件时,Android 系统将为该硬件组件加载库模块。
第二章:Android常见桌面布局&第三章:Android常见界面控件知识总结1.Android应用的界面是由View 对象和 ViewGroup 对象构建而成的。
ViewGroup继承自View,ViewGroup作为容器盛装界面中的其他控件Android应用的每个界面的根元素必须有且只有一个ViewGroup容器Android常见布局和特点:Android中常见的五种布局:相对布局(RelativeLayout)、线性布局(LinearLayout)、表格布局(TableLayout)、帧布局(FrameLayout)、约束布局(ConstraintLayout)1.相对布局(RelativeLayout):相对布局是一种基于相对位置的布局方式,可以通过设置组件相对于其他组件或父容器的位置来实现布局。
Android开发⾃学笔记(AndroidStudio)—4.1布局组件⼀、引⾔Android的界⾯是有布局和组件协同完成的,布局好⽐是建筑⾥的框架,⽽组件则相当于建筑⾥的砖⽡。
组件按照布局的要求依次排列,就组成了⽤户所看见的界⾯。
在Android4.0之前,我们通常说Android开发五⼤布局和四⼤组件,这五⼤布局就是:1. LinearLayout 线性布局2. FrameLayout 单帧布局,也有中⽂翻译为帧布局、框架布局。
3. RelativeLayout 相对布局4. AbsoluteLayout 绝对布局5. TableLayout 表格布局⽽在Android4.0之后⼜新增了⼀种GridLayout⽹格布局。
⼆、LinearLayout线性布局线性布局是Android开发中最常见的⼀种布局⽅式,它是按照垂直或者⽔平⽅向来布局,通过“android:orientation”属性可以设置线性布局的⽅向。
属性值有垂直(vertical)和⽔平(horizontal)两种。
线性布局的排列在某⾏或者某列并不会⾃动换⾏或换列,就是说如果采⽤⽔平布局,控件宽度超过屏幕显⽰的话,后⾯的控件都将被隐藏,不会⾃动换⾏。
常⽤的属性有:1. android:orientation:可以设置布局的⽅向2. android:id - 为控件指定相应的ID3. android:text - 指定控件当中显⽰的⽂字,需要注意的是,这⾥尽量使⽤string.xml4. android:gravity - 指定控件的基本位置,⽐如说居中,居右等位置5. android:textSize - 指定控件当中字体的⼤⼩6. android:background - 指定控件所⽤的背景⾊,RGB命名法7. android:layout_width - 指定控件的宽度8. android:layout_height - 指定控件的⾼度9. android:layout_weight - 指定控件的占⽤⽐例10. android:padding - 指定控件的内边距,也就是说控件当中的内容11. android:sigleLine - 如果设置为真的话,则将控件的内容显⽰在⼀⾏当中layout_weight属性以控制各个控件在布局中的相对⼤⼩。
AndroidGridLayout使⽤案例详解⽬录⼀、简介⼆、常⽤属性介绍三、平分问题四、⼩⽶计算器效果五、动态加载⼀、简介GridLayout是Android4.0引⼊的⽹格布局,使⽤它可以减少布局嵌套。
也算是常⽤,但⼀直没仔细看过,今天研究⼀下⼆、常⽤属性介绍GridLayout 使⽤属性属性作⽤android:columnCount最⼤列数android:rowCount最⼤⾏数android:orientation GridLayout中⼦元素的布局⽅向android:alignmentMode alignBounds:对齐⼦视图边界 alignMargins :对齐⼦视距内容,默认值android:columnOrderPreserved使列边界显⽰的顺序和列索引的顺序相同,默认是trueandroid:rowOrderPreserved使⾏边界显⽰的顺序和⾏索引的顺序相同,默认是trueandroid:useDefaultMargins没有指定视图的布局参数时使⽤默认的边距,默认值是falseitem属性属性作⽤android:layout_column指定该单元格在第⼏列显⽰android:layout_row指定该单元格在第⼏⾏显⽰android:layout_columnSpan指定该单元格占据的列数android:layout_rowSpan指定该单元格占据的⾏数android:layout_gravity指定该单元格在容器中的位置android:layout_columnWeight(API21加⼊)列权重android:layout_rowWeight(API21加⼊)⾏权重android:layout_gravity作⽤center不改变元素的⼤⼩,仅居中center_horizontal不改变⼤⼩,⽔平居中center_vertical不改变⼤⼩,垂直居中top不改变⼤⼩,置于顶部left不改变⼤⼩,置于左边bottom不改变⼤⼩,置于底部right不改变⼤⼩,置于右边start不改变⼤⼩,根据系统语⾔,置于开始位置end不改变⼤⼩,置于结尾fill拉伸元素控件,填满其应该所占的格⼦fill_vertical仅垂直⽅向上拉伸填充fill_horizontal仅⽔平⽅向上拉伸填充clip_vertical垂直⽅向上裁剪元素,仅当元素⼤⼩超过格⼦的空间时clip_horizontal⽔平⽅向上裁剪元素,仅当元素⼤⼩超过格⼦的空间时注意使⽤layout_columnSpan 、layout_rowSpan时要加上layout_gravity属性,否则没有效果;另外item在边缘时宽⾼计算会出现错误,需要我们⼿动设置宽⾼,否则达不到想要的效果三、平分问题GridLayout在API21时引⼊了android:layout_columnWeight和android:layout_rowWeight来解决平分问题那么在API21以前的,想要平分的话:引⽤兼容包compile 'com.android.support:gridlayout-v7:25.+'注意:1. 使⽤该控件,命名空间使⽤app2. 单独设置app:layout_columnWeight时,这⼀列的所有item都设置为这个属性,才能达到预期效果,否则这⼀列中设置了该属性的item,都会被隐藏,显⽰不出来3. 单独设置app:layout_rowWeight时,没有问题四、⼩⽶计算器效果<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.GridLayoutxmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:id="@+id/grid_layout"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#ece7e7"app:alignmentMode="alignBounds"app:columnCount="4"app:orientation="horizontal"app:rowCount="5"app:useDefaultMargins="false"tools:context="com.strivestay.gridlayoutdemo.MainActivity"><!-- 如果不使⽤ app:layout_gravity="fill",则实际下⾯这个textview的宽度只是wrap_content,实现不了想要的right|bottom效果;或者,⽤app:layout_columnWeight="1",效果等同,填充满--><TextViewandroid:gravity="right|bottom"android:text="0"app:layout_columnSpan="4"app:layout_rowWeight="3"app:layout_columnWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="AC"android:textColor="#f68904"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="退格"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="/"app:layout_rowWeight="1"/> <TextViewandroid:layout_margin="1dp" android:background="#ffffff" android:gravity="center"android:text="*"app:layout_columnWeight="1" app:layout_rowWeight="1"/> <TextViewandroid:layout_margin="1dp" android:background="#ffffff" android:gravity="center"android:text="7"app:layout_columnWeight="1" app:layout_rowWeight="1"/> <TextViewandroid:layout_margin="1dp" android:background="#ffffff" android:gravity="center"android:text="8"app:layout_columnWeight="1" app:layout_rowWeight="1"/> <TextViewandroid:layout_margin="1dp" android:background="#ffffff" android:gravity="center"android:text="9"app:layout_columnWeight="1" app:layout_rowWeight="1"/> <TextViewandroid:layout_margin="1dp" android:background="#ffffff" android:gravity="center"android:text="—"app:layout_columnWeight="1" app:layout_rowWeight="1"/> <TextViewandroid:layout_margin="1dp" android:background="#ffffff" android:gravity="center"android:text="4"app:layout_columnWeight="1" app:layout_rowWeight="1"/> <TextViewandroid:layout_margin="1dp" android:background="#ffffff" android:gravity="center"android:text="5"app:layout_columnWeight="1" app:layout_rowWeight="1"/> <TextViewandroid:layout_margin="1dp" android:background="#ffffff" android:gravity="center"android:text="6"app:layout_columnWeight="1" app:layout_rowWeight="1"/> <TextViewandroid:layout_margin="1dp" android:background="#ffffff" android:gravity="center"android:text="+"app:layout_columnWeight="1" app:layout_rowWeight="1"/> <TextViewandroid:background="#ffffff"android:gravity="center"android:text="1"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="2"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="3"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#f68904"android:gravity="center"android:text="="android:textColor="#ffffff"app:layout_columnWeight="1"app:layout_rowSpan="2"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="%"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="0"app:layout_columnWeight="1"app:layout_rowWeight="1"/><TextViewandroid:layout_margin="1dp"android:background="#ffffff"android:gravity="center"android:text="."app:layout_columnWeight="1"app:layout_rowWeight="1"/></android.support.v7.widget.GridLayout>效果: 4.4.4模拟器五、动态加载1.xml引⽤GridLayout<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.GridLayoutxmlns:android="/apk/res/android" xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:id="@+id/grid_layout"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#ece7e7"app:orientation="horizontal"app:useDefaultMargins="false"app:alignmentMode="alignBounds"tools:context="com.strivestay.gridlayoutdemo.MainActivity"> </android.support.v7.widget.GridLayout>2.动态添加package com.strivestay.gridlayoutdemo;import android.graphics.Color;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.GridLayout;import android.view.Gravity;import android.widget.TextView;/*** GridLayout⽰例* @author StriveStay* @date 2018/3/27*/public class MainActivity extends AppCompatActivity {private String[] mStrings = {"0","AC","退格","/","*","7","8","9","—","4","5","6","+","1","2","3","=","%","0","."}; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// xml布局// setContentView(yout.activity_main);// 动态添加setContentView(yout.activity_main2);GridLayout gridLayout = (GridLayout) findViewById(R.id.grid_layout);// 6⾏ 4列gridLayout.setColumnCount(4);gridLayout.setRowCount(6);for (int i = 0; i < mStrings.length; i++) {TextView textView = new TextView(this);youtParams params = new youtParams();params.width =0;params.height =0;if(i == 0){// 设置⾏列下标,所占⾏列,⽐重// 对应: layout_row , layout_rowSpan , layout_rowWeight// 如下代表: item坐标(0,0),占 1 ⾏,⽐重为 3 ;占 4 列,⽐重为 1params.rowSpec = GridLayout.spec(0,1,3f);params.columnSpec = GridLayout.spec(0,4,1f);textView.setGravity(Gravity.BOTTOM|Gravity.RIGHT);}else{// 设置⾏列下标,和⽐重params.rowSpec = GridLayout.spec((i+3)/4,1f);params.columnSpec = GridLayout.spec((i+3)%4,1f);// 背景textView.setBackgroundColor(Color.WHITE);// 字体颜⾊if("AC".equals(mStrings[i])){textView.setTextColor(Color.parseColor("#f68904"));}if("=".equals(mStrings[i])){textView.setBackgroundColor(Color.parseColor("#f68904"));textView.setTextColor(Color.WHITE);params.rowSpec = GridLayout.spec((i+3)/4,2,1f);}// 居中显⽰textView.setGravity(Gravity.CENTER);// 设置边距params.setMargins(2,2,2,2);}// 设置⽂字textView.setText(mStrings[i]);// 添加itemgridLayout.addView(textView,params);}}}效果和⽤xml中直接布局⼀样:注意:GridLayout.spec(); 这个⽅法是⼀个重点,需要好好看⼀下,⽽且由于它有⼏个重载⽅法,使⽤时也要注意。
Android开发之EditText属性详解1、EditText输入的文字为密码形式的设置(1)通过.xml里设置:把该EditText设为:android:password="true" // 以”.”形式显示文本(2)在代码里设置:通过设置EditText的setTransformationMethod()方法来实现隐藏密码或这显示密码。
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());//设置密码为不可见。
2、(1)EditText输入的文字为电话号码Android:phoneNumber=”true” //输入电话号码3、EditText字数限制的设置(1)在.xml中设置:android:maxLength=“50”(2)代码中设置:editText.setFilters(new InputFilter[]{newInputFilter.LengthFilter(100)});4、EditText设置字体android:typeface="monospace" //设置字型。
字形有:normal, sans, serif,monospace5、EditText是否可编辑Android:editable // 是否可编辑6、在EditText中软键盘的调起、关闭(1)EditText有焦点(focusable为true)阻止输入法弹出editText=(EditText)findViewById(R.id.txtBody);editText.setOnTouchListener(new OnTouchListener(){public boolean onTouch(View v, MotionEvent event){editText.setInputType(InputType.TYPE_NULL); //关闭软键盘return false;}});(2)当EidtText无焦点(focusable=false)时阻止输入法弹出InputMethodManager imm =(InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);imm.hideSoftInputFromWindow(editText.getWindowToken(),0);(3)调用数字键盘并设置输入类型和键盘为英文etNumber.setInputType(InputType.TYPE_CLASS_NUMBER); //调用数字键盘rlEditText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);//设置输入类型和键盘为英文或者:android:inputType="textUri|textMultiLine"(4)android:focusable="false"//键盘永远不会弹出<activity android:name=".AddLinkman"android:windowSoftInputMode="adjustUnspecified|stateHidden"/>//不自动弹出键盘//关闭键盘(比如输入结束后执行)InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(etEditText.getWindowToken(), 0);//自动弹出键盘((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);etEditText.requestFocus();//让EditText获得焦点,但是获得焦点并不会自动弹出键盘7、android:layout_gravity和android:gravity的区别(1)android:layout_gravity是本元素对父元素的重力方向。
框架布局:FrameLayoutFrameLayout布局:所有添加到这个布局中的视图都以层叠的方式显示。
第一个添加的组件放到最底层,最后添加到框架中的视图显示在最上面。
上一层的会覆盖下一层的组件。
<FrameLayoutxmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="200dp"android:layout_height="200dp"android:background="#aa0000"android:text="第一个"android:gravity="bottom|right"/><TextViewandroid:layout_width="100dp"android:layout_height="100dp"android:background="#00aa00"android:text="第二个"android:gravity="bottom|right"/><TextViewandroid:layout_width="50dp"android:layout_height="50dp"android:background="#0000aa"android:text="第三个"android:gravity="bottom|right"/></FrameLayout>android:gravity:”bottom|right”//控制文字在一个文本框里的位置android:layout_gravity=”center_vertical|center_horizontal”//控制文本框在页面的位置。
简单解释一下android 中layout_gravity和gravity
本篇文章的目的就是为大家简单解释android 中layout_gravity和gravity,希望看完对大家有帮助。
相信很多学习了android的人,都知道布局中存在两个很相似的属性:android :layout_gravity和android:gravity。
一般的都知道,
android :layout_gravity 是表示该组件在父控件中的位置
android:gravity 是表示该组件的子组件在自身中的位置
例子:
<LinearLayout xmlns:android="/apk/res/android" Xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Text View
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="我在右边显示"
android:layout_gravity="right"
android:background="#00ff00"
/>
</LinearLayout>
如上的代码显示效果如下:
经管很明了,但是某些时候也会出现失效的情况,如下:
<LinearLayout xmlns:android="/apk/res/android" Xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Text View
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="我在右边显示"
android:layout_gravity="right"
android:background="#00ff00"
/>
</LinearLayout>
如上代码效果如下:
Why?
第二段代码和第一段代码唯一的区别,线性布局中
android:orientation="vertical"属性不存在,使用的是默认水平方向。
也说明当最为父布局的LinearLayout是android:orientation="vertical"的时候,
android:layout_gravity="right"才会生效。
我们在定义一个控件默认的位置是在屏幕左上方,线性布局是存在两个方向的,既然当水平方向为垂直的时候是,android:layout_gravity="right"才会生效。
那么当水平方向为默认的水平方向的时候,会不会对其他的属性有影响呢?答案是肯定的。
既然左右为一对的话,那么上下似乎应该为一对。
经过测试当LinearLayout是android:orientation="horizontal"的时候,android:layout_gravity="bottom"才会生效。
不再一一验证其他属性,做一个简单的总结如下:
当作为父layout的LinearLayout的属性为androidrientation="vertical" 的时候,android:layout_gravity="?"这里设为横向的时候才能生效。
比如:left,right,center_horizontal等
当作为父layout的LinearLayout的属性为androidrientation="horizental" 的时候,android:layout_gravity="?"这里设为纵向的时候才能生效。
比如:top,bottom,center_vertical等;
以上内容就是简单解释了android 中layout_gravity和gravity,更多精彩内容可以在华清远见官网获取,华清远见提供免费的学习资料供大家参考。