`
sarin
  • 浏览: 1749023 次
  • 性别: Icon_minigender_1
  • 来自: 大连
博客专栏
E3b14d1f-4cc5-37dd-b820-b6af951740bc
Spring数据库访问系列...
浏览量:172903
C2083dc5-6474-39e2-993e-263652d27795
Android学习笔记
浏览量:366712
5f40a095-b33c-3e8e-8891-606fcf3b8d27
iBatis开发详解
浏览量:188408
B272a31d-e7bd-3eff-8cc4-c0624ee75fee
Objective-C学习...
浏览量:98874
社区版块
存档分类
最新评论

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

阅读更多
    接上文
   Button组件也是我们前面看到过的一个简单组件,这里我们来进行深入的介绍。按钮的基本功能就是供用户点击,然后产生一些效果,比如Web开发中的登录按钮,点击之后即可实现登录效果。
    这里我们没有对Button的事件处理操作,还是简单的了解Button的配置。首先来看一下Button的文档:

java.lang.Object
   ↳ android.view.View
   ↳ android.widget.TextView
   ↳ android.widget.Button

   可以看到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是极其类似的,因为从类的继承结构上就不难得出这个结论。这里我们也是设置了按钮组件显示的文本,文本的颜色以及文本的大小,下面运行程序来看一下具体效果:

    再来看第二个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的显示文本和文字大小,再次运行程序,这次在横屏下显示,来看一下效果:

    可以看到最终的效果就是上边距为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个字符,之后运行程序,我们可以看到如下的显示效果:

    要注意的是这里如果设置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>

   这样,上面的程序代码就可以执行了,效果如下:

    至此,我们已经看到了最后的显示效果,我们把一个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):

    这部分的演示代码请参考附件。
    接下文
  • 大小: 66.5 KB
  • 大小: 28.2 KB
  • 大小: 39.3 KB
  • 大小: 39.7 KB
  • 大小: 48.7 KB
  • 大小: 48.8 KB
6
0
分享到:
评论
1 楼 qianguming 2013-09-29  
紧紧跟随4

相关推荐

Global site tag (gtag.js) - Google Analytics