请教如何更改Button样式,或是能否在ImageButton上添加文字
推荐于2016-11-12
展开全部
我写了一个这样的:
package com.comment.activity;
import android.content.Context;
import android.view.Gravity;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TextImageView extends LinearLayout {
public TextImageView(Context context, String title, int resId) {
super(context);
this.setOrientation(HORIZONTAL);
// Here we build the child views in code. They could also have
// been specified in an XML file.
mTitle = new TextView(context);
mTitle.setText(title);
mTitle.setGravity(Gravity.CENTER);
addView(mTitle, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
mImage = new ImageView(context);
mImage.setImageResource(resId);
addView(mImage, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));// width height
}
/**
* Convenience method to set the title of a SpeechView
*/
public void setTitle(String title) {
mTitle.setText(title);
}
public void setImage(int resId) {
mImage.setImageResource(resId);
}
private TextView mTitle;
private ImageView mImage;
}
package com.comment.activity;
import android.content.Context;
import android.view.Gravity;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TextImageView extends LinearLayout {
public TextImageView(Context context, String title, int resId) {
super(context);
this.setOrientation(HORIZONTAL);
// Here we build the child views in code. They could also have
// been specified in an XML file.
mTitle = new TextView(context);
mTitle.setText(title);
mTitle.setGravity(Gravity.CENTER);
addView(mTitle, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
mImage = new ImageView(context);
mImage.setImageResource(resId);
addView(mImage, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));// width height
}
/**
* Convenience method to set the title of a SpeechView
*/
public void setTitle(String title) {
mTitle.setText(title);
}
public void setImage(int resId) {
mImage.setImageResource(resId);
}
private TextView mTitle;
private ImageView mImage;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询