布局入门(一)
来源:互联网 发布:php简历项目经验怎么写 编辑:程序博客网 时间:2024/06/05 04:40
一,手上接了一个Android项目,因为做的是动态的布局。所以在布局上很是纠结了一下,正好趁着这个机会,总结一下LinearLayout布局上的应用方法。
一个是LinearLayout本身的布局。
1.1 命名空间
xmlns 顾名思义xml的命名空间,(xml name space)关于命名空间上,不懂的可以百度,这里不多介绍。
1.2 排列
ientation 英[ˌɔ:riənˈteɪʃn] 原意是排列方向。这里有vertical(纵向)horizontal(橫向)两个选项。当LinearLayout布局layout_width属性定义为
tch_parent时,orientation的属性必须指定。其中horizontal是默认选项。
1.3 Layout_width 控件宽度,一般有match_parent(匹配父窗口)和wrap_content(内容填充)两个选项,在新版本中, fill_parent被摈弃不用,一般用
match_parent代替。
1.4 Layout_height 控件高度,属性和用法跟Layout_width相同
1.5 layout_gravity 用来设置该控件相对与父控件的位置.比如一个button 在linearlayout里,你想把该button放在linearlayout里靠左、
靠右等位置就可以通过该属性设置.
1.6 weightSum 定义weight总和的最大值。如果未指定该值,以所有子视图的layout_weight属性的累加值作为总和的最大值。一个典型
的案例是:通过指定子视图的layout_weight属性为0.5,并设置LinearLayout的weightSum属性为1.0,实现子视图占据可用宽度的50%。
1.7 layout_margin 指view距离父view的距离。或者说距离view的相对view的距离
1.8 apha 可见度.android:alpha="0.0"看不见的,android:alpha="0.5"透视 android:alpha="1.0"全可见
1.9 accessibilityLiveRegion辅助性服务是否应自动通知更改视图的内容描述或文本,或者子view的文字描述或文本内容;(none——不通知、polite——通知、assertive——打断语音,并通知)
二,控件的属性种类
通常意义上讲,我们在对一个控件进行属性赋值的时候大体上有种类型的属性,一种为layout_开头的属性,一种为不是以layout_开头的属性,下面以TextView为例进行说明,如下所示
1 <RelativeLayout 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" > 4 5 <TextView 6 android:layout_width="wrap_content" 7 android:layout_height="wrap_content" 8 android:text="hello_world" /> 9 10 </RelativeLayout>
我为TextView设置了三个属性layout_width、layout_height以及text,可以看到这三个属性中layout_width、layout_height属性为layout_开头而text没有以Layout_开头。
以layout_开头的属性为从容器中继承的属性,在这个例子里面即是从RelativeLayout中继承来的,TextView本身并没有此属性。而text则是TextView自身拥有的属性。
为了说明layout属性为容器属性,我做了下面的例子,把TextView分别放置到RelativeLayout和LinearLayout中,然后对TextView设置layout_centerInParent属性,之所有选择这个属性,是因为这个属性为RelativeLayout所有而LinearLayout没有,实验代码如下
1 <RelativeLayout 2 android:layout_width="wrap_content" 3 android:layout_height="wrap_content" > 4 5 <TextView 6 android:layout_width="wrap_content" 7 android:layout_height="wrap_content" 8 android:layout_centerInParent="true" 9 android:text="hello_world" />10 </RelativeLayout>11 12 <LinearLayout13 android:layout_width="wrap_content"14 android:layout_height="wrap_content" >15 16 <TextView17 android:layout_width="wrap_content"18 android:layout_height="wrap_content"19 android:layout_centerInParent="true"20 android:text="hello world2" />21 </LinearLayout>
如上进行设置之后会发现,编辑器提示“Invalid layout param in a LinearLayout: layout_centerInParent”,如下所示
通过此实验可以得出结论,layout开头的属性并非TextView所拥有,而是继承的容器中关于布局的属性,继而推而广之,可以得出结论,控件的属性可以分为自身属性和容器中的布局属性。下面就通过TextView和各个布局容器一起来详细分析下属性。
LinearLayout和TextView
这一小节主要介绍下LinearLayout和TextView的属性,先来看下TextView的属性,及属性继承关系,
下面为TextView自身所拥有的属性
TextView继承属性
From class android.view.View
Attribute Nameandroid:accessibilityLiveRegionandroid:alphaandroid:backgroundandroid:clickableandroid:contentDescriptionandroid:drawingCacheQualityandroid:duplicateParentStateandroid:fadeScrollbarsandroid:fadingEdgeLengthandroid:filterTouchesWhenObscuredandroid:fitsSystemWindowsandroid:focusableandroid:focusableInTouchModeandroid:hapticFeedbackEnabledandroid:idandroid:importantForAccessibilityandroid:isScrollContainerandroid:keepScreenOnandroid:layerTypeandroid:layoutDirectionandroid:longClickableandroid:minHeightandroid:minWidthandroid:nextFocusDownandroid:nextFocusForwardandroid:nextFocusLeftandroid:nextFocusRightandroid:nextFocusUpandroid:onClickandroid:paddingandroid:paddingBottomandroid:paddingEndandroid:paddingLeftandroid:paddingRightandroid:paddingStartandroid:paddingTopandroid:requiresFadingEdgeandroid:rotationandroid:rotationXandroid:rotationYandroid:saveEnabledandroid:scaleXandroid:scaleYandroid:scrollXandroid:scrollYandroid:scrollbarAlwaysDrawHorizontalTrackandroid:scrollbarAlwaysDrawVerticalTrackandroid:scrollbarDefaultDelayBeforeFadeandroid:scrollbarFadeDurationandroid:scrollbarSizeandroid:scrollbarStyleandroid:scrollbarThumbHorizontalandroid:scrollbarThumbVerticalandroid:scrollbarTrackHorizontalandroid:scrollbarTrackVerticalandroid:scrollbarsandroid:soundEffectsEnabledandroid:tagandroid:textAlignmentandroid:textDirectionandroid:transformPivotXandroid:transformPivotYandroid:translationXandroid:translationYandroid:visibility也就是说这两部分加起来就是TextView的全部属性,然而我们在XML编辑器中编辑代码的时候会发现,除了以上属性外还有很多以layout开头的属性可以设置,这些就是容器的属性,下面以LinearLayout为例,实验场景如下
1 <LinearLayout 2 android:layout_width="wrap_content" 3 android:layout_height="wrap_content" > 4 5 <TextView 6 android:layout_width="wrap_content" 7 android:layout_height="wrap_content" 9 android:text="hello world2" />10 </LinearLayout>
下面我们就看下LinearLayout的布局属性,通过LinearLayout.LayoutParams类来体现
通过帮助文档可以看到,其自身属性为以下内容
还有继承了两类属性,android.view.ViewGroup.MarginLayoutParams
Attribute Nameandroid:layout_marginBottomandroid:layout_marginEndandroid:layout_marginLeftandroid:layout_marginRightandroid:layout_marginStartandroid:layout_marginTop
和android.view.ViewGroup.LayoutParams
attributeNameRelatedMethodDescriptionandroid:layout_height Specifies the basic height of the viewandroid:layout_width Specifies the basic width of the view所以,TextView可以设置的属性即为自身属性和以上的布局属性。 以上所有属性就是布局控件提供的布局属性
后记
以上我们通过LinearLayout和TextView的组合分析了Android中布局的属性使用情况,通过以上的分析使得我们能够了解到控件属性的由来,这样就可以更加随心所欲的进行界面布局了。
同样的道理,可以去分析RelativeLayout、TableLayout等等。
- 布局入门(一)
- JavaFX入门(四):JavaFX布局(一)
- DIV+CSS布局入门示例(一)页面布局与规划
- DIV+CSS布局入门示例(一)页面布局与规划
- PhoneGap 布局(一)
- 布局问题(一)
- DotNetBar布局(一)
- 布局杂谈(一)
- 框架(一)布局
- Flex布局(一)
- flex布局(一)
- 【南大软院大神养成计划:第四天】实战初入门:网页布局(一)
- React Native入门——布局实践:开发京东客户端首页(一)
- 安卓入门学习(一)从布局XML到视图对象
- AutoLayout(自动布局)入门
- AutoLayout(自动布局)入门
- AutoLayout(自动布局)入门
- iOSLearningDiaryAutoLayout(自动布局)入门
- Writing a Linux Kernel Module — Part 2: A Character Device
- 动态规划9:变态跳台问题
- java创建对象的方式
- Android之Adapter用法总结
- 答同行:提高科研能力很有必要
- 布局入门(一)
- 数据库概论---数据定义
- 四.控制执行流程——Java编程思想第4版学习笔记
- 第七届福建省大学生程序设计竞赛 Problem J- X(floyd)
- UE4源码clone
- ecplise快捷键
- C# 抽象类 虚方法 抽象方法
- java编程重点笔记
- 关于统一品牌的水趣多饮品的感受