android 界面中的各种布局

来源:互联网 发布:北京市软件企业认定 编辑:程序博客网 时间:2024/06/03 04:17

布局:  

  在 android 中我们常用的布局体式格式有这么几种:

1.LinearLayout  线性布局  (里面只可以有一个控件,并且不克不及设计这个控件的地位,控件会放到左上角)

                                           线性布局分为程度线性和垂直线性二者的属性分别为: android:orientation= " horizontal   android:orientation= "vertical" 

           2.RelativeLayout  相对布局 )  (里面可以放多个控件,然则一行只能放一个控件)

                                                  附加几类 RelativeLayout 的属性供大师参考:

第一类 : 属性值为 true  false

android:layout_centerHrizontal                   程度居中

android:layout_centerVertical                    垂直居中

android:layout_centerInparent                 相对于父元素完全居中

android:layout_alignParentBottom              贴紧父元素的下边沿

android:layout_alignParentLeft                 贴紧父元素的左边沿

android:layout_alignParentRight                贴紧父元素的右边沿

android:layout_alignParentTop                  贴紧父元素的上边沿

android:layout_alignWithParentIfMissing   若找不到兄弟元素以父元素做参照物

第二类:属性值必须为 id 的引用名“ id/id-name 

android:layout_below                          在某元素的下方

android:layout_above                          在某元素的上方

android:layout_toLeftOf                       在某元素的左边

android:layout_toRightOf                     在某元素的右边

android:layout_alignTop            本元素的上边沿和某元素的的上边沿对齐

android:layout_alignLeft           本元素的左边沿和某元素的的左边沿对齐

android:layout_alignBottom         本元素的下边沿和某元素的的下边沿对齐

android:layout_alignRight          本元素的右边沿和某元素的的右边沿对齐

第三类:属性值为具体的像素值,如 30dip  40px

android:layout_marginBottom              离某元素底边沿的间隔

android:layout_marginLeft                 离某元素左边沿的间隔

android:layout_marginRight                离某元素右边沿的间隔

android:layout_marginTop                  离某元素上边沿的间隔

          3.TableLayout  表格布局 )  (这个要和TableRow共同应用,很像html里面的table

                                             这个表格布局不像HTML中的表格那样灵活,只能经由过程 TableRow 属性来把握它的行而列的话里面有几个控件就是几列(一般景象)。 如:

                                                    <TableLayout>

<TableRow>

                                                              <EditText></EditText>

                                                              <EditText></EditText>

</TableRow>

<TableRow>

                                                               <EditText></EditText>

                                                               <EditText></EditText>

</TableRow>

</TableLayout>

默示两行两列的一个表格。

android:gravity="center" 书面申明是权重比。当时就是让它居中显示。它还可以动态添加里面的每行每列。如下代码所示:

/*按照id查找表格对象*/

TableLayout tableLayout = TableLayout) findViewByIdR.id.table01;

/*创建列对象*/

TableRow tableRow = new TableRowthis;

/*文本框对象*/

TextView temp = new TextViewthis;

temp.setText"text的值";

/*将此文本添加到列中*/

tableRow.addViewtemp;

android:stretchColumns="1234它的意思就是主动拉伸1234列。

                                                   

4.AbsoluteLayout  绝对布局 )  (里面可以放多个控件,并且可以本身定义控件的xy的地位)

5.FrameLayout  帧布局 ) :(里面可以放多个控件,不过控件的地位都是相对地位)

                               在它里面的控件都是按后面的一个控件叠加在前一个控件上来显示的,所有元素都被放置在最左上角。 如:

                                     <FrameLayout android:layout_width="wrap_content"

                                                        android:layout_height="wrap_content" android:layout_weight="1">

                                              <ImageView android:id="+id/iv1" android:layout_width="wrap_content"

                                                            android:layout_height="wrap_content" android:visibility="invisible"

                                                            android:src="drawable/lotusleaf"></ImageView>

                                             <ImageView android:id="+id/f1" android:layout_width="wrap_content"

                                                            android:layout_height="wrap_content" android:src="drawable/frog_right"

                                                            android:visibility="invisible"></ImageView>

                                         </FrameLayout> 

默示的是idf1的控件叠加在idiv1的控件上方显示

 

          (LinearLayout 和 RelativeLayout 应当又是此顶用的较多的两种。AbsoluteLayout 斗劲罕用,因为它是按屏幕的绝对地位来布局的若是屏幕大小产生改变的话控件的地位也产生了改变。这个就相当于HTML中的绝对布局一样,一般不推荐应用 )  

          重视事项:

1 、各布局不要乱花各自的属性。比如把属于 AbsoluteLayout 布局的android:layout_xandroid:layout_y用到 LinearLayout 布局或 RelativeLayout 布局,或者把 RelativeLayout 布局的 below  rightof 等属性应用到其他布局中。如许做固然不会报错,但这是白浪费情感的工作,底子达不到我们须要的结果。

