如何在Android中为单击的图像按钮制作边框? [英] How can I make make a border for a clicked imagebutton in Android?

查看:90
本文介绍了如何在Android中为单击的图像按钮制作边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Imagebuttons在我的android项目中显示一些图标.

I am using Imagebuttons to show some icons in my android project.

<ImageButton
    android:id="@+id/button_one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:padding="20dp"
    android:contentDescription="@string/button_one"
    android:background="@android:color/transparent"
    android:src="@drawable/button_one" />

我想当按钮处于按下状态时,会有一个带有圆角的白色边框.我该怎么做才能做到这一点?可以仅使用代码来完成此操作,还是需要额外的背景图片?

I would like, when the button is in pressed state, there to be a white border with rounded corners. What can I do to make this happen? Can this be done using just code or will I need an extra background image?

推荐答案

如果不想创建带有边框的单独图像,这是一种方法.

Here's one way if you don't want to create separate images with a border.

使用边框样式(例如border.xml)创建可绘制的xml

Create a drawable xml with your border style (ex. border.xml)

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
        android:width="1dp"
        android:color="#FFF" />
    <padding
        android:left="1dp"
        android:top="1dp"
        android:right="1dp"
        android:bottom="1dp" />
    <corners
        android:bottomRightRadius="8dip"
        android:bottomLeftRadius="8dip"
        android:topRightRadius="8dip"
        android:topLeftRadius="8dip" />
</shape>

创建一个可绘制的选择器(例如some_selector_name.xml).基本上,当按下时,它将显示您的边框可绘制.否则,它将是透明的.

Create a selector drawable (ex. some_selector_name.xml). Basically when pressed, it will show your border drawable. Otherwise, it will be transparent.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/border" />
    <item android:drawable="@android:color/transparent" /> 
</selector>

将图像按钮的背景设置为可绘制的选择器.

Set your image button's background to your selector drawable.

<ImageButton
    android:id="@+id/button_one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:padding="20dp"
    android:contentDescription="@string/button_one"
    android:background="@drawable/some_selector_name.xml"
    android:src="@drawable/button_one" />

现在,当您按下按钮时,它应该显示白色边框. 这只是一个例子,但您应该明白这一点.

Now when you press the button, it should display the white border. This is just an example but you should get the idea.

这篇关于如何在Android中为单击的图像按钮制作边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