关于android:Android入门教程-ButtonTextView背景设置

2次阅读

共计 4086 个字符,预计需要花费 11 分钟才能阅读完成。

Button 按钮

Button 能对用户的点击行为作出反应。
在 xml 文件中搁置一个 button。

<Button
     android:id="@+id/btn"
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="@string/self_destruct" />

要想监听到 button 的点击事件,咱们在 Activity 中进行设置。

public class MyActivity extends Activity {protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);


         setContentView(R.layout.content_layout_id);

         final Button button = findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// 这里是按钮点击事件,在主线程执行}
         });
     }
 }

下面咱们用到了 View.OnClickListener。button 被点击后会执行onClick 办法。零碎会在 App 的主线程中执行 onClick 办法。咱们能够在这外面更新 UI。但不要做太耗时的操作。

咱们留神到 OnClickListener 其实是 View 中的一个接口。setOnClickListener 也是 View 的一个办法。换句话说,就算咱们这里用的不是 button,也能够用这样的形式来监听点击事件。即View.setOnClickListener(View.OnClickListener())

当前会遇到 TextView,ImageView 监听点击事件,或是整个 Layout 来监听点击事件。这里应用的是监听器模式。

实际上,Button 继承自 TextView。

Button,TextView 背景设置

如何给按钮减少动感?

Button 有按下 (pressed) 和未按下之分,咱们可给这 2 种状态不同的背景色彩和文字色彩。本文要介绍的是 selector,即状态列表。和后面的 shape 相似,selector 也是一个 xml 文件。

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

</selector>

设置 Button 背景

  • 筹备 shape 文件

先筹备 shape 文件。这里筹备 3 个 shape。别离代表 3 个不同的状态。

shape_btn_1_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#6CB3DD" />
    <corners android:radius="2dp" />
</shape>

shape_btn_1_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#1E6283" />
    <corners android:radius="4dp" />
</shape>

shape_btn_disable.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#6D6D6D" />
</shape>
  • 新建 selector 文件

新建 drawable 文件bg_1.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shape_btn_disable" android:state_enabled="false" />
    <item android:drawable="@drawable/shape_btn_1_normal" android:state_enabled="true" android:state_pressed="false" />
    <item android:drawable="@drawable/shape_btn_1_pressed" android:state_enabled="true" android:state_pressed="true" />
</selector>
  • 设置 Button 背景

在 layout 中设置背景。

<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_1"
        android:text="RFDev btn 1" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:background="@drawable/bg_1"
        android:enabled="false"
        android:text="RFDev btn 2" />

运行一下看看成果。按下按钮和没按下的时候,按钮的背景色彩是不同的。

selector 介绍

StateListDrawable 是在 XML 文件中定义的可绘制对象,它会依据对象状态,应用多个不同的图像来示意同一个图形。

例如,Button 微件能够是多种不同状态(按下、聚焦或这两种状态都不是)中的其中一种,并且可利用状态列表可绘制对象,为每种状态提供不同的背景图片。能够在 XML 文件中形容状态列表。每个图形由繁多 <selector> 元素内的 <item> 元素示意。每个 <item> 均应用各种属性来形容利用作可绘制对象的图形的状态。在每个状态变更期间,将从上到下遍历状态列表,并应用第一个与以后状态匹配的我的项目 — 此抉择并非基于“最佳匹配”,而是抉择合乎状态最低条件的第一个我的项目。selector 指向的是 StateListDrawable,这里咱们不关注 StateListDrawable,只关注 xml。

语法

参考如下语法:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize=["true" | "false"]
    android:dither=["true" | "false"]
    android:variablePadding=["true" | "false"] >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_hovered=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_activated=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>

应用注意事项

咱们能够给 Button 设置背景,也能够给 TextView 设置同样的背景。

<TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="12dp"
        android:background="@drawable/bg_1"
        android:gravity="center"
        android:text="RFDev text view 1" />

bg_1中设置了 state_pressed 的。如果 TextView 没有设置点击事件,用户点击或按着这个 TextView 是不会产生背景变动的。给 TextView 设置点击事件后,再点击就能够看到背景变动了。

本文咱们用 Button 和 TextView 来做例子。实际上 View 的其它子类,比方 ImageView,LinearLayout 都能够用这种形式设置背景。

Android 零根底入门教程视频参考

正文完
 0