Android中利用LinearLayout继承实现ImageButton

    xiaoxiao2021-12-14  20

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

    直接上源码:

      

    view plain copy to clipboard print ? import android.widget.TextView;     public class ImageButton1 extends LinearLayout  {    private ImageView mImage;    private TextView mText;       public ImageButton1(Context context, AttributeSet attrs)    {      super(context,attrs);         mImage = new ImageView(context,attrs);      mImage.setPadding(0,0,0,0);      mText = new TextView(context,attrs);      //mText.setGravity(android.view.Gravity.CENTER_HORIZONTAL);     //  mText.setGravity(android.view.Gravity.CENTER_VERTICAL);       mText.setPadding(0,0,0,0);                 setClickable(true);      setFocusable(true);      setBackgroundResource(android.R.drawable.btn_default);      setOrientation(LinearLayout.VERTICAL);      addView(mImage);      addView(mText);    }  }   [c-sharp] view plain copy print ? import android.widget.TextView; public class ImageButton1 extends LinearLayout{  private ImageView mImage;  private TextView mText;   public ImageButton1(Context context, AttributeSet attrs)  {    super(context,attrs);     mImage = new ImageView(context,attrs);    mImage.setPadding(0,0,0,0);    mText = new TextView(context,attrs);    //mText.setGravity(android.view.Gravity.CENTER_HORIZONTAL);  //  mText.setGravity(android.view.Gravity.CENTER_VERTICAL);    mText.setPadding(0,0,0,0);           setClickable(true);    setFocusable(true);    setBackgroundResource(android.R.drawable.btn_default);    setOrientation(LinearLayout.VERTICAL);    addView(mImage);    addView(mText);  }}   import android.widget.TextView; public class ImageButton1 extends LinearLayout{ private ImageView mImage; private TextView mText; public ImageButton1(Context context, AttributeSet attrs) { super(context,attrs); mImage = new ImageView(context,attrs); mImage.setPadding(0,0,0,0); mText = new TextView(context,attrs); //mText.setGravity(android.view.Gravity.CENTER_HORIZONTAL); // mText.setGravity(android.view.Gravity.CENTER_VERTICAL); mText.setPadding(0,0,0,0); setClickable(true); setFocusable(true); setBackgroundResource(android.R.drawable.btn_default); setOrientation(LinearLayout.VERTICAL); addView(mImage); addView(mText); }}

     

    调用自己编写的ImageButton1

     

    view plain copy to clipboard print ? <com.test.b.ImageButton1         android:id="@+id/imbtn01"      android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:src="@drawable/icon"        android:text="MOAR"        android:textColor="#ff000000"        />    [xhtml] view plain copy print ? <com.test.b.ImageButton1       android:id="@+id/imbtn01"    android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/icon"      android:text="MOAR"      android:textColor="#ff000000"      />    <com.test.b.ImageButton1 android:id="@+id/imbtn01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon" android:text="MOAR" android:textColor="#ff000000" />

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

     

     

    效果:button中上图下文字

     

     

     

    转载请注明原文地址: https://ju.6miu.com/read-964300.html

    最新回复(0)