首页 技术 正文
技术 2022年11月23日
0 收藏 532 点赞 3,678 浏览 4781 个字

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方法:

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,505
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844