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
最满意答案
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 typeGame
with the size10
(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.
-
抽象类的数组(Array of abstract class)[2024-03-22]
Game[] gamesArray = new Game[10]; 实例化意味着创建一个类的实例。 在上面的场景中,你刚刚声明了一个gamesArray类型的Game ,其大小为10 (仅仅是引用而没有其他)。 这就是为什么它不会抛出任何错误。 当你尝试时你会得到错误 gamesArray[0] = new Game(); // because abstract class cannot be instantiated 但创建一个抽象类的数组? 稍后,你可以做这样的事情 gamesArray[0] = ... -
何时使用抽象类?(When to use abstract classes?)[2022-04-26]
当您需要一个类用于继承和多态的目的时,抽象类很有用,但实例化它本身,而仅仅是其子类没有任何意义。 当您想为共享一些常用实现代码的一组子类定义模板时,通常使用它们,但您也希望保证无法创建超类的对象。 例如,假设您需要创建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 ...
-
抽象类的实现(Implementation of an abstract class)[2023-12-02]
如果子类也应该是通用的,它应该具有与基类相同的泛型参数声明: public class MyFirstClassImpl, V> extends MyFirstClass { @Override public Iterator > iterator() { // Code } } 否则,您的子类根本不应具有任何泛型类型参数: public class MyFirstCla ... -
抽象类的大小(Size of abstract class)[2022-09-17]
您可以使用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 ...