android img标签属性_Android 自定义标签 Imagebutton实现ImageButton里放置文字

本文介绍了如何在Android中为ImageButton添加文字。通过继承LinearLayout创建自定义标签`ImageButton_define`,结合布局文件实现图文结合的效果,或者使用`drawableLeft`属性直接在Button上设置图片和文本。
摘要由CSDN通过智能技术生成

1 .android的ImageButton标签是没有text属性的,那么我们要在一个ImageButton里放置文字怎么办呢?

先上效果图如下:

如果我们不设置背景效果如下:但是给人的感觉就行分离的两个组件:

我们可以继承LinearLayout自定义标签:ImageButton_define.java

package com.test;

import android.content.Context;

import android.util.AttributeSet;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.TextView;

public class ImageButton_define extends LinearLayout {

private ImageView imageViewbutton;

private TextView textView;

public ImageButton_define(Context context,AttributeSet attrs) {

super(context,attrs);

// TODO Auto-generated constructor stub

imageViewbutton = new ImageView(context, attrs);

imageViewbutton.setPadding(0, 0, 0, 0);

textView =new TextView(context, attrs);

//水平居中

textView.setGravity(android.view.Gravity.CENTER_HORIZONTAL);

textView.setPadding(0, 0, 0, 0);

setClickable(true);

setFocusable(true);

setBackgroundResource(android.R.drawable.btn_default);

setOrientation(LinearLayout.VERTICAL);

addView(imageViewbutton);

addView(textView);

}

}

2。接下来定义布局文件:

这个不就文件使用的标签就是我们上面自定义的标签:如下:text.xml

android:layout_width="wrap_content"

android:layout_height="wrap_content">

android:id="@+id/define_iamgebutton"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:src="@drawable/android"

android:background="#00000000"

android:text="图片文本"

android:textColor="#cc0000"

>

之后我们定义个activity,引入我们定义的布局文件资源:ImageButton_de.java

package com.test;

import android.app.Activity;

import android.os.Bundle;

public class ImageButton_de extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.test);

}

}

方法二:

定义布局文件:

android:id="@+id/button_text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="标题"

android:drawableLeft="@drawable/android"

/>

运行效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值