zoukankan      html  css  js  c++  java
  • AndroidのUI之Spinner箭头效果

    先上图:

    点击张开,再点击收回。一开始,还以为有多复杂,原来就两下搞定。

    我们知道Button可以有好多state.pressed/clicked/checked等,实现点击效果,就用state_list _drawable(忘了叫什么,反正意识差不多)好,而箭头呢?

    这个就麻烦了,首先你想到肯定是drawableRight属性,但是要和selector配合,还是难以实现。所以只要把箭头切换放在代码里面就行了。

    main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello" />
        
        <Button android:id="@+id/bt"
            android:text="开始"
            android:textColor="#fff"
            android:textSize="20sp"
            android:background="@drawable/bt_bg"
             android:layout_width="100dp"
            android:layout_height="50dp"
            />
        
    
    </LinearLayout>

    bt_bg.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        
        <item android:state_pressed="true" >
            
            <shape>
                <solid android:color="#001122"/>
            </shape>
            
        </item>
        <item  >
            
            <shape>
                <solid android:color="#221100"/>
            </shape>
            
        </item>
    
    </selector>

    MainActivity.java

    package com.bvin.del;
    
    import android.app.Activity;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
        /** Called when the activity is first created. */
        Button bt;
        boolean btIsOpen = false;
        Drawable arrowUp,arrowDown;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            initRes();
            initViews();
        }
        
        void initRes(){//
            arrowUp = getResources().getDrawable(R.drawable.ic_arrow_up_black);
            arrowDown = getResources().getDrawable(R.drawable.ic_arrow_down_black);
            
            arrowUp.setBounds(0, 0, arrowUp.getMinimumWidth(), arrowUp.getMinimumHeight());
            arrowDown.setBounds(0, 0, arrowUp.getMinimumWidth(), arrowUp.getMinimumHeight());
        }
        
        void initViews(){
            bt = (Button)findViewById(R.id.bt);
            bt.setCompoundDrawables(null, null, arrowDown, null);
            bt.setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(btIsOpen){//打开状态
                        Log.e("开关", "关闭");
                        bt.setText("关闭");
                        bt.setCompoundDrawables(null, null,arrowDown , null);
                        btIsOpen = false;
                    }else {//默认状态
                        Log.e("开关", "打开");
                        bt.setText("打开");
                        bt.setCompoundDrawables(null, null, arrowUp, null);
                        btIsOpen = true;
                    }
                }
            });
        }
    }
  • 相关阅读:
    iOS高仿微信悬浮窗、忍者小猪游戏、音乐播放器、支付宝、今日头条布局滚动效果等源码
    iOS宇宙大战游戏、调试工具、各种动画、AR相册、相机图片编辑等源码
    android支付宝首页、蚂蚁森林效果、视频背景、校园电台、载入收缩动画等源码
    iOS天气动画、高仿QQ菜单、放京东APP、高仿微信、推送消息等源码
    android高仿抖音、点餐界面、天气项目、自定义view指示、爬取美女图片等源码
    Java_WebKit_ZC01
    Java_WebKit
    ZC_RemoteThread
    运行jar_测试代码
    运行jar_命令
  • 原文地址:https://www.cnblogs.com/bvin/p/2746342.html
Copyright © 2011-2022 走看看