Android学习之带图片的Button

本文介绍了如何在Android中创建带有图片且图片与文字居中的Button。通过两种方法实现:一是利用布局配置,将图片和文字覆盖在Button之上并居中;二是创建自定义布局,通过新建布局文件和类实现这一效果,虽然程序复杂但灵活性高。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在手机应用中,往往会看到这样子的图片:
这里写图片描述
Button中有属性是可以带入图片,并设置位置的,但是偏偏Drawable Left,Drawable Top,Drawable Right,Drawable Top这四个属性放的图片都是在最旁边,像是这样:
这里写图片描述
所以,我整理两种通俗易懂,简单的方式来解决这个问题,
一、利用布局去配置,按钮在背后,前面在盖图片跟文字然后置中

<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/footer_btn4" >


<Button android:id="@+id/btnReset"  android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/selector_menu_footer_btn" android:onClick="btnConfirmSearch"
android:textColor="@color/white"
android:textSize="@dimen/tenant_search_condition_condition_btn_txt" 
    />



<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center" >



<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/footer_btn3" >
</RelativeLayout>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:text="@string/tenant_search_condition_loc_btn_reset"
android:textColor="@color/white"
android:textSize="@dimen/tenant_search_condition_condition_btn_txt" />
</LinearLayout>
</RelativeLayout>

这样的做法,布局可能会有点麻烦,不过能用
二、新建一个布局,用一个类来导入这个布局,可以说是自定义布局吧
先上布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"

    >

    <ImageView

        android:contentDescription="@string/null_button"
        android:id="@+id/imgview_comments"
        style="@style/activity_toolbar_buttom"
        android:src="@drawable/btn_comments"/>

    <TextView
        android:maxLines="1"
        android:id="@+id/textview_comments"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:text="25条评论"
        android:textColor="#999999"
        android:textStyle="bold"
        android:typeface="serif"/>
</LinearLayout>

布局效果图:
这里写图片描述
然后新建一个类:

package com.jp.tools;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jp.activity.R;


/**
 * Created by Administrator on 2015/8/31.
 */
public class CommentsButton extends RelativeLayout
{
    private ImageView imgView;
    private TextView textView;
    private RelativeLayout layout;

    public CommentsButton(Context context) {
        super(context);
    }

    public CommentsButton(Context context, AttributeSet attributeSet) {
        super(context,attributeSet);
        LayoutInflater.from(context).inflate(R.layout.button_comments, this, true);

        this.imgView = (ImageView) findViewById(R.id.imgview_collection);
        this.textView = (TextView) findViewById(R.id.textview_collection);
//设置允许点击事件
        this.setClickable(true);
        this.setFocusable(true);

    }

    public void setImgResource(int resourceID) {
        this.imgView.setImageResource(resourceID);
    }

    public void setText(String text) {
        this.textView.setText(text);
    }

    public void setTextColor(int color) {
        this.textView.setTextColor(color);
    }

    public void setTextSize(float size) {
        this.textView.setTextSize(size);
    }



}

再在主布局上做下引用:

<com.jp.tools.CommentsButton
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:background="@drawable/item_style_line"
                android:gravity="center"/>

就是这样子的效果了
这里写图片描述
优点layout文件干净,缺点程序较复杂,不过是一劳永逸,灵活性高

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值