常见XML属性解析

属性 描述
android:id android:id的设置,通常有三种方式,详见下文
android:layout_width 控件宽度
android:layout_height 控件高度
android:padding 内边距
android:margin 外边距
android:layout_weight 权重
android:layout_gravity 相对重力
android:gravity 本身位置
android:visbility 可视性
android:background 背景
android:onClick 点击属性
android:foucusable 焦点
android:foucsableInTouchMode 焦点

android:id

android:id的设置,通常有三种方式,分别是以下这三种:
android:id=”@+id/xxx”
android:id=”@android:id/tabhost”
android:id=”@id/xxx”

(1)android:id=”@+id/xxx”
表示在R.java文件里面新增一个id为xxx的控件索引,最常用的一种声明控件id的方式。
代码获取方式:findViewById(R.id.xxx);

(2) android:id=”@android:id/tabhost”
表示引用的是系统已有的ID,在对应的sdk目录下的ids.xml里面。一般外部不去调用,是组件内部调用的时候使用。

(3)android:id=”@id/xxx”
表示引用一个已经存在的ID,在R.java里面的,比如我们自己建了一个ids.xml,里面声明了一组id,其中一个是xxx,那么你就可以这样引用了。
代码获取方式同(1)

android:layout_width和android:layout_height

所有控件必须指定:android:layout_width和android:layout_height属性。这两个属性有以下三种形式:
a. 具体的大小,如:100px;
b. wrap_content(包含内容),表示控件应该保持原来大小;
c. fill_parent(填充父元素),表示在处理完所有其他控件之后,当前控件应该填满包含它的容器的所有空用空间。

android:layout_weight

权重

android:layout_weight属性:表示为相应控件分配的空间比例。其默认值为0,
如果一个控件设置为1,另一个为2,那么第二个控件占用的空间是第一个的两倍。
另一种方式是以百分比为单位,使用百分比有下面三个步骤:
a. 将布局中控件的layout_width设置为0;
b. 将控件设置成想要的百分比;
c. 保证所有这些控件的百分比和为100.

weight是线性布局的一个独特的属性,我们可以使用这个属性来按照比例对界面进行分配,完成一些特殊的需求。
但是,我们对于这个属性的计算应该如何理解呢?
首先看下面的例子,我们在布局中这样设置我们的界面

我们在布局里面设置为线性布局,横向排列,然后放置两个宽度为0dp的按钮,分别设置weight为1和2,在效果图中,我们可以看到两个按钮按照1:2的宽度比例正常排列了,这也是我们经常使用到的场景,这是时候很好理解,Button1的宽度就是1/(1+2) = 1/3,Button2的宽度则是2/(1+2) = 2/3,我们可以很清楚的明白这种情景下的占比如何计算。
但是假如我们的宽度不是0dp(wrap_content和0dp的效果相同),则是match_parent呢?
下面是设置为match_parent的效果

我们可以看到,在这种情况下,占比和上面正好相反,这是怎么回事呢?说到这里,我们就不得不提一下weight的计算方法了。
android:layout_weight的真实含义是:如果View设置了该属性并且有效,那么该 View的宽度等于原有宽度(android:layout_width)加上剩余空间的占比。
从这个角度我们来解释一下上面的现象。在上面的代码中,我们设置每个Button的宽度都是match_parent,假设屏幕宽度为L,那么每个Button的宽度也应该都为L,剩余宽度就等于L-(L+L)= -L。
Button1的weight=1,剩余宽度占比为1/(1+2)= 1/3,所以最终宽度为L+1/3*(-L)=2/3L,Button2的计算类似,最终宽度为L+2/3(-L)=1/3L。
这是在水平方向上的,那么在垂直方向上也是这样吗?
下面是测试代码和效果
如果是垂直方向,那么我们应该改变的是layout_height的属性,下面是0dp的显示效果

下面是match_parent的显示效果,结论和水平是完全一样的

虽然说我们演示了match_parent的显示效果,并说明了原因,但是在真正用的时候,我们都是设置某一个属性为0dp,然后按照权重计算所占百分比。

android:padding

内边距
通过android:padding属性可以为部件的四边设置内边距。
属性: android:padding 、android:paddingLeft(左边距)、android:paddinRight(右边距)、android:paddinTop(上边距)、android:paddinBottom(下边距),单位是px,如:5px。

android:margin 和android:layout_margin

Margin 为外边框

padding与margin区别

padding是站在父view的角度描述问题,它规定它里面的内容必须与这个父view边界的距离。margin则是站在自己的角度描述问题,规定自己和其他(上下左右)的view之间的距离,如果同一级只有一个view,那么它的效果基本上就和padding一样了。

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

