Android学习笔记五:基本视图组件:Button

[url=http://sarin.iteye.com/blog/1567318]接上文[/url]
Button组件也是我们前面看到过的一个简单组件,这里我们来进行深入的介绍。按钮的基本功能就是供用户点击,然后产生一些效果,比如Web开发中的登录按钮,点击之后即可实现登录效果。
这里我们没有对Button的事件处理操作,还是简单的了解Button的配置。首先来看一下Button的文档:
[img]http://dl.iteye.com/upload/attachment/0071/0288/0da541cf-50e8-3638-b5bd-8d11a21958a3.jpg[/img]
[b]java.lang.Object
↳ android.view.View
↳ android.widget.TextView
↳ android.widget.Button[/b]
可以看到Button类是TextView类的子类,而不是直接继承自View类的。可以说Button是一个特殊的TextView,下面在Eclipse中新建一个项目来演示Button组件:

<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是一个Button组件"
android:textColor="#FDF5E6"
android:textSize="16dp" />

可以看出,Button组件的配置和TextView是极其类似的,因为从类的继承结构上就不难得出这个结论。这里我们也是设置了按钮组件显示的文本,文本的颜色以及文本的大小,下面运行程序来看一下具体效果:
[img]http://dl.iteye.com/upload/attachment/0071/0291/4d830421-ecf3-38b9-914f-a1815abc05e0.jpg[/img]
再来看第二个Button示例:

<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="点击访问: http://www.google.com.hk"
android:textSize="10dp" />

这个Button组件中,我们设置layout_width为wrap_content,也就是包裹文字大小,而不是充满屏幕。同时设置了layout_gravity属性为center,也就是居中显示,而layout_marginTop的意思就是距离上个组件的上边距为20dp,这和CSS中的概念也是一致的。重新设置Button的显示文本和文字大小,再次运行程序,这次在横屏下显示,来看一下效果:
[img]http://dl.iteye.com/upload/attachment/0071/0293/192f8c13-b72f-368c-8c1c-9c31773f3cc9.jpg[/img]
可以看到最终的效果就是上边距为20dp,而左右边距没有变化,仅仅是包裹文字的宽度来显示的。
下面再来看一个示例:
    <Button
andr
oid:id="@+id/btn3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="15dp"
android:maxLength="14"
android:text="Powered By Nan Lei"
android:textSize="12dp" />

这里我们调整了layout_width为fill_parent,而设置layout_margin为15dp,也就是上下左右边距都为15dp,再设置最大的显示长度为14个字符,之后运行程序,我们可以看到如下的显示效果:
[img]http://dl.iteye.com/upload/attachment/0071/0295/2ad13457-90f5-3990-a35a-532458c4c551.jpg[/img]
要注意的是这里如果设置layout_width为wrap_content,那么就不会得到左右边距15dp的效果,而仅仅是包裹文字了。具体所需的显示效果,我们可以根据具体需求去详细调整。
从上面三个例子中可以很容易看出,Button组件的操作和TextView是基本一致的。最后我们来看看在程序中动态生成按钮,代码如下:
package org.ourpioneer;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
public class ButtonDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
LinearLayout layout = (LinearLayout) super.findViewById(R.id.layout);
Button btn = new Button(this);
btn.setText("我是在程序中动态创建的按钮");
layout.addView(btn);
}
}

程序代码很简单,但是要说一点就是,我们通过findViewById()方法获取布局管理器时,要在XML中为该布局管理器设置id,也就是说,在main.xml中,我们要修改代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
......
</LinearLayout>

这样,上面的程序代码就可以执行了,效果如下:
[img]http://dl.iteye.com/upload/attachment/0071/0297/afb13613-4b78-30d3-8e29-393fb5bf0f93.jpg[/img]
至此,我们已经看到了最后的显示效果,我们把一个Button动态的添加到了布局管理器中,此时来看看R.java中都有什么:
package org.ourpioneer;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int btn1=0x7f050001;
public static final int btn2=0x7f050002;
public static final int btn3=0x7f050003;
public static final int layout=0x7f050000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}

可以看到我们为布局管理器加的ID也显示出来了。
最后,我们将该程序安装到Android设备上来具体看看效果(运行设备为Motorola Defy+ Android 2.3.7 MIUI):
[img]http://dl.iteye.com/upload/attachment/0071/0299/4eebbb6e-54b5-346f-abf5-0c821d134a10.jpg[/img]
这部分的演示代码请参考附件。
[url=http://sarin.iteye.com/blog/1632124]接下文[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值