Custom ImageTextButton with images and text,

Source: Internet
Author: User

Custom ImageTextButton with images and text,

Create image_text_buttton.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="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 inherits 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.xml

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/activity_v Ertical_margin "tools: context =". MainActivity "> <! -- ToggleButton can also achieve similar effects, except that the image spacing is not well controlled <ToggleButton android: id = "@ + id/toggle" android: layout_width = "120dp" android: layout_height = "83dp" android: background = "# ff00ff" android: drawableTop = "@ drawable/cancel" android: textOn = "keyboard" android: textOff = "keyboard" 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>

 

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"));    }}

 

As follows:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.