来个Button看一看
0.目录
1.前言
2.基本属性与方法
3.点点更健康
4.我的Button有点多
5.震惊!TextView竟然...
1.前言
每次写代码总会忘记一些东西,又要重新Goooooooooogle,好烦呐~
本文参考网站(排名不分先后):
1.Android Button的基本使用
2.Android中设置文本颜色的三种方法
3.android:layout_gravity和android:gravity的区别
2.基本属性与方法
Button 支持的 XML 属性及相关方法:
XML属性 | 相关方法 | 说明 |
---|---|---|
android:id | findViewById | 在XML中设置id,然后在.java中才可以调用这个按钮做其他事 |
android:text | setText() | 设置文字 |
android:textColor | setTextColor() | 设置文字颜色 |
android:textSize | setTextSize() | 设置文字大小 |
android:background | setBackground() | 设置背景颜色或者背景图片 |
android:enabled | setEnabled() | 设置按钮是否可以被点击 |
android:layout_gravity | 设置按钮的位置 | |
android:gravity | 设置文字的位置 |
以下用实例来讲解:
XML文件为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.pylearn_01_1.MainActivity" >
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自古一楼没卵用" />
<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="使用XML布局"
android:textColor="@android:color/white"
android:textSize="50sp" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/background_dark" />
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可远观而不可亵玩"
android:enabled="false" />
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用layout_gravity让按钮居中"
android:layout_gravity="center" />
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用gravity让文字居中"
android:gravity="center" />
</LinearLayout>
java文件为:
package com.example.pylearn_01_1;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends Activity {
/* pylearn_01_1
* Button基本属性与方法
*/
Button btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn3=(Button)findViewById(R.id.btn3);
btn3.setText("使用java布局");//设置文字内容
btn3.setTextColor(android.graphics.Color.RED);//设置文字颜色
btn3.setTextSize(45);//设置文字大小
btn3.setEnabled(false);//设置按钮不能被点击
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
eclipse中看xml为:
模拟器中运行结果为:
源代码在此:pylearn_01_1
3.点点更健康
Button弄出来当然不是为了当花瓶的,咱们需要通过点击它来完成一些事情。
按钮的点击事件可以使用
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//do something
}
});
来实现。
源代码在此:pylearn_01_2
也可以使用
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
然后让MainActivity extends Activity implements OnClickListener
实现onClick方法:
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
switch ( v.getId() ) {
case R.id.btn1:
fun_btn1();
break;
case R.id.btn2:
fun_btn2();
break;
}
}
源代码在此:pylearn_01_3
这两种方法都可以实现按钮点击事件的处理。
4.我的Button有点多
如何实现多个Button平分天下:
使用android:layout_weight控制各个按钮的权重,然后在parent布局中控制好权重和android:weightSum。最后设置各个按钮的占比为android:layout_width="0dp"。这样就实现了按钮的多个按钮的平均分配。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.pylearn_01_1.MainActivity" >
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="4" >
<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<Button
android:id="@+id/btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
<Button
android:id="@+id/btn4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4" />
</LinearLayout>
</LinearLayout>
效果如下:
源代码在此:pylearn_01_4
5.震惊!TextView竟然...
忘记在哪看到的一句话:
能用TextView的地方就别用Button
Button能实现的基本上TextView也能实现
有兴趣可以试试:把上面所有程序中的Button换成TextView,毫无违和感。
来个Button看一看的更多相关文章
- 瞧一瞧,看一看呐,用MVC+EF快速弄出一个CRUD,一行代码都不用写,真的一行代码都不用写!!!!
瞧一瞧,看一看呐用MVC+EF快速弄出一个CRUD,一行代码都不用写,真的一行代码都不用写!!!! 现在要写的呢就是,用MVC和EF弄出一个CRUD四个页面和一个列表页面的一个快速DEMO,当然是在不 ...
- 2015年4月27日---C语言:输出特殊图案,请在c环境中运行,看一看,Very Beautiful!
---恢复内容开始--- 题目:输出特殊图案,请在c环境中运行,看一看,Very Beautiful! 1.程序分析:字符共有256个.不同字符,图形不一样. 2.程序源代码: [code=c] #i ...
- Loj#6183. 看无可看
Loj#6183. 看无可看 题目描述 首先用特征根求出通项公式\(A_n=p\cdot 3^n+q\cdot(-1)^n\).通过给定的\(f_0,f_1\)可以解出\(p,q\). 然后我们要求的 ...
- 看无可看 分治FFT+特征值方程
题面: 看无可看(see.pas/cpp/c) 题目描述 “What’s left to see when our eyes won’t open?” “若彼此瞑目在即,是否终亦看无可看?” ---- ...
- Scrum模拟微信看一看“疫情专区”的敏捷开发过程
无论作为产品用户还是管理咨询顾问,都非常非常喜欢微信.自认感情比较克制属于“高冷”挂,但从很多方面都太佩服太崇拜张小龙了(新书里微信也会是最喜欢的案例之一,真的不只是一个产品而已,很多方面都太牛了). ...
- Mysql数据库优化技术之配置篇、索引篇 ( 必看 必看 转)
转自:Mysql数据库优化技术之配置篇.索引篇 ( 必看 必看 ) (一)减少数据库访问对于可以静态化的页面,尽可能静态化对一个动态页面中可以静态的局部,采用静态化部分数据可以生成XML,或者文本文件 ...
- iOS - 基础知识总结(OC版) 面试必看 再不看就要用swift了
OC的理解与特性 OC作为一门面向对象的语言,自然具有面向对象的语言特性:封装.继承.多态.它既具有静态语言的特性(如C++),又有动态语言的效率(动态绑定.动态加载等).总体来讲,OC确实是一门不错 ...
- 今天做项目用到框架,关于angual,然后自己整理了一番,自己上网也看了看。
1. Angular 1.1. 库与框架的区别 jQuery:库 库一般都是封装了一些常用的方法 自己手动去调用这些方法,来完成我们的功能 $('#txt').val('我是小明'): $('div' ...
- PHP_Bibel阅读学习(一)——看书看经典,写文写代码
基础快速再看一下,然后每天有新的好玩的看. 这本书,反正好评不少,就是`PHP和MySQL Web开发`,机械工业出版社,澳洲人写的,红皮,有兴趣的可以看一下. 第一篇 使用PHP 一.入门 5分钟翻 ...
随机推荐
- hadoop2集群中关键配置文件的记录
配置HDFS 高可用 1.配置HDFS配置文件 $ vi hdfs-site.xml #写入 <configuration> #配置NameService 名字随便起 <prope ...
- setImmediate()
在循环事件任务完成后马上运行指定代码 以前使用 setTimeout(fn, 0); Since browsers clamp their timers to 4ms, it really d ...
- vue初级学习--路由router的编写(resolve的使用)
一.导语 最近在用vue仿写淘宝的商品详情页面以及加入购物车页面,若是成功了,分享给大家~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 二.正文 我先用控制台创建了vue项目demo(如 ...
- MQTT——发布报文
发布报文的知识点并不难,只是多.看过前面几章的读者们应该或多或少都认识服务质量QOS.发布报文跟他的联系最紧的.我们也清楚订阅报文里面虽然也有用到QOS,但是他却没有更进一步的联系.往下看就知道是什么 ...
- Python学习--字典
在Python中通过名字来引用值的数据结构称为映射(mapping).字典是Python中唯一内建(Python解释器本身支持,不需要import)的映射类型.字典中的值没有特殊的顺序,都存储在一个特 ...
- IE浏览器中用Firebug调试网站的方法
对于大部分做前端设计者而言应该都使用过Firefox浏览器下一款调试网站的扩展插件firebug吧,功能非常的强大,对于我们找出网页兼容性的问题非常的有效.不过对于很多不喜欢使用Firefox浏览器的 ...
- 利用C#转换图片格式及转换为ico
注意:转换为ICO后效果不好. 源代码: using System;using System.Collections.Generic;using System.Text;using System.Dr ...
- python基础-------函数(二)
闭包函数与装饰器 一.闭包函数 闭包函数:函数内部定义函数,成为内部函数,该内部函数包含对外部作用域,而不是对全局作用域名字的引用那么该内部函数成为闭包函数 name='alex' # def fun ...
- javascript第七章--DOM
① 节点层次 ② DOM操作技术
- .net core2.0下使用Identity改用dapper存储数据
前言. 已经好多天没写博客了,鉴于空闲无聊之时又兴起想写写博客,也当是给自己做个笔记.过了这么些天,我的文笔还是依然那么烂就请多多谅解了.今天主要是分享一下在使用.net core2.0下的实际遇到的 ...