100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > android按钮放图片不显示文字 Android 自定义标签 Imagebutton实现ImageButton里放置文字...

android按钮放图片不显示文字 Android 自定义标签 Imagebutton实现ImageButton里放置文字...

时间:2022-05-03 07:38:40

相关推荐

android按钮放图片不显示文字 Android 自定义标签 Imagebutton实现ImageButton里放置文字...

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

先上效果图以下:android

若是咱们不设置背景效果以下:可是给人的感受就行分离的两个组件:app

咱们能够继承LinearLayout自定义标签:ImageButton_define.javaide

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.xmlspa

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.javacode

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

}

}

方法二:xml

定义布局文件:继承

android:id="@+id/button_text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="标题"

android:drawableLeft="@drawable/android"

/>

运行效果:

图片

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。