Android自訂控制項

來源:互聯網
上載者:User

標籤:android

android自訂按鈕

1》定義按鈕布局檔案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android650) this.width=650;" src="http://techfoxbbs.com/static/image/smiley/default/shocked.gif" border="0" />rientation="horizontal"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        >
    <ImageView
            android:id="@+id/iconMoney"
            android:layout_width="25dp"
            android:layout_height="fill_parent"
            >
    </ImageView>
    <TextView
            android:id="@+id/numMeoney"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="4444"
            android:textSize="20dp"
            android:layout_marginTop="0dp"
            android:layout_marginBottom="3dp"
            android:layout_marginLeft="8dp"
            >
    </TextView>
    <ImageView
            android:layout_marginLeft="5dp"
            android:id="@+id/iconAdd"
            android:layout_width="25dp"
            android:layout_height="fill_parent"
            >
    </ImageView>
</LinearLayout>


2》繼承布局檔案

package com.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.dreamplanegames.R;

public class MoneyView extends LinearLayout {

    private ImageView iconMoney;
    private TextView textView;

    private ImageView Addmoney;

    public MoneyView(Context context,AttributeSet attributeSet) {
        super(context, attributeSet);
        LayoutInflater.from(context).inflate(R.layout.money, this,true);//指定布局

        this.iconMoney = (ImageView)findViewById(R.id.iconMoney);
        this.textView = (TextView)findViewById(R.id.numMeoney);
        this.Addmoney=(ImageView)findViewById(R.id.iconAdd);

        this.setClickable(true);//可以點擊
        this.setFocusable(true);
    }


//設定控制項內容
    public void setText(String text) {
        this.textView.setText(text);
    }

    public void setTextColor(int color) {
        this.textView.setTextColor(color);
    }

    public void setTextSize(float size) {
        this.textView.setTextSize(size);
    }

    public void setImg(Bitmap img1,Bitmap img2) {
        this.iconMoney.setImageBitmap(img1);
        this.Addmoney.setImageBitmap(img2);
        //this.yes.setImageBitmap(img2);
    }

}


3》控制項的調用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android650) this.width=650;" src="http://techfoxbbs.com/static/image/smiley/default/shocked.gif" border="0" />rientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <ImageView android:id="@+id/mainMenuImage"
        android:src="@drawable/xk_bg2"
        android:scaleType="fitXY"
        android:layout_width="match_parent" 
        android:layout_height="match_parent">
    </ImageView>
//調用自訂控制項
    <com.widget.MoneyView--------------------類名
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:id="@+id/btnMoney"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="20dp"
            >
    </com.widget.MoneyView>

</RelativeLayout>


4》到相應的activity調用
public class SFMainMenu extends Activity implements View.OnClickListener
{
    public MoneyView moneyview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //設定全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //去除應用程式標題
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //設定豎屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);   
        setContentView(R.layout.main);

//擷取自訂按鈕
        moneyview=(MoneyView)findViewById(R.id.btnMoney);

//調用自訂控制項的函數設定控制項內容
        moneyview.setImg(BitmapFactory.decodeResource(getResources(), R.drawable.money),BitmapFactory.decodeResource(getResources(), R.drawable.add));
        moneyview.setText(""+myPointBalance);

        //自訂按鈕響應事件
        moneyview.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
         
            }
        });


}
    //返回鍵
    @Override
    public void onBackPressed() {
        super.onBackPressed();

    }

}



Android自訂控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.