Android:如何创建自定义按钮小部件

Android: How to Create a Custom button widget(Android:如何创建自定义按钮小部件)

本文介绍了Android:如何创建自定义按钮小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个具有圆形或矩形背景、文本和文本下方或上方的图像的按钮.

I would like to create a button with circular or rectangular background, text and an image below or above the text.

这是我添加对象的 CustomButton 布局(背景和文本 - ImageView 缺失):

Here is the CustomButton Layout where I added the objects (background and text - ImageView is missing):

<FrameLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_height="wrap_content"
 android:orientation="horizontal" android:layout_width="wrap_content" android:layout_gravity="center_vertical|center_horizontal">

我想使用 setText() 和 setImage() 方法创建一个 CustomButton 对象,这将更改按钮文本和图像并将多个 CustomButtons 放置到主布局中.

I would like to create a CustomButton object with methods setText() and setImage() which would change the button text and image and place multiple CustomButtons into main layout.

有谁知道如何创建自定义布局,将其放入另一个布局(主)并从绑定到主布局的活动中修改其元素?

Does anyone know how to create a custom layout, place it into another layout(main) and modify its elements from the activity which is bound to main layout?

非常感谢您的帮助.

谢谢!

推荐答案

如果你想要编程访问,你应该继承 View 并在 java 中完成你的工作.您仍然可以在 xml 中进行布局,但具有您想要的图像和文本方法.然后,您将能够在另一个布局中使用它来放置您的自定义按钮.

If you want programmatic access, you should subclass View and do your work there in java. You can still do the layout in xml, but have the image and text methods that you want. You will then be able to use this in another layout to place your CustomButtons.

这篇关于Android:如何创建自定义按钮小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android:如何创建自定义按钮小部件

基础教程推荐