如何在Android中添加控件

如何在Android中添加控件,第1张

可以如下 *** 作: 1、在一段文字中插入n个标记,比如 2、用string.split("")方法把该文字拆分成String数组,进而把String数组转换成TextView数组tvs[n+1] 3、蓝色按钮是自定义的控件,共有n个,用for循环和LayoutInflater方法转换成cvs[n], 4、for

首先添加控件:

<Switch

    android:id="@+id/sw_sfktmmzf"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_marginRight="15dp"

    android:showText="false"

    android:switchMinWidth="50dp"

    android:thumb="@drawable/thumb"

    android:track="@drawable/track" />

以下是该控件的常用属性:

textOn:控件打开时显示的文字

textOff:控件关闭时显示的文字

thumb:控件开关的图片(设置小圆圈颜色

track:控件开关的轨迹图片(设置小圆圈背景颜色)

typeface:设置字体类型

switchMinWidth:开关最小宽度

switchPadding:设置开关 与文字的空白距离

switchTextAppearance:设置文本的风格

checked:设置初始选中状态

splitTrack:是否设置一个间隙,让滑块与底部图片分隔(API 21及以上)

showText:设置是否显示开关上的文字(API 21及以上)

创建北京控制文件在drawable文件下

1、thumb.xml

<?xml version="1.0" encoding="utf-8"?><!-- 按钮的选择器,可以设置按钮在不同状态下的时候,按钮不同的颜色 -->

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/green_thumb" android:state_checked="true" />

    <item android:drawable="@drawable/gray_thumb" />

颜色文件:

green_thumb.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle" >

    <!-- 高度40 -->

    <size android:height="@dimen/switch_height" android:width="@dimen/switch_height"/>

    <!-- 圆角弧度 20 -->

<corners android:radius="20dp"/>

<!-- 变化率 -->

    <gradient

        android:endColor="#eeeeee"

        android:startColor="#eeeeee" />

<stroke android:width="1dp"

        android:color="@color/home_text1"/>

</shape>

gray_thumb.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle" >

    <!-- 高度40 -->

    <size android:height="@dimen/switch_height" android:width="@dimen/switch_height"/>

    <!-- 圆角弧度 20 -->

<corners android:radius="20dp"/>

<!-- 变化率 -->

    <gradient

        android:endColor="#eeeeee"

        android:startColor="#eeeeee" />

    <stroke android:width="1dp"

        android:color="@color/text_color03"/>

</shape>

2、track.xml

<?xml version="1.0" encoding="utf-8"?><!-- 底层下滑条的样式选择器,可控制Switch在不同状态下,底下下滑条的颜色 -->

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/green_track" android:state_checked="true" />

    <item android:drawable="@drawable/gray_track" />

</selector>

颜色文件:

green_track.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle">

    <!-- 高度40 -->

    <size android:height="@dimen/switch_height"/>

    <!-- 圆角弧度 20 -->

    <corners android:radius="15dp"/>

    <!-- 变化率 -->

    <gradient

        android:endColor="@color/home_text1"

        android:startColor="@color/home_text1" />

</shape>

gray_track.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle">

    <!-- 高度30  此处设置宽度无效-->

    <size android:height="@dimen/switch_height" />

    <!-- 圆角弧度 15 -->

    <corners android:radius="15dp" />

    <!-- 变化率  定义从左到右的颜色不变 -->

    <gradient

        android:endColor="@color/text_color03"

        android:startColor="@color/text_color03" />

</shape>

switch 控件监听事件:

 aSwitch.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {

      @Override

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        //控制开关字体颜色

        if(isChecked) {

         //打开

        }else{

         //关闭

        }

      }

    })


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11731896.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存