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;
                }
            }
        });
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM