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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)