你的位置:首页 > 操作系统

[操作系统]自定义ImageTextButton 带图片和文字

新建 image_text_buttton.

<??> <LinearLayout ="http://schemas.android.com/apk/res/android"   android:orientation="vertical"   android:layout_width="120dp"   android:layout_height="83dp"    >   <ImageView     android:id="@+id/iv"     android:layout_width="wrap_content"     android:layout_height="wrap_content"    android:layout_gravity="center_horizontal"    android:layout_marginTop="12dp"    />   <TextView     android:id="@+id/tv"    android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_gravity="center_horizontal"    android:layout_marginTop="8dp"    android:textColor="#A29797"     /> </LinearLayout> 

ImageTextButton 继承LinearLayout 

package com.example.button;import android.content.Context;import android.util.AttributeSet;import android.view.LayoutInflater;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;public class ImageTextButton extends LinearLayout {   private ImageView iv;      private TextView tv;     public ImageTextButton(Context context) {    super(context);  }  public ImageTextButton(Context context, AttributeSet attrs) {    super(context, attrs);    // inflate the layout view    LayoutInflater.from(context).inflate(R.layout.image_text_buttton,this,true);    iv = (ImageView) findViewById(R.id.iv);     tv = (TextView) findViewById(R.id.tv);  }    public void setDefaultImageResource(int resId) {     iv.setImageResource(resId);   }     public void setDefaultTextViewText(String text){    tv.setText(text);   }    /**   * @param resId   */  public void setImageResource(int resId) {     iv.setImageResource(resId);   }     /**   * @param text   */  public void setTextViewText(String text) {     tv.setText(text);   }    /**   * @param color   */  public void setTextColor(int color) {     tv.setTextColor(color);   } }

 

activity_main.

<RelativeLayout ="http://schemas.android.com/apk/res/android"  ="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:paddingBottom="@dimen/activity_vertical_margin"  android:paddingLeft="@dimen/activity_horizontal_margin"  android:paddingRight="@dimen/activity_horizontal_margin"  android:paddingTop="@dimen/activity_vertical_margin"  tools:context=".MainActivity" >  <!-- 用ToggleButton也可以实现类似效果 只不过图片间距不太好控制  <ToggleButton       android:id="@+id/toggle"      android:layout_width="120dp"      android:layout_height="83dp"      android:background="#ff00ff"      android:drawableTop="@drawable/cancel"      android:textOn="键盘"      android:textOff="键盘"      android:textSize="14dip"      android:drawablePadding="-10dip"      android:textColor="#FFFFFF"    />   -->    <RelativeLayout     android:orientation="horizontal"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:layout_gravity="bottom"     >     <com.example.button.ImageTextButton       android:id="@+id/bt_confirm"       android:layout_height="wrap_content"       android:layout_width="wrap_content"       android:layout_alignParentBottom="true"       android:background="#655454"       android:clickable="true"       android:focusable="true"       />     <com.example.button.ImageTextButton       android:id="@+id/bt_cancel"       android:layout_toRightOf="@id/bt_confirm"       android:layout_height="wrap_content"       android:layout_width="wrap_content"       android:layout_alignParentBottom="true"       android:background="#655454"       android:clickable="true"       android:focusable="true"       />     </RelativeLayout> </RelativeLayout>

 

MainActivity.java

package com.example.button;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity {  ImageTextButton btn1;    ImageTextButton btn2;    @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    btn1 = (ImageTextButton)findViewById(R.id.bt_confirm);    btn2 = (ImageTextButton)findViewById(R.id.bt_cancel);        btn1.setImageResource(R.drawable.confirm);    btn2.setImageResource(R.drawable.cancel);        btn1.setTextViewText("OK");    btn2.setTextViewText("Cancel");    //    //    btn1.setTextColor(Color.parseColor("#00CCFF"));//    btn2.setTextColor(Color.parseColor("#FFCCFF"));  }}

 

效果图如下: