您的位置:首页 > 移动开发 > Android开发

android:布局参数,控件属性及各种xml的作用

2014-10-27 01:05 429 查看
今天看别人项目的时候,突然发现自己的界面布局这块实在是弱得很,所以特意上网找了一些布局方面的文来看,下面为了便于以后翻阅,特意转载过来。

android:布局参数,控件属性及各种xml的作用

以下大部为用在RelativeLayout中的一些参数:

android:layout_above 将该控件的底部至于给定ID的控件之上,但不会左对齐,默认置于父窗口最左边,会覆盖最左边的控件

android:layout_below 将该控件的顶部至于给定ID的控件之下,但不会左对齐,默认置于父窗口最左边,会覆盖最左边的控件

android:layout_toLeftOf 将该控件的右边缘和给定ID的控件的左边缘对齐,默认置于父窗口最上面,会覆盖最上面的控件

android:layout_toRightOf 将该控件的左边缘和给定ID的控件的右边缘对齐,默认置于父窗口最上面,会覆盖最上面的控件









android:alignParentBottom 如果该值为true,则将该控件的底部和父控件的底部对齐,默认置于父窗口最左下,会覆盖最左下的控件

android:layout_alignParentLeft 如果该值为true,则将该控件的左边与父控件的左边对齐,默认置于父窗口最左上,会覆盖最左上的控件

android:layout_alignParentRight 如果该值为true,则将该控件的右边与父控件的右边对齐,默认置于父窗口最右上,会覆盖最右上的控件

android:layout_alignParentTop 如果该值为true,则将控件的顶部与父控件的顶部对齐,默认置于父窗口最左上,会覆盖最左上的控件



android:id —— 为控件指定相应的ID

android:text —— 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串

android:textSize —— 指定控件当中字体的大小

android:background —— 指定该控件所使用的背景色,RGB命名法

android:sigleLine —— 如果设置为真的话,则将控件的内容在同一行当中进行显示

android:grivity —— 指定控件的基本位置,比如说居中,居右等位置



android:layout_width="fill_parent" —— 剩余空间有多大就填充多大,如要再加别的控件,则空间没了,不会显示出来,每个控件都不能少这个

android:layout_height ="wrap_content"—— 内容多大就多大,如果不停写内容,会不断变大,最终导致别的控件不能显示,每个控件都不能少这个

上面2个也可设固定值,如android:layout_width="200px"

android:width —— 指定控件的宽度

android:height —— 指定控件的高度

当layout_width或layout_height的值为:1、固定值 2、fill_parent 时;width 和 height 的值无论设什么都不起作用。此时控件大小以layout_width和layout_height为准。

当layout_width或layout_height的值为:1、wrap_content 时;此时控件大小以width和height为准。

android:padding* —— 指定控件的内边距,也就是说控件当中的内容,下图指EditText和主窗口的间距为50px



android:layout_alignBaseline该控件的baseline和给定ID的控件的baseline对齐,并置于父窗口最左边,会覆盖最左边的控件

android:layout_alignBottom 将该控件的底部边缘与给定ID控件的底部边缘对齐,并置于父窗口最左边,会覆盖最左边的控件

android:layout_alignLeft 将该控件的左边缘与给定ID控件的左边缘对齐,并置于父窗口最上边,会覆盖最上边的控件

android:layout_alignRight 将该控件的右边缘与给定ID控件的右边缘对齐,并置于父窗口最上边,会覆盖最上边的控件

android:layout_alignTop 将给定控件的顶部边缘与给定ID控件的顶部对齐,并置于父窗口最左边,会覆盖最左边的控件



android:layout_centerHorizontal如果值为真,该控件将被至于水平方向的中央

android:layout_centerInParent 如果值为真,该控件将被至于父控件水平方向和垂直方向的中央

android:layout_centerVertical 如果值为真,该控件将被至于垂直方向的中央



android:layout_weight=""权重,如下,垂直线性布局中两个水平线性布局,权重分别为1、3,每个水平线性布局里面的值代表该textview在该水平布局里的权重。

但权重所显示的宽度不一定是固定值,当textview里的内容大于权重所占宽度时,还是会变化的。



Android控制字体大小和字体颜色:

字体大小

对于能够显示文字的控件(如TextView EditText RadioButton Button CheckBox Chronometer等等),你有时需要控制字体的大小。Android平台定义了三种字体大小。

"?android:attr/textAppearanceLarge"

"?android:attr/textAppearanceMedium"

"?android:attr/textAppearanceSmall"

使用方法为:

android:textAppearance="?android:attr/textAppearanceLarge"

android:textAppearance="?android:attr/textAppearanceMedium"

android:textAppearance="?android:attr/textAppearanceSmall"



style="?android:attr/textAppearanceLarge"

style="?android:attr/textAppearanceMedium"

style="?android:attr/textAppearanceSmall"

字体颜色

android:textColor="?android:attr/textColorPrimary"

android:textColor="?android:attr/textColorSecondary"

android:textColor="?android:attr/textColorTertiary"

android:textColor="?android:attr/textColorPrimaryInverse"

android:textColor="?android:attr/textColorSecondaryInverse"

效果如下:



ProgressBar

style="?android:attr/progressBarStyleHorizontal"

style="?android:attr/progressBarStyleLarge"

style="?android:attr/progressBarStyleSmall"

style="?android:attr/progressBarStyleSmallTitle"

其它一些参数:

android:excludeFromRecents="true"为true表示长按home键将显示此应用

android:screenOrientation="portrait" 永远竖屏显示

android:screenOrientation="portrait" 永远横屏显示

android:configChanges="keyboardHidden|orientation"竖屏切换横屏时应用也能弹出界面

分隔符

横向:

<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="?android:attr/listDivider" />


纵向:

<View android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="?android:attr/listDivider" />


CheckBox

style="?android:attr/starStyle"

类似标题栏效果的TextView

style="?android:attr/listSeparatorTextViewStyle"

其它有用的样式

android:layout_height="?android:attr/listPreferredItemHeight"

android:paddingRight="?android:attr/scrollbarSize"

style="?android:attr/windowTitleBackgroundStyle"

style="?android:attr/windowTitleStyle"

android:layout_height="?android:attr/windowTitleSize"

android:background="?android:attr/windowBackground"

在drawable-mdpi文件夹下定义button_selector.xml

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
< selector xmlns:android="http://schemas.android.com/apk/res/android">
< !-- 指定按钮按下时的图片 -->
<item android:state_pressed="true"
android:drawable="@drawable/start_down"
/>
< !-- 指定按钮松开时的图片 -->
<item android:state_pressed="false"
android:drawable="@drawable/start"
/>
< /selector></span>


在Layout中的main.xml

<span style="font-size:18px;"><Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_selector" /></span>


EditText 和 TextView 参数

android:inputType

指定输入法的类型,int类型,可以用|选择多个。取值可以参考:android.text.InputType类。取值包括:text, textUri, phone,number,等。

android:imeOptions

指定输入法窗口中的回车键的功能,可选值为normal, actionNext,actionDone,actionSearch等。部分输入法对此的支持可能不够好。

android:imeOptions="actionNext"

android:nextFocusDown="@+id/minute"

上两条加一起才能跳转到minute。

转载自:http://www.360doc.com/content/12/0410/16/9085503_202516981.shtml
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: