Android界面基本属性

2014-08-27 Android入门 6105 ℃ 0 评论


布局:在 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) findViewById(R.id.table01);
/*创建列对象*/
TableRow tableRow = new TableRow(this);
/*文本框对象*/
TextView temp = new TextView(this);
temp.setText("text的值");
/*将此文本添加到列中*/
tableRow.addView(temp);
android:stretchColumns="1,2,3,4" 它的意思就是自动拉伸1,2,3,4列。

                                                   

4.AbsoluteLayout ( 绝对布局 ) : (里面可以放多个控件,并且可以自己定义控件的x,y的位置)

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>


 表示的是id为f1的控件叠加在id为iv1的控件上面显示 (LinearLayout 和 RelativeLayout 应该又是其中用的较多的两种。AbsoluteLayout 比较少用,因为它是按屏幕的绝对位置来布局的如果屏幕大小发生改变的话控件的位置也发生了改变。这个就相当于HTML中的绝对布局一样,一般不推荐使用 )  

注意事项:

        1 、各布局不要乱用各自的属性。比如把属于 AbsoluteLayout 布局的android:layout_x和android: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     //设置text与drawable(图片)的间隔,与drawableLeft、 drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果

    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:selectAllOnFocus        //如果文本是可选择的,让他获取焦点而不是将光标移动为文本的开始位置或者末尾位置。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用到的一些重要的属性:第一类:属性值为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              //离某元素上边缘的距离 EditText的android: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 使当前控件的右端和父控件的右端对齐。这里属性值只能为true或false,默认false。 android:scaleType: android:scaleType是控制图片如何resized/moved来匹对ImageView的size。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 = "#ff$200"                 //字体颜色            

    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|stateHiddenandroid:windowSoftInputMode="adjustUnspecified|stateHidden"

       方法二:

         让 EditText失去焦点,使用EditText的clearFocus方法

            edit.clearFocus();

       方法三:

             强制隐藏Android输入法窗口

             例如:EditText edit=(EditText)findViewById(R.id.edit);

               InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

               imm.hideSoftInputFromWindow(edit.getWindowToken(),0);

 

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

        例:EditText edit=(EditText)findViewById(R.id.edit);

          edit.setInputType(InputType.TYPE_NULL);

 

Button   继承自 VIEW , VIEW 有的属性它都能用

 

<?xml version="1.0" encoding="utf-8"?> 
    <selector 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>
</selector>


注:

<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>


 

CheckBox、RadioGroup、Spinner、TimePicker、ScrollView、ProgressBar、RatingBar、ImageView、ImageButton          android:background="#00000000"     //设置背景图空白的部分直接透视背景

ImageSwicher&Gallery、GradView、Tab、Menu


猜你喜欢

日历
«    2024年3月    »
123
45678910
11121314151617
18192021222324
25262728293031
标签列表
最近发表
友情链接