Frage

Ich habe eine solche Klasse und es gibt ungefähr 10 von ihnen

public class DataItemPlainView extends View{

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

Jetzt muss ich TextView, ImageView usw. in diese Ansicht einfügen.Und wenn ich es von irgendwoher aufrufe, möchte ich meine benutzerdefinierte Ansicht erhalten.Das Festlegen einer Ansicht für ein benutzerdefiniertes Layout ist ebenfalls ein Fall.

Danke

War es hilfreich?

Lösung

Ihre benutzerdefinierte Ansicht muss ViewGroup oder eine der anderen Klassen erweitern, die ViewGroup erweitern.Sie können beispielsweise von RelativeLayout oder LinearLayout erweitern, wenn diese Layouts den Anforderungen Ihrer benutzerdefinierten Ansicht entsprechen.

Denken Sie daran, dass selbst die Layoutklassen nur ein weiterer generischer Codetagcode sind.Sie haben einfach Methoden, um andere Ansichten als Kinder hinzuzufügen, und Code, um ihre Kinder rekursiv zu messen und zu zeichnen.

Andere Tipps

Ich würde versuchen, eine Art Layout zu erweitern.Denken Sie daran, dass sie (größtenteils) auch als Ansichten behandelt werden.Weitere Informationen / die Auswahl des Layouts finden Sie hier:

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

Das Spielen mit den Rändern, um eine absolute Positionierung zu erreichen, ist FALSCH.Das wird nicht vergrößert, wenn Sie jemals die Ränder benötigen.

Stehlen Sie den Code von Android, ändern Sie ihn und verwenden Sie dann Ihr absolutes Layout "nicht veraltet".

AbsoluteLayout ist veraltet, weil sie es nicht unterstützen möchten, nicht weil es nicht funktioniert.

Scheiß drauf, ihre Layouts machen nicht das, was wir brauchen, also was empfehlen sie?benutzerdefinierte Ansicht.

Also hier ist es (überarbeitet, ohne Style (Kotze) Unterstützung):

/*
 * 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);
        }

    }
}

Im Allgemeinen ist die Android-Benutzeroberfläche ein Albtraum.Verwenden Sie eine Webansicht für Ihr gesamtes Projekt.Was die Arbeit mit dem System angeht, ist "es ist okay", aber diese Benutzeroberfläche ist ein Zugunglück.

HTML war schon immer der richtige Weg für Stile und dynamische UI-Inhalte.Nichts anderes kann mithalten, als eine benutzerdefinierte Benutzeroberfläche (in Bezug auf die Leistung).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top