horizontal 都是操作的水平方向,即横向, vertical 都是炒作的垂直方向,即纵向。

对于LinearLayout何时生效的问题

参看:
也谈layout_gravity和gravity

对于 LinearLayout

当 android:orientation=”vertical” 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。

当 android:orientation=”horizontal” 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。

android:visbility

VISIBLE:设置控件可见

INVISIBLE:设置控件不可见

GONE:设置控件隐藏

而INVISIBLE和GONE的主要区别是:当控件visibility属性为INVISIBLE时,界面保留了view控件所占有的空间;而控件属性为GONE时,界面则不保留view控件所占有的空间。

可见(visible)

XML文件:android:visibility=”visible”

Java代码:view.setVisibility(View.VISIBLE);

不可见(invisible)

XML文件:android:visibility=”invisible”

Java代码:view.setVisibility(View.INVISIBLE);

隐藏(GONE)

XML文件:android:visibility=”gone”

Java代码:view.setVisibility(View.GONE);

android:onClick

Represents a push-button widget. Push-buttons can be pressed, or clicked, by the user to perform an action.

A typical use of a push-button in an activity would be the following:

 public class MyActivity extends Activity {     protected void onCreate(Bundle icicle) {         super.onCreate(icicle);         setContentView(R.layout.content_layout_id);         final Button button = (Button) findViewById(R.id.button_id);         button.setOnClickListener(new View.OnClickListener() {             public void onClick(View v) {                 // Perform action on click             }         });     } }

However, instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick attribute. For example:

 <Button  android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/self_destruct" android:onClick="selfDestruct" />

Now, when a user clicks the button, the Android system calls the activity’s selfDestruct(View) method. In order for this to work, the method must be public and accept a View as its only parameter. For example:

 public void selfDestruct(View view) {     // Kabloey }

The View passed into the method is a reference to the widget that was clicked.

android:foucusable和android:foucsableInTouchMode

官方touch-mode

当用户直接使用keys或trackball与UI进行交互的时候, 必须先使目标控件获取焦点(比如按钮),这样用户才会注意到是什么控件接收输入. 然而如果设备支持触摸手势的话, 用户可能使用触摸屏与UI进行交互, 这个时候就没有必要将目标控件高亮显示了(即,获取焦点). 因此就产生了这样一种交互模式叫”touch mode .”

对于一个拥有触摸屏功能的设备而言, 一旦用户用手点击屏幕, 设备立刻进入touch mode . 这时候被点击的控件只有isFocusableInTouchMode()方法返回true的时候才会 focusable , 比如EditText控件. 其他可以触摸的控件, 比如按钮, 当被点击的时候不会获取焦点; 它们只是简单地执行onClick事件而已.

任何时候只要用户点击key或滚动trackball, 设备就会退出touch mode ,并且找一个view将焦点置于其上. 此时用户可以不使用触摸手势了.

touch mode 在整个系统运行期间都是有效的(在任何activities中). 如果想要查询当前处于何种状态, 你可以调用View#isInTouchMode()来看看当前是否处于touch mode .

获取焦点,我们只需要设置
android:foucusableInTouchMode=“true”就可以了。
所有的获取焦点,都要有一个前提,那就是该控件必须设置android:clickable=”true”,如果都点击不了,设置焦点应该没什么意义了吧。

android:background

1.指定颜色

android:background="@color/mycolor"

These colors can be defined in the res/values/colors.xml file (see here how to do this).

You can also define a color directly at the attribute (android:background=”#ffff0000”), but that’s usually not good. By defining the colors in the XML file you can give it a descriptive name (improves code readability) and you can reuse it somewhere else.

2.指定图片

android:background="@drawable/backgroud"

更多相关文章

  1. android:使用fragment实现tab切换
  2. TextView跑马灯必成五属性
  3. android圆形进度条颜色的设置
  4. Android(安卓)xml资源文件中@、@android:type、@*、?、@+含义和区
  5. Android中Button的使用
  6. android ImageView的属性android:scaleType,即ImageView.setScale
  7. Android实现LIstView条目单选和多选RadioButton
  8. android selector状态详解
  9. android FrameLayout响应了下层view的点击事件

随机推荐

  1. Android(安卓)限制EditText只能输入数字
  2. 学习Android从0开始之背景篇-Android系统
  3. android源码学习之animation1
  4. Android(安卓)xml资源文件中@、@android:
  5. Android相对布局RelativeLayout各属性介
  6. Android(安卓)TextView 文字居中
  7. Android:控件样式触发
  8. Android系统自带样式(@android:style/)
  9. Android中TextView中加图片,超链接,部分字
  10. Android(安卓)EditText inputType同时设