如何从多个 UI 组件创建一个 UI 组件

发布于 2024-12-15 22:43:27 字数 1825 浏览 3 评论 0原文

朋友们,

我创建了一个扩展 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落□心尘 2024-12-22 22:43:27

创建一个扩展 LinearLayout 并包含 3 个 compTV 实例的视图。

public class 3CompTV extends LinearLayout {

    public 3CompTV(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(LinearLayout.VERTICAL);

        for (int i = 0; i < 3; i++) {
            addView(new CompTV(context));      
        }  
    }
}

我个人的偏好是将 3 个 CompTV 视图放入 XML 布局中,其父元素为 。这允许您在 XML 中指定它们的属性,例如 wrap_content,我觉得这样更简洁。您可以将它们添加到您的自定义视图中,如下所示:

public class 3CompTV extends LinearLayout {

    public 3CompTV(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(LinearLayout.VERTICAL);
        View.inflate(context, R.id.three_comp_tvs, this);           
    }
}

Make a view that extends LinearLayout and contains 3 instances of compTV.

public class 3CompTV extends LinearLayout {

    public 3CompTV(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(LinearLayout.VERTICAL);

        for (int i = 0; i < 3; i++) {
            addView(new CompTV(context));      
        }  
    }
}

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 like wrap_content in XML, which I find much cleaner. You add them to your custom view like this:

public class 3CompTV extends LinearLayout {

    public 3CompTV(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(LinearLayout.VERTICAL);
        View.inflate(context, R.id.three_comp_tvs, this);           
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文