资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android自定义控件-创新互联

android自定义按钮

成都网站建设哪家好,找创新互联公司!专注于网页设计、重庆网站建设公司、微信开发、微信平台小程序开发、集团成都企业网站定制等服务项目。核心团队均拥有互联网行业多年经验,服务众多知名企业客户;涵盖的客户类型包括:自拌料搅拌车等众多领域,积累了大量丰富的经验,同时也获得了客户的一致称扬!

1》定义按钮布局文件


      xmlns:android="http://schemas.android.com/apk/res/android"
      androidAndroid自定义控件rientation="horizontal"
      android:layout_gravity="center"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      >
          android:id="@+id/iconMoney"
        android:layout_width="25dp"
        android:layout_height="fill_parent"
        >
  
          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"
        >
  
          android:layout_marginLeft="5dp"
        android:id="@+id/iconAdd"
        android:layout_width="25dp"
        android:layout_height="fill_parent"
        >
  

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》控件的调用


  androidAndroid自定义控件rientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  >
        android:src="@drawable/xk_bg2"
      android:scaleType="fitXY"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
  

//调用自定义控件
          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"
        >
  

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

  }

}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享题目:Android自定义控件-创新互联
标题网址:http://cdkjz.cn/article/dggghe.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220