zoukankan      html  css  js  c++  java
  • Android中利用LinearLayout继承实现ImageButton 转

    原理:通过继承Linearlayout,摆放自己所需的imageview和textview,形成ImageButton

    直接上源码:

      

    [c-sharp] view plaincopy
    1. import android.widget.TextView;  
    2.    
    3. public class ImageButton1 extends LinearLayout  
    4. {  
    5.   private ImageView mImage;  
    6.   private TextView mText;  
    7.    
    8.   public ImageButton1(Context context, AttributeSet attrs)  
    9.   {  
    10.     super(context,attrs);  
    11.    
    12.     mImage = new ImageView(context,attrs);  
    13.     mImage.setPadding(0,0,0,0);  
    14.     mText = new TextView(context,attrs);  
    15.     //mText.setGravity(android.view.Gravity.CENTER_HORIZONTAL);  
    16.   //  mText.setGravity(android.view.Gravity.CENTER_VERTICAL);  
    17.     mText.setPadding(0,0,0,0);  
    18.      
    19.       
    20.     setClickable(true);  
    21.     setFocusable(true);  
    22.     setBackgroundResource(android.R.drawable.btn_default);  
    23.     setOrientation(LinearLayout.VERTICAL);  
    24.     addView(mImage);  
    25.     addView(mText);  
    26.   }  
    27. }  

    调用自己编写的ImageButton1

    1. <com.test.b.ImageButton1     
    2.     android:id="@+id/imbtn01"  
    3.     android:layout_width="wrap_content"      
    4.     android:layout_height="wrap_content"      
    5.     android:src="@drawable/icon"    
    6.     android:text="MOAR"    
    7.     android:textColor="#ff000000"    
    8.     />   

    注意调用ImageButton1时,要用全名:com.test.b.ImageButton1 

    效果:button中上图下文字

    ImageButton

  • 相关阅读:
    使用npm安装包失败的解决办法(使用npm国内镜像介绍)
    JavaScript的变量、作用域和内存问题
    JavaScript的基本概念
    在Html中使用JavaScript
    JavaScript简介
    C++为了兼容,所以并不是纯面向对象编程语言
    C++四种不同的对象生存方式
    Java BigDecimal使用
    ext 对齐
    ext grid 子表格
  • 原文地址:https://www.cnblogs.com/carbs/p/2570502.html
Copyright © 2011-2022 走看看