2 、关于android:layout_width="fill_parent"  android:layout_height="wrap_content" ,这是对每个布局宽和高的设置。 wrap_content 可默示跟着此中控件的不合而改变这个布局的宽度或高度,类似于主动设置宽和高, fill_parent 使布局填充全部屏幕,别的还有一种 match_parent ,它本质上和 fill_parent 一样,并从 API  Level8 开端调换 fill_parent 

TextView 的属性 :

android:autoLink              //设置是否当文本为URL链接/email/德律风号码/map时,文本显示为可点击的链接。可选值(none/web /email/phone/map/all

android:autoText                   //若是设置,将主动履行输入值的拼写改正。此处无结果,在显示输入法并输入的时辰起感化

android:bufferType               //指定getText()体式格式取得的文本类别。选项editable 类似于StringBuilder可追加字符,也就是说getText后可调用append办法设置文本内容。spannable 则可在给定的字符区域应用样式

android:capitalize                  //设置英文字母大写类型。此处无结果,须要弹出输入法才干看获得,拜见EditView此属性申明

android:cursorVisible              //设定光标为显示/隐蔽,默认显示

android:digits                     //设置容许输入哪些字符。如“1234567890.+-*/% ()”

android:drawableBottom         //text的下方输出一个drawable,如图片。若是指定一个色彩的话会把text的靠山设为该色彩,并且同时和background应用时覆盖后者

android:drawableLeft             //text的左边输出一个drawable,如图片

android:drawablePadding         //设置textdrawable(图片)的间隔,与drawableLeft、 drawableRightdrawableTopdrawableBottom一路应用,可设置为负数,零丁应用没有结果

android:drawableRight            //text的右边输出一个drawable

android:drawableTop              //text的正上方输出一个drawable

android:editable                   //设置是否可编辑

android:editorExtras              //设置文本的额外的输入数据

android:ellipsize                //设置当文字过长时,该控件该如何显示。有如下值设置:”start”—?省略号显示在开首;end” ——省略号显示在结尾;middle”—-省略号显示在中心;marquee” ——以跑马灯的体式格式显示(动画横向移动)

android:freezesText               //设置保存文本的内容以及光标的地位

android:gravity                    //设置文本地位,如设置成“center”,文本将居中显示

android:hintText                 //为空时显示的文字提示信息,可经由过程textColorHint设置提示信息的色彩。此属性在 EditView中应用,然则这里也可以用

android:imeOptions                //附加功能,设置右下角IME动作与编辑框相干的动作,如actionDone右下角将显示一个“完成”,而不设置默认是一个回车符号。这个在EditView中再具体申明,此处无用

android:imeActionId                 //设置IME动作ID

android:imeActionLabel              //设置IME动作标签

android:includeFontPadding         //设置文本是否包含顶部和底部额外空白,默认为true

android:inputMethod                //为文本指定输入法,须要完全限制名(完全的包名)。例如:com.google.android.inputmethod.pinyin,然则这里报错找不到

android:inputType                    //设置文本的类型,用于帮助输入法显示合适的键盘类型。在EditView中再具体申明,这里无结果

android:linksClickable                //设置链接是否点击连接,即使设置了autoLink

android:marqueeRepeatLimit        //ellipsize指定marquee的景象下,设置反复迁移转变的次数,当设置为 marquee_forever时默示无穷次

android:ems                         //设置TextView的宽度为N个字符的宽度。这里测试为一个汉字字符宽度

android:maxEms                    //设置TextView的宽度为最长为N个字符的宽度。与ems同时应用时覆盖ems选项

android:maxLength                 //限制显示的文本长度,超出项目组不显示

android:lines                        //设置文本的行数,设置两行就显示两行,即使第二行没稀有据

android:maxLines                   //设置文本的最大显示行数,与width或者layout_width连络应用,超出项目组主动换行,超出行数将不显示

android:minLines                    //设置文本的最小行数,与lines类似

android:lineSpacingExtra            //设置行间距

android:lineSpacingMultiplier        //设置行间距的倍数。如”¥2

android:numeric                     //若是被设置,该TextView有一个数字输入法。此处无用,设置后独一结果是TextView有点击结果,此属性在EdtiView将具体申明

android:password                    //以小点”.”显示文本 android:phoneNumber设置为德律风号码的输入体式格式

android:privateImeOptions          //设置输入法选项,此处无用,在EditText将进一步评论辩论

android:scrollHorizontally            //设置文本超出TextView的宽度的景象下,是否呈现横拉条

android:AllOnFocus              //若是文本是可选择的,让他获取核心而不是将光标移动为文本的开端地位或者末尾地位。TextView中设置后无结果

android:shadowColor                //指定文本暗影的色彩,须要与shadowRadius一路应用

android:shadowDx                   //设置暗影横向坐标开端地位

android:shadowDy                   //设置暗影纵向坐标开端地位

android:shadowRadius                //设置暗影的半径。设置为0.1就变成字体的色彩了,一般设置为3.0的结果斗劲好

android:singleLine                     //设置单行显示。若是和layout_width一路应用,当文本不克不及全部显示时,后面用“…”来默示。如android:text="test_ singleLine " android:singleLine="true" android:layout_width="20dp"将只显示“t…”。若是不设置singleLine或者设置为false,文本将主动换行 android:text设置显示文本. android:textAppearance设置文字外观。如 “?android:attr/textAppearanceLargeInverse”这里引用的是体系自带的一个外观,?默示体系是否有这种外观,不然应用默认的外观。可设置的值如下:textAppearanceButton/textAppearanceInverse /textAppearanceLarge/textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/textAppearanceSmall/textAppearanceSmallInverse

android:textColor                    //设置文本色彩

android:textColorHighlight           //被选中文字的底色,默认为蓝色

android:textColorHint                //设置提示信息文字的色彩,默认为灰色。与hint一路应用。

android:textColorLink                //文字链接的色彩.

android:textScaleX                  //设置文字之间间隔,默认为¥2

android:textSize                      //设置文字大小,推荐怀抱单位”sp”,如”15sp

android:textStyle                     //设置字形[bold(粗体) 0, italic(斜体) 1, bolditalic(又粗又斜) 2] 可以设置一个或多个,用“|”隔开

android:typeface                     //设置文本字体,必须是以下常量值之一:normal 0, sans 1, serif 2, monospace(等宽字体) 3]

android:height                       //设置文本区域的高度,支撑怀抱单位:px(像素)/dp/sp/in/mm(毫米)

android:maxHeight                  //设置文本区域的最大高度

android:minHeight                   //设置文本区域的最小高度

android:width                        //设置文本区域的宽度,支撑怀抱单位:px(像素)/dp/sp/in/mm(毫米),与layout_width 的差别看这里

android:maxWidth                   //设置文本区域的最大宽度

android:minWidth                   //设置文本区域的最小宽度 android布局属性详解 RelativeLayout用到的一些首要的属性:第一类:属性值为truefalse

android:layout_centerHrizontal         //程度居中

android:layout_centerVertical           //垂直居中

android:layout_centerInparent          //相对于父元素完全居中

android:layout_alignParentBottom       //贴紧父元素的下边沿

android:layout_alignParentLeft          //贴紧父元素的左边沿

android:layout_alignParentRight        //贴紧父元素的右边沿

android:layout_alignParentTop          //贴紧父元素的上边沿

android:layout_alignWithParentIfMissing      //若是对应的兄弟元素找不到的话就以父元素做参照物 第二类:属性值必须为id的引用名“@id/id-name

android:layout_below                    //在某元素的下方

android:layout_above                    //在某元素的的上方

android:layout_toLeftOf                  //在某元素的左边

android:layout_toRightOf                //在某元素的右边

android:layout_alignTop                 //本元素的上边沿和某元素的的上边沿对齐

android:layout_alignLeft                //本元素的左边沿和某元素的的左边沿对齐

android:layout_alignBottom            //本元素的下边沿和某元素的的下边沿对齐

android:layout_alignRight              //本元素的右边沿和某元素的的右边沿对齐 第三类:属性值为具体的像素值,如30dip40px

android:layout_marginBottom         //离某元素底边沿的间隔

android:layout_marginLeft             //离某元素左边沿的间隔

android:layout_marginRight            //离某元素右边沿的间隔

android:layout_marginTop              //离某元素上边沿的间隔 EditTextandroid:hint 设置EditText为空时输入框内的提示信息 

android:gravity                         //属性是对该view 内容的限制.比如一个button 上方的text. 你可以设置该text view的靠左,靠右等地位.以button为例,android:gravity="right"button上方的文字靠右 android:layout_gravity android:layout_gravity是用来设置该view相对与起父view 的地位.比如一个button linearlayout里,你想把该button放在靠左、靠右等地位就可以经由过程该属性设置.以button为例,android:layout_gravity="right"button靠右 android:layout_alignParentRight 使当前控件的右端和父控件的右端对齐。这里属性值只能为truefalse,默认false。 android:scaleType: android:scaleType是把握图片如何resized/moved来匹对ImageViewsize。 ImageView.ScaleType / android:scaleType值的意义差别: CENTER /center 按图片的本来size居中显示,当图片长/宽跨越View的长/宽,则截取图片的居中项目组显示 CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)便是或大于View的长(宽) CENTER_INSIDE / centerInside 将图片的内容完全居中显示,经由过程按比例缩小或本来的size使得图片长/宽便是或小于View的长/宽 FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示 FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下项目组地位 FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上项目组地位 FIT_XY / fitXY 把图片不按比例扩大/缩小到View的大小显示 MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。 ** 要重视一点,Drawable文件夹里面的图片定名是不克不及大写的

 

 

 

Edittext 的属性 :    EditText持续关系:View-->TextView-->EditText。 EditText 的属性很多,这里介绍几个:            android:layout_gravity="center_vertical"      //设置控件显示的地位:默认top,这里居中显示,还有bottom         android:hint="请输入数字!"                  //设置显示在空间上的提示信息         android:numeric="integer"                    //设置只能输入整数,若是是小数则是:decimal         android:singleLine="true"                     //设置单行输入,一旦设置为true,则文字不会主动换行。         android:password="true"                     //设置只能输入暗码         android:textColor = "ff200"                 //字体色彩         android:textStyle="bold"                      //字体,bold, italic, bolditalic         android:textSize="20dip"                     //大小         android:capitalize = "characters"              //以大写字母写         android:textAlign="center"                    //EditText没有这个属性,但TextView有,居中         android:textColorHighlight="cccccc"         //被选中文字的底色,默认为蓝色         android:textColorHint="ffff00"               //设置提示信息文字的色彩,默认为灰色         android:textScaleX="1.5"                    //把握字与字之间的间距         android:typeface="monospace"              //字型,normal, sans, serif, monospace         android:background="null"                 //空间靠山,这里没有,指透明         android:layout_weight="1"                   //权重,把握控件之间的地位,在把握控件显示的大小时蛮有效的。         android:textAppearance="android:attr/textAppearanceLargeInverse"

     

        1.EditText默认不弹出软件键盘

           办法一:

          在 AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为 adjustUnspecified|stateHidden

                   android:windowSoftInputMode="adjustUnspecified|stateHidden"

          办法二:

         让 EditText落空核心,应用EditTextclearFocus办法

            edit.clearFocus();

          办法三:

         强迫隐蔽Android输入法窗口

         例如:EditText edit=EditTextfindViewByIdR.id.edit

           InputMethodManager imm = InputMethodManagergetSystemServiceContext.INPUT_METHOD_SERVICE;

           imm.hideSoftInputFromWindowedit.getWindowToken(),0;

        2.EditText始终不弹出软件键盘

        例:EditText edit=EditTextfindViewByIdR.id.edit;

          edit.setInputTypeInputType.TYPE_NULL;


 


Button 持续自 VIEW , VIEW 有的属性它都能用 <xml version="1.0" encoding="utf-8"

<or xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true"(这里的样式是当按钮被按下时的显示)

<shape>

<gradient

android:startColor="drawable/gray"

android:endColor="drawable/white"

android:angle="*"/>

<stroke

android:width="*dp"

android:color="drawable/teal"/>

<corners

android:radius="*dp"/>

<padding

android:left="**dp"

android:top="*dp"

android:right="**dp"

android:bottom="*dp"/>

</shape>

</item>

 

<item android:state_focused="true">(这里的样式是移动到按钮时的显示)

<shape>

<gradient

android:startColor="drawable/silver"

android:endColor="drawable/springgreen"

android:angle="*"/>

<stroke

android:width="*dp"

android:color="drawable/teal"/>

<corners

android:radius="*dp"/>

<padding

android:left="**dp"

android:top="*dp"

android:right="**dp"

android:bottom="*dp"/>

</shape>

</item>

 

<item> (这里的样式是按钮正常时的显示)

<shape>

<gradient

android:startColor="drawable/silver"

android:endColor="drawable/snow"

android:angle="*"/>

<stroke

android:width="*dp"

android:color="drawable/teal"/>

<corners

android:radius="*dp"/>

<padding

android:left="**dp"

android:top="*dp"

android:right="**dp"

android:bottom="*dp"/>

</shape>

</item>

</or>

注:

<padding

android:left="**dp"

android:top="*dp"

android:right="**dp"

android:bottom="*dp" />

这里 left  right 把握的是 Button 上的字体与按钮的左边沿和右边沿的间隔,也就是把握按钮是长还是短;这里的 top  bottom 把握的是 Button 上的字体与按钮的上边沿和下边沿的间隔,也就是把握按钮时高还是矮。

 

 

Shape 样式油滑结果:

<xml version="1.0" encoding="UTF-8">

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color=""/>

<stroke android:width="*dp" android:color=" " />

<padding android:left="*dp" android:top="*dp"

android:right="*dp" android:bottom="*dp"/>

<corners android:radius="*dp"/>

</shape>

原创粉丝点击