如何从多个 UI 组件创建一个 UI 组件
朋友们,
我创建了一个扩展 Textview 的 UI 组件“compTV”。它运作得很好。 现在我想创建一个 UI 组件“3compTV”,它由 3 个彼此相邻的“compTV”组成。
如果我只是扩展 Activity,创建 LinearLayout 并添加 3 个“compTV”的代码效果非常好。 但如何从中创建一个组件呢? 我必须为“3compTV”组件扩展什么类以及还需要什么。 当我扩展 compTV 时,只会绘制一个对象。所以我想我必须扩展一个不同的类或采取其他方法来解决这个问题。
感谢您的支持
public class 3compTV extends compTV{
Context ctx;
int layoutMaringLeft = 100;
int layoutMaringRight = 0;
int layoutMaringTop = 0;
int layoutMaringBottom = 0;
int amountOfComponents = 5;
public components(Context context) {
super(context);
ctx = context;
Log.d(ctx.getString(R.string.app_name), "components, Constructor1");
compTV comp1 = new compTV(ctx);
compTV comp2 = new compTV(ctx);
compTV comp3 = new compTV(ctx);
comp2.setLetter("A");
comp2.setState("grey");
comp3.setLetter("A");
comp3.setState("grey");
LinearLayout LL2 = new LinearLayout(ctx);
LL2.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(layoutMaringLeft, layoutMaringTop,
layoutMaringRight, layoutMaringBottom);
LL2.addView(comp1, layoutParams);
comp1.setLetter("H");
comp1.setState("green");
LL2.addView(comp2, layoutParams);
LL2.addView(comp3, layoutParams);
}
public components(Context context, AttributeSet attrs) {
super(context, attrs);
ctx = context;
Log.d(ctx.getString(R.string.app_name), "components, Constructor2");
}
public components(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
ctx = context;
Log.d(ctx.getString(R.string.app_name), "components, Constructor3");
}
}
Friends,
I created an UI component "compTV" that extends Textview. It works very well.
Now i want to create an UI compoentent "3compTV" that just consists out of 3 "compTV" ´s next to each other.
The code, creating a LinearLayout and add 3 "compTV" ´s works very well if i just extend Activity.
But how to create a Component out of this?
What class do i have to extend for the "3compTV" component and what else would be necessary.
When i extend compTV only one object will be drawn. So i guess i have to extend a different class or take some other approach to this problem.
Thanks for your support
public class 3compTV extends compTV{
Context ctx;
int layoutMaringLeft = 100;
int layoutMaringRight = 0;
int layoutMaringTop = 0;
int layoutMaringBottom = 0;
int amountOfComponents = 5;
public components(Context context) {
super(context);
ctx = context;
Log.d(ctx.getString(R.string.app_name), "components, Constructor1");
compTV comp1 = new compTV(ctx);
compTV comp2 = new compTV(ctx);
compTV comp3 = new compTV(ctx);
comp2.setLetter("A");
comp2.setState("grey");
comp3.setLetter("A");
comp3.setState("grey");
LinearLayout LL2 = new LinearLayout(ctx);
LL2.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(layoutMaringLeft, layoutMaringTop,
layoutMaringRight, layoutMaringBottom);
LL2.addView(comp1, layoutParams);
comp1.setLetter("H");
comp1.setState("green");
LL2.addView(comp2, layoutParams);
LL2.addView(comp3, layoutParams);
}
public components(Context context, AttributeSet attrs) {
super(context, attrs);
ctx = context;
Log.d(ctx.getString(R.string.app_name), "components, Constructor2");
}
public components(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
ctx = context;
Log.d(ctx.getString(R.string.app_name), "components, Constructor3");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个扩展
LinearLayout
并包含 3 个compTV
实例的视图。我个人的偏好是将 3 个 CompTV 视图放入 XML 布局中,其父元素为
。这允许您在 XML 中指定它们的属性,例如wrap_content
,我觉得这样更简洁。您可以将它们添加到您的自定义视图中,如下所示:Make a view that extends
LinearLayout
and contains 3 instances ofcompTV
.My personal preference would be to put the 3 CompTV views in an XML layout, with their parent element being
<merge>
. This allows you to specify their attributes likewrap_content
in XML, which I find much cleaner. You add them to your custom view like this: