一個簡單的android自訂view(Switch Button for api < 14)

來源:互聯網
上載者:User

標籤:android   style   http   io   ar   color   sp   java   for   

1.編寫主類
package com.sample.button;import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.util.Log;import android.widget.CompoundButton;import com.example.buttonsample.R;public class MyButton extends CompoundButton {public MyButton(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}private Drawable imgOFF;private Drawable imgON;public MyButton(Context context, AttributeSet attrs) {super(context, attrs);TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyButton, 0, 0);try {imgOFF = a.getDrawable(R.styleable.MyButton_imgOFF);imgON = a.getDrawable(R.styleable.MyButton_imgON);} catch (Exception e) {e.printStackTrace();}a.recycle();}public MyButton(Context context) {super(context);}@Overridepublic void setChecked(boolean checked) {super.setChecked(checked);udpateDrawable();}private void udpateDrawable() {if (this.isChecked()) {Log.e(MyButton.class.getName(),"changing to imgON");this.setBackgroundDrawable(imgON);} else {Log.e(MyButton.class.getName(),"changing to imgOFF");this.setBackgroundDrawable(imgOFF);}}@Overrideprotected void onFinishInflate() {udpateDrawable();super.onFinishInflate();}}



2.編寫attrs定義檔案(res/values/attrs.xml)
<?xml version="1.0" encoding="utf-8"?><resources>     <declare-styleable name="MyButton">        <attr name="imgON" format="reference" />        <attr name="imgOFF" format="reference" />    </declare-styleable> </resources>



3.涉及到的2個圖片資源如下

res/drawable/off.png

res/drawable/on.png

4.測試用activity的layout檔案
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res/com.example.buttonsample"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <com.sample.button.MyButton        android:id="@+id/testmy"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@android:color/white"        app:imgOFF="@drawable/off"        app:imgON="@drawable/on" >    </com.sample.button.MyButton></LinearLayout>


5.實際效果即:

點擊按鈕時,按鈕會在以下2個圖片之間切換.

一個簡單的android自訂view(Switch Button for api < 14)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.