首页 \ 问答 \ Android:基于布局的自定义视图:如何?(Android: Custom view based on layout: how?)

Android:基于布局的自定义视图:如何?(Android: Custom view based on layout: how?)

我正在构建一个Android应用程序,我有点挣扎于自定义视图。

我想有一个可重用的视图,它由几个标准布局元素组成。 让我们说一个与其中的一些按钮相关的布局。

我应该如何继续。 我应该创建一个自定义视图类来扩展RelativeLayout并以编程方式添加这些按钮吗? 我认为这有点矫枉过正?

在Android中正确使用它的方法是什么?


I am building a Android app and I am a bit struggling with custom Views.

I would like to have a reusable View that consist of a few standard layout elements. Let's say a relativelayout with some buttons in it.

How should I proceed. Should I create a custom view class that extends RelativeLayout and programmaticly add those buttons? I would think that's a bit overkill?

What's the way to do it properly in Android?


原文:https://stackoverflow.com/questions/2568519
更新时间:2022-09-24 21:09

最满意答案

Game[] gamesArray = new Game[10];

实例化意味着创建一个类的实例。 在上面的场景中,你刚刚声明了一个gamesArray类型的Game ,其大小为10 (仅仅是引用而没有其他)。 这就是为什么它不会抛出任何错误。

当你尝试时你会得到错误

gamesArray[0] = new Game(); // because abstract class cannot be instantiated

但创建一个抽象类的数组?

稍后,你可以做这样的事情

gamesArray[0] = new NonAbstractGame(); // where NonAbstractGame extends the Games abstract class.

这是非常容许的,这就是为什么你会首先进入抽象类。


Game[] gamesArray = new Game[10];

Instantiation means creation of an instance of a class. In the above scenario, you've just declared a gamesArray of type Game with the size 10(just the references and nothing else). That's why its not throwing any error.

You'll get the error when you try to do

gamesArray[0] = new Game(); // because abstract class cannot be instantiated

but make an array of the abstract class?

Later on, you can do something like this

gamesArray[0] = new NonAbstractGame(); // where NonAbstractGame extends the Games abstract class.

This is very much allowed and this is why you'll be going in for an abstract class on the first place.

相关问答

更多
  • 是的,这是可能的。 如果一个子类没有实现抽象超类的所有抽象方法,它也必须是抽象的。 Yes, this is possible. If a subclass does not implements all abstract methods of the abstract superclass, it must be abstract too.
  • Game[] gamesArray = new Game[10]; 实例化意味着创建一个类的实例。 在上面的场景中,你刚刚声明了一个gamesArray类型的Game ,其大小为10 (仅仅是引用而没有其他)。 这就是为什么它不会抛出任何错误。 当你尝试时你会得到错误 gamesArray[0] = new Game(); // because abstract class cannot be instantiated 但创建一个抽象类的数组? 稍后,你可以做这样的事情 gamesArray[0] = ...
  • 当您需要一个类用于继承和多态的目的时,抽象类很有用,但实例化它本身,而仅仅是其子类没有任何意义。 当您想为共享一些常用实现代码的一组子类定义模板时,通常使用它们,但您也希望保证无法创建超类的对象。 例如,假设您需要创建Dog,Cat,Hamster和Fish对象。 他们具有类似的属性,如颜色,大小和腿数以及行为,因此您创建一个动物超类。 但是,什么颜色是动物? 动物对象有多少条腿? 在这种情况下,实例化动物类型的对象并不仅仅是它的子类没有什么意义。 抽象类还具有多态性的附加优势 - 允许您使用(抽象)超类的 ...
  • 将internal成员添加到public抽象类中使得不可能在它声明的程序集之外继承该abstract类。但是在声明程序集之外,类本身以及所有派生类仍可以使用(因为类型是public )。 假设你有一个抽象类: public abstract AMyClass { public string DoSomething() { return DoSomethingInternal(); } internal abstract string DoSomethingI ...
  • 你需要声明C类是抽象的。 You Need to declare Class C is Abstract.
  • 基本上,我使用一个经验法则,当他们必须被继承来实例化时,你应该总是定义抽象类。 因为抽象类本身不能。 例如。 假设你有一个Building类,它是抽象的。 然后它必须通过派生类实例化,例如银行或房屋。 Building类中没有抽象方法的事实与抽象或非抽象无关。 Basically, I use as a rule of thumb that you should always define classes abstract when they MUST be inherited to be instanti ...
  • 如果子类也应该是通用的,它应该具有与基类相同的泛型参数声明: public class MyFirstClassImpl, V> extends MyFirstClass { @Override public Iterator> iterator() { // Code } } 否则,您的子类根本不应具有任何泛型类型参数: public class MyFirstCla ...
  • 您可以使用sizeof运算符: int a_size = sizeof(A); You can use the sizeof operator: int a_size = sizeof(A);
  • 来自ECMA: 嵌套在泛型类声明或泛型结构声明(第25.2节)中的任何类本身都是泛型类声明,因为应提供包含类型的类型参数以创建构造类型。 因此,如果不为A提供类型参数,则无法实现嵌套B void Main() { var c = new C(); var result = c.GetAnotherObject(new BImpl()); } public class BImpl : A.B { public override int ...
  • 如果你知道users[findUser(userName)]的结果总是某种类型,你可以这样做: ((ConcreteUser)users[findUser(userName)]).setVehicle(vehicles[counterVehicles]) 否则,您必须在拨打电话之前使用某种类型的instanceof进行测试。 为了避免这一切,你可以做的最好的事情是实现访问者模式。 这是怎么回事: interface UserVisitor { public void visit(ConcreteU ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。