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

  • 相关阅读:
    SQL解发器与SQL游标实例
    动态调用JS
    HDU_5729_rmq+二分
    struts2 在MyEclipse中 的配置
    Struts 1.2 中如何测试Action
    OGNL使用小结【转】
    JUnit中assertEquals和assertSame方法的不同
    struts2 ActionContext
    ser文件与Java对象序列化
    测试Action组件代码(StrutsTestCase)
  • 原文地址:https://www.cnblogs.com/carbs/p/2570502.html
Copyright © 2011-2022 走看看