如何解决Android自定义按钮里面有imageview和textview?
| 我正在寻找一个自定义按钮。理想情况下,此按钮的左侧应为图片,右侧为textview。我将如何完成?解决方法
最快的方法
使用可绘制的Button创建可点击的视图作为背景,其中包含ImageView和TextView。
<RelativeLayout
android:clickable=\"true\"
android:background=\"@android:drawable/btn_default\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\">
<ImageView
android:id=\"@+id/image\"
android:layout_alignParentLeft=\"true\"
android:layout_centerVertical=\"true\"
android:src=\"@android:drawable/ic_input_add\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\">
</ImageView>
<TextView
android:id=\"@+id/text\"
android:text=\"Sample text\"
android:textAppearance=\"?android:attr/textAppearanceButton\"
android:layout_toRightOf=\"@id/image\"
android:layout_centerVertical=\"true\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"></TextView>
</RelativeLayout>
另一种方式
创建您自己的类,扩展RelativeLayout,如上所述设置内容视图,添加自定义方法以设置文本,图像等,仅此而已。
使用您的自定义布局。
,您可以将以下代码用于该任务:
<Button
android:layout_width=\"fill_parent\"
android:layout_height=\"50dp\"
android:id=\"@+id/btnLogin\"
android:text=\"@string/text\"
android:gravity=\"center\"
android:drawableLeft=\"@drawable/your_image\"
android:background=\"@drawable/login\"/> // this is for nice background - *.9.png
,使用所需的布局创建一个xml,创建一个看起来像您想要Button看起来的drawable,将drawable作为背景添加到Linear Layout中,然后将xml膨胀到自定义视图。
可绘制示例:
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<selector xmlns:android=\"http://schemas.android.com/apk/res/android\">
<item android:state_pressed=\"true\" >
<shape>
<solid android:color=\"#f3ae1b\" />
<stroke android:width=\"1dp\" android:color=\"#bb6008\" />
<corners android:radius=\"3dp\" />
<padding android:left=\"10dp\" android:top=\"10dp\" android:right=\"10dp\" android:bottom=\"10dp\" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor=\"#f3ae1b\"
android:endColor=\"#bb6008\"
android:angle=\"270\" />
<stroke
android:width=\"1dp\"
android:color=\"#bb6008\" />
<corners
android:radius=\"4dp\" />
<padding
android:left=\"10dp\"
android:top=\"10dp\"
android:right=\"10dp\"
android:bottom=\"10dp\" />
</shape>
</item>
</selector>
编辑:代码膨胀
private LayoutInflater mInflater;
mInflater = LayoutInflater.from(this);
public LinearLayout yourButton;
yourButton = (LinearLayout) mInflater.inflate(R.xml.yourButton,null);
,在Xml布局中,使用此可绘制的布局选项并获得简单的布局。下一行给出了文本左侧的图像。
android:drawableLeft=\"@drawable/image\"
,如果要编写代码来执行此操作,请使用Button类的setCompoundDrawables()来确定可绘制对象相对于文本应出现的位置。
寻找API以获得更多详细信息。
,您可以使用NoboButton做到这一点
简单快捷的方法来创建带有图标,半径,背景的android按钮
库网址https://github.com/alex31n/NoboButton
截图https://raw.githubusercontent.com/alex31n/NoboButton/master/Assets/button_screenshot.jpg
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。