zoukankan      html  css  js  c++  java
  • textview设置drawable

    textview可以在上下左右四个方向添加图片,同时也可以动态改变这些图片;

     
    下面有我写的一个例子:

    在xml文件中:
     <TextView
                    android:id="@+id/day_night_mode_tv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:background="@drawable/btn_sidebar_item"
                    android:clickable="true"
                    android:drawableTop="@drawable/ic_day_night_mode_auto"
                    android:gravity="center"
                    android:paddingBottom="5dp"
                    android:paddingTop="5dp"
                    android:text="自动"
                    android:textColor="@color/day_night_mode_text_color" />
     
    在代码中动态改变图片:
        在代码中如果要修改drawableRight设置的图片可以使用
          setCompoundDrawables(Drawable left,Drawable top,Drawable right,Drawable bottom)
         Drawable可以通过 Drawable drawable =getResources().getDrawable(R.drawable.button_nav_up);得到

         但是API提示,setCompoundDrawables() 调用的时候,Drawable对象必须调用setBounds(int left, int top, int right, int bottom)方法

        具体如下:

      if (mDaynightModeInt % 3 == 0) {

        // 自动

        mDaynightModeTv.setText("自动");

        Drawable drawable = getResources().getDrawable(

          R.drawable.ic_day_night_mode_auto);

        drawable.setBounds(0, 0, drawable.getMinimumWidth(),

          drawable.getMinimumHeight());

        mDaynightModeTv

          .setCompoundDrawables(null, drawable, null, null);

     

       } else if (mDaynightModeInt % 3 == 1) {

        // 日间模式

        mDaynightModeTv.setText("日间模式");

        Drawable drawable = getResources().getDrawable(

          R.drawable.ic_day_night_mode_day);

        drawable.setBounds(0, 0, drawable.getMinimumWidth(),

          drawable.getMinimumHeight());

        mDaynightModeTv

          .setCompoundDrawables(null, drawable, null, null);   

       } else {

        // 夜间模式

        mDaynightModeTv.setText("夜间模式");

        Drawable drawable = getResources().getDrawable(

          R.drawable.ic_day_night_mode_night);

        drawable.setBounds(0, 0, drawable.getMinimumWidth(),

          drawable.getMinimumHeight());

        mDaynightModeTv

          .setCompoundDrawables(null, drawable, null, null);

       }

  • 相关阅读:
    堆排序,C++模板编程
    洗牌程序的两种实现方法比较
    读取/保存xml文件的类(序列化/反序列化)
    [返回上一页,并且刷新]
    实现等级的存储过程sql
    C#实现WMI读取远程计算机信息【原】
    开源IT资产管理系统>OCS Inventory NG服务端
    xaf实现自定义只读参数
    How to show a Detail View via code
    15个最好的免费开源电子商务平台
  • 原文地址:https://www.cnblogs.com/shenchanghui/p/4933458.html
Copyright © 2011-2022 走看看