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,毫无违和感。

最新文章

  1. 25+ Useful Selenium Web driver Code Snippets For GUI Testing Automation
  2. (喷血分享)利用.NET生成数据库表的创建脚本,类似SqlServer编写表的CREATE语句
  3. 利用DBCC PAGE查看SQL Server中的表和索引数据
  4. Java中List Set Map 是否有序等总结
  5. 经典DP 二维换一维
  6. Python进阶06 循环对象
  7. nodejs学习--express篇
  8. 使用 环境变量 来配置批量配置apache
  9. [RxJS] Stopping a Stream with TakeUntil
  10. Android 网络通信框架Volley基本介绍
  11. ajax编程**
  12. 练手项目:利用pygame库编写射击游戏
  13. 全局Threshold和动态阈值分割Dyn_Threshold的应用场景
  14. 我的海外购页面List
  15. 四则运算生成程序——GUI支持和部分功能改进
  16. TTPRequest 提示#import &lt;libxml/HTMLparser.h&gt;找不到 的解决方法
  17. 教你调用数据库读取短信 记事本 通讯录文件,让ios5的短信恢复到ios4
  18. Website Develop: Handler “PageHandlerFactory-Integrated” has a bad module “ManagedPipelineHandler” in its module list
  19. 更换主机后SSH无法登录的问题
  20. JavaWeb基础—上传与下载

热门文章

  1. SQL2005清空删除日志
  2. common lisp和scheme的区别
  3. web前端工程师全套教程免费分享
  4. js解析器的执行原理
  5. iOS 输入时键盘处理问题
  6. 基于Spring、SpringMVC、MyBatis、Druid、Shrio构建web系统
  7. CodeForces - 846F Random Query(期望)
  8. Android 开发笔记___SQLite__基本用法
  9. 《剑指offer》面试题的Python实现
  10. java删除数组中的第n个数