记录安卓基础控件的相关属性和常用功能(一)--TextView、Button

目录 

TextView

Button


TextView

  • TextView的基础属性

 layout_width:组件的宽度                                         layout_height:组件的高度

 id:为TextView设置一个组件id                                  text:设置显示的文本内容

 textColor:设置字体颜色                                           textSize:字体大小,单位一般用sp

 textStyle:设置字体风格,三个可选值,normal(无效果)、bold(加粗)、italic(斜体)

 background:控件的背景颜色,可以理解为填充整个控件的颜色,可以是图片

 gravity:设置控件中内容的对齐方向

  • 实现阴影效果的TextView

 android:shadowColor:设置阴影颜色,需要与shadowRadius一起使用

 android:shadowRadius:设置阴影的模糊程度。设为0.1就变成字体颜色了,建议使用3.0

 android:shadowDx:设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置

 android:shadowDy:设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置

  • 实现跑马灯效果的TextView 

 android:singleLine:内容单行显示                        android:focusable:是否可以获取焦点

 android:focusableInTouchMode:用于控制视图在触摸模式下是否可以聚焦

 android:ellipsize:在哪里省略文本

 android:marqueeRepeatLimit:字幕动画重复的次数

Button

  •  目前最新版本里Button的常见问题(按钮颜色不发生变化)

 android:background="@color/black" 使用该语句改变按钮的颜色,发现按钮颜色并没有发生变化,如图1.1所示。

 图1.1

 解决该问题,需要修改value中的themes里的相关内容,如图1.2所示:

如图1.2

修改完的效果如图1.3所示,可以看出按钮颜色已经成功发生改变:

 如图1.3

  •   Button的常用效果(如点击与未点击显示图片不一样)

      Button 按下按钮时不同的按钮颜色或背景需要使用StateListDrawable,StateListDrawable是Drawable资源的一种,可以根据不同的状态,设置不同的图片效果,关键节点<selector>,我们只需要将Button的background属性设置为该drawable资源即可轻松实现。

state_focused:是否获得焦点                                   state_pressed:控件是否被按下

state_enabled:控件是否可用                                   state_checked:控件是否被勾选

state_selected:控件是否被选择,针对有滚轮的情况

state_checkable:控件可否被勾选                            state_active:控件是否处于活动状态

state_window_focused:是否获得窗口焦点

state_single:控件包含多个子控件时,确定是否只显示一个子控件

state_first:控件包含多个子控件时,确定第一个子控件是否处于显示状态

state_middle:控件包含多个子控件时,确定中间一个子控件是否处于显示状态

state_last:控件包含多个子控件时,确定最后一个子控件是否处于显示状态

 具体实现过程记录:

btn_selector里面的完整代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/ic_baseline_accessibility_24" android:state_pressed="true"></item>
    <item android:drawable="@drawable/ic_baseline_accessible_24"></item>
</selector>

 控件的完整代码:

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_selector"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" >
</Button>

 以上便可实现按钮在点击与未点击图片的改变。

若想实现点击与未点击图片颜色的变化该怎么实现呢?

具体操作流程如下:

 

之后创建color Resource File文件:

 btn_color_selector里面的完整代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:color="#4CAF50" android:state_pressed="true"></item>
    <item android:color="@color/black"></item>
</selector>

控件的完整代码:

 <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_selector"
        android:backgroundTint="@color/btn_color_selector"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" >
 </Button>

以上便是对TextView、Button的相关知识的整合,希望可以帮助到大家!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值