【问题标题】:how to add views inside a custom View?如何在自定义视图中添加视图?
【发布时间】:2011-09-13 23:24:42
【问题描述】:

我有一个这样的课,大约有 10 个

public class DataItemPlainView extends View{

    public DataItemPlainView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }}

现在我需要将 TextView、ImageView 等放在这个视图中。当我从某个地方调用它时,我想获得我的 customView。将视图设置为自定义布局也是一种情况。

谢谢

【问题讨论】:

    标签: android custom-view android-custom-view


    【解决方案1】:

    弄乱边距以实现绝对定位是错误的。如果您需要边距,那将不会扩大规模。

    从 Android 中窃取代码,对其进行修改,然后使用您的“未弃用”绝对布局。

    AbsoluteLayout 被弃用是因为他们不想支持它,而不是因为它不起作用。

    搞砸了,他们的布局不能满足我们的需要,那么他们有什么建议?自定义视图。

    所以这里是(重构,没有样式(puke)支持):

    /*
     * Copyright (C) 2006 The Android Open Source Project
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    package com.example;
    
    import android.content.Context;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.RemoteViews.RemoteView;
    
    
    /**
     * A layout that lets you specify exact locations (x/y coordinates) of its
     * children. Absolute layouts are less flexible and harder to maintain than
     * other types of layouts without absolute positioning.
     *
     */
    @RemoteView
    public class DCAbsoluteLayout extends ViewGroup {
        int mPaddingLeft, mPaddingRight, mPaddingTop, mPaddingBottom;
    
        public DCAbsoluteLayout(Context context) {
            super(context);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int count = getChildCount();
    
            int maxHeight = 0;
            int maxWidth = 0;
    
            // Find out how big everyone wants to be
            measureChildren(widthMeasureSpec, heightMeasureSpec);
    
            // Find rightmost and bottom-most child
            for (int i = 0; i < count; i++) {
                View child = getChildAt(i);
                if (child.getVisibility() != GONE) {
                    int childRight;
                    int childBottom;
    
                    DCAbsoluteLayout.LayoutParams lp
                            = (DCAbsoluteLayout.LayoutParams) child.getLayoutParams();
    
                    childRight = lp.x + child.getMeasuredWidth();
                    childBottom = lp.y + child.getMeasuredHeight();
    
                    maxWidth = Math.max(maxWidth, childRight);
                    maxHeight = Math.max(maxHeight, childBottom);
                }
            }
    
            // Account for padding too
            maxWidth += mPaddingLeft + mPaddingRight;
            maxHeight += mPaddingTop + mPaddingBottom;
    
            // Check against minimum height and width
            maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
            maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
    
            setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, 0),
                    resolveSizeAndState(maxHeight, heightMeasureSpec, 0));
        }
    
        /**
         * Returns a set of layout parameters with a width of
         * {@link ViewGroup.LayoutParams#WRAP_CONTENT},
         * a height of {@link ViewGroup.LayoutParams#WRAP_CONTENT}
         * and with the coordinates (0, 0).
         */
        @Override
        protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
            return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0);
        }
    
        @Override
        protected void onLayout(boolean changed, int l, int t,
                int r, int b) {
            int count = getChildCount();
    
            for (int i = 0; i < count; i++) {
                View child = getChildAt(i);
                if (child.getVisibility() != GONE) {
    
                    DCAbsoluteLayout.LayoutParams lp =
                            (DCAbsoluteLayout.LayoutParams) child.getLayoutParams();
    
                    int childLeft = mPaddingLeft + lp.x;
                    int childTop = mPaddingTop + lp.y;
                    child.layout(childLeft, childTop,
                            childLeft + child.getMeasuredWidth(),
                            childTop + child.getMeasuredHeight());
    
                }
            }
        }
    
        // Override to allow type-checking of LayoutParams.
        @Override
        protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
            return p instanceof DCAbsoluteLayout.LayoutParams;
        }
    
        @Override
        protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
            return new LayoutParams(p);
        }
    
        @Override
        public boolean shouldDelayChildPressedState() {
            return false;
        }
    
        public static class LayoutParams extends ViewGroup.LayoutParams {
            /**
             * The horizontal, or X, location of the child within the view group.
             */
            public int x;
            /**
             * The vertical, or Y, location of the child within the view group.
             */
            public int y;
    
            /**
             * Creates a new set of layout parameters with the specified width,
             * height and location.
             *
             * @param width the width, either {@link #MATCH_PARENT},
                      {@link #WRAP_CONTENT} or a fixed size in pixels
             * @param height the height, either {@link #MATCH_PARENT},
                      {@link #WRAP_CONTENT} or a fixed size in pixels
             * @param x the X location of the child
             * @param y the Y location of the child
             */
            public LayoutParams(int width, int height, int x, int y) {
                super(width, height);
                this.x = x;
                this.y = y;
            }
    
            /**
             * {@inheritDoc}
             */
            public LayoutParams(ViewGroup.LayoutParams source) {
                super(source);
            }
    
        }
    }
    

    一般来说,Android UI 是一场噩梦。考虑为整个项目使用 webview。就系统的工作而言,“没关系”,但那个 UI 是个烂尾。

    HTML 一直是样式和动态 UI 内容的首选方式。除了自定义绘制的 UI(在性能方面)之外,没有其他东西可以与之匹敌。

    【讨论】:

    • HTML 支持绝对位置!还有多屏设置,为什么安卓不呢?
    【解决方案2】:

    我会尝试扩展某种布局。请记住(在大多数情况下)它们也被视为视图。有关更多信息/决定选择哪种布局,请尝试查看此处:

    http://developer.android.com/guide/topics/ui/layout-objects.html

    【讨论】:

      【解决方案3】:

      您的自定义视图需要扩展 ViewGroup 或扩展 ViewGroup 的其他类之一。例如,您可以从 RelativeLayout 或 LinearLayout 扩展,如果这些布局符合您的自定义视图需要做的事情。

      请记住,即使是布局类也只是另一个View。他们只是碰巧有方法可以将其他视图添加为子视图,并且有代码来递归测量和绘制子视图。

      【讨论】:

      • 我需要返回一个视图以获取适合适配器的 getView 方法的视图。如果我在这种情况下扩展 ViewGroup,是否可以将其称为视图? (或者是 View 扩展 ViewGroup),谢谢
      • ViewGroup 肯定会扩展 View。
      • 我扩展了RelativeLayout,但是当我将视图添加到此自定义视图时,它不会出现,因此不会显示子视图..
      猜你喜欢
      • 1970-01-01
      • 2012-05-10
      • 2011-09-06
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      相关资源
      最近更新 更多