充气和分配布局视图后,内部布局的元素没有见过 [英] After inflating and assigning a layout to view, the elements inside layout not seen

查看:122
本文介绍了充气和分配布局视图后,内部布局的元素没有见过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写这从View扩展3D翻页自定义视图。这里面的自定义视图,我申报前景和页面背景的两种观点。我宣布每一前景和网页背景不同的布局。每个布局有一个RelativeLayout的和内部的一些元素。

在自定义视图,我膨胀的布局,并将其分配给前台和后台视图。

该RelativeLayout的看到,但里面的布置元素没有显示出来。

有人请建议我如何实现这一目标。我坚持..

我customview code:

 公共类PageCurlView扩展视图{    / **我们的日志标签* /
    私人最终静态字符串变量=PageCurlView;
    私人语境myAppContext;    / **拥有我们的上下文* /
    私人的WeakReference<语境GT; mContext;
    / ** LAGACY当前前景色* /
    //私人位图mForeground;
    公共查看mForeground;    / ** LAGACY当前背景* /
    //私人位图mBackground;    公共查看mBackground;        / ** LAGACY当前选定的页面* /
    私人INT MINDEX = 0;
    公共整数[] = mViewIds {
            R.layout.view_1,
            R.layout.view_2,
            R.layout.view_3,
            R.layout.view_4,
            R.layout.view_5,
            R.layout.view_6,
            R.layout.view_7,
            R.layout.view_8,
            R.layout.view_9,
            R.layout.view_10       };    私人INT mTotalViews = mViewIds.length;    //变量内联滑块
    公共ViewPager mInlinePager;
    公共AwesomePagerAdapter mInlineAdapter;    / **
     *基地
     * @参数方面
     * /
    公共PageCurlView(上下文的背景下){
        超级(上下文);
        初始化(上下文);
        ResetClipEdge();
    }    / **
     *从XML文件构造对象。有效属性:
     *
     * @see android.view.View#查看(android.content.Context,android.util.AttributeSet)
     * /
    公共PageCurlView(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        初始化(上下文);        //从XML的AttributeSet数据
        {
            TypedArray A = context.obtainStyledAttributes(ATTRS,R.styleable.PageCurlView);            //获取数据
            bEnableDebugMode = a.getBoolean(R.styleable.PageCurlView_enableDebugMode,bEnableDebugMode);
            mCurlSpeed​​ = a.getInt(R.styleable.PageCurlView_curlSpeed​​,mCurlSpeed​​);
            mUpdateRate = a.getInt(R.styleable.PageCurlView_updateRate,mUpdateRate);
            mInitialEdgeOffset = a.getInt(R.styleable.PageCurlView_initialEdgeOffset,mInitialEdgeOffset);
            mCurlMode = a.getInt(R.styleable.PageCurlView_curlMode,mCurlMode);
            //循环对象(因此它可以被他人使用)
            a.recycle();        }        ResetClipEdge();
    }    / **
     *初始化视图
     * /
    私人最终无效的init(上下文的背景下){        myAppContext =背景;
        //缓存上下文
        mContext =新的WeakReference<语境GT;(上下文);        //基本填充
        setPadding(3,3,3,3);        //需要的焦点标志
        setFocusable(真);
        setFocusableInTouchMode(真);        mMovement =新的Vector2D(0,0);
        mFinger =新的Vector2D(0,0);
        mOldMovement =新的Vector2D(0,0);
        //设置默认的道具,那些来自一个XML:D        //创建一些示例图片        LayoutInflater吹气=(LayoutInflater)myAppContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        查看视图1 = inflater.inflate(mViewIds [0],NULL);
        查看视图2 = inflater.inflate(mViewIds [1],NULL);        //从假期回来后修复
        //用于在线滑块        mForeground =厂景;
        mBackground =视图2;    }
    / **
     *重设点,这是最初的剪辑边缘状态
     * /
    / **
     *渲染文本
     *
     * @see android.view.View#的onDraw(android.graphics.Canvas)
     * /
    // @覆盖
    //保护无效的onDraw(帆布油画){
    // super.onDraw(画布);
    // canvas.drawText(MTEXT,getPaddingLeft(),getPaddingTop() - mAscent,mTextPaint);
    //}    // ------------------------------------------------ ---------------
    //冰壶。这种处理触摸事件,实际的卷曲
    //实现等。
    // ------------------------------------------------ ---------------    / **
     *中脱颖而出,回地面之间的交换。
     * /
    @德precated
    私人无效SwapViews(){
        / *位图TEMP = mForeground;
        mForeground = mBackground;
        mBackground =温度; * /        查看TEMP = mForeground;
        mForeground = mBackground;
        mBackground =温度;
    }    / **
     *交换到下一个视图
     * /
    私人无效的新思维(){//苏希尔需要取消注释
        INT foreIndex = MINDEX + 1;
        如果(foreIndex> = /*mPages.size()*/mTotalViews){
            // foreIndex = 0;
            foreIndex = mTotalViews-1;
        }
        INT backIndex = foreIndex + 1;
        如果(backIndex> = /*mPages.size()*/mTotalViews){
            // backIndex = 0;
            backIndex = mTotalViews-1;
        }
        MINDEX = foreIndex;            setViews(foreIndex,backIndex);
    }    / **
     *交换到previous视图
     * /
    私人无效previousView(){//苏希尔需要取消注释
        Log.i(苏希尔,.... previousView()......);
        INT backIndex = MINDEX;
        INT foreIndex = backIndex - 1;
        如果(foreIndex℃,){
            foreIndex = /*mPages.size()*/0;
        }
        MINDEX = foreIndex;
        setViews(foreIndex,backIndex);    }    / **
     *设置当前脱颖而出和背景
     * @参数前台 - 前景视图索引
     * @参数背景 - 背景视图索引
     * /
    私人无效setViews(INT前景,诠释背景){        LayoutInflater吹气=(LayoutInflater)myAppContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        查看视图1 = inflater.inflate(mViewIds [前景],NULL);
        查看视图2 = inflater.inflate(mViewIds [背景],NULL);        mForeground =厂景; //(的WebView)mPages.get(前景);
        mBackground =视图2; //(的WebView)mPages.get(背景);
    }    // ------------------------------------------------ ---------------
    //绘制方法
    // ------------------------------------------------ ---------------    @覆盖
    保护无效的onDraw(帆布油画){
        //始终刷新偏移
        mCurrentLeft = getLeft();
        mCurrentTop =共达();        //翻译整个画布
        //canvas.translate(mCurrentLeft,mCurrentTop);        //我们需要初始化所有大小的数据,当我们第一次绘制视图
        如果(!bViewDrawn){
            bViewDrawn = TRUE;
            onFirstDrawEvent(画布);
        }        canvas.drawColor(Color.WHITE);        //页面卷曲
        // DoPageCurl();        // TODO:这只是尺度的意见,目前的
        //宽度和高度。我们应该增加一些逻辑:
        // 1)保持纵横比
        // 2)统一的规模
        // 3)...
        RECT RECT =新的矩形();
        rect.left = 0;
        rect.top = 0;
        rect.bottom =的getHeight();
        rect.right =的getWidth();        //第一页呈现
        涂料粉刷=新的油漆();        //我们的绘制元素
        drawForeground(帆布,RECT,油漆);
        drawBackground(帆布,RECT,油漆);        drawCurlEdge(画布);        //绘制任何调试信息,一旦我们完成
        如果(bEnableDebugMode)
            drawDebug(画布);        //检查是否可以重新启用输入
        如果(bEnableInputAfterDraw)
        {
            bBlockTouchInput = FALSE;
            bEnableInputAfterDraw = FALSE;
        }        //恢复帆布
        //canvas.restore();
        super.onDraw(画布);
    }    / **
     *叫上视图的第一次抽签活动
     * @参数帆布
     * /
    保护无效onFirstDrawEvent(帆布油画){        mFlipRadius =的getWidth();        ResetClipEdge();
        DoPageCurl();
    }    / **
     *绘制前景
     * @参数帆布
     * @参数RECT
     * @参数漆
     * /
    私人无效drawForeground(帆布油画,矩形RECT,涂料粉刷){
        //canvas.drawBitmap(mForeground,空,矩形,油漆);
        //mForeground.loadUrl(\"file:///android_asset/WebContent/Section01.html);        mForeground.layout(rect.left,rect.top,rect.right,rect.bottom);
        mForeground.draw(画布);
        //绘制页码(第一页是在现实生活中1:D
        //没有页码0嘿嘿)
        // drawPageNum(帆布,MINDEX);
    }    / **
     *创建用作遮罩的路径绘制背景页面
     * @返回
     * /
    私人路径createBackgroundPath(){
        路径path =新路径();
        path.moveTo(mA.x,mA.y);
        path.lineTo(mB.x,mB.y);
        path.lineTo(mC.x,mC.y);
        path.lineTo(mD.x,mD.y);
        path.lineTo(mA.x,mA.y);
        返回路径;
    }    / **
     *绘制背景图片。
     * @参数帆布
     * @参数RECT
     * @参数漆
     * /
    私人无效drawBackground(帆布油画,矩形RECT,涂料粉刷){
        路径面膜= createBackgroundPath();        //保存当前画布,所以我们不要搞砸了
        canvas.save();
        canvas.clipPath(掩模);
        //canvas.drawBitmap(mBackground,空,矩形,油漆);
        //mBackground.loadUrl(\"file:///android_asset/WebContent/Section01.html);
        mBackground.layout(rect.left,rect.top,rect.right,rect.bottom);
        mBackground.draw(画布);        //绘制页码(第一页是在现实生活中1:D
        //没有页码0嘿嘿)
        drawPageNum(帆布,MINDEX);        canvas.restore();
    }}

我的一个布局文件的:

 <?XML版本=1.0编码=UTF-8&GT?;<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT>
<的TextView
    机器人:ID =@ + ID / sampletextview
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字颜色=@色/红
    机器人:文字=查看1/>< / RelativeLayout的>


解决方案

你这样做是错误的

您的自定义视图包含一堆的另一个观点,因此在逻辑上应该是的ViewGroup (或它的子类)用正确的实施,不仅仅是查看

I am writing a custom view for 3D page turning which extends from View. Inside this custom view, I declare two views for foreground and background of page. I have declared different layouts for each foreground and background of page. Each layout has a Relativelayout and some elements inside that.

Inside custom view, I inflate the layouts and assign them to foreground and background view.

The RelativeLayout is seen but elements inside the layout is not showing up.

Can someone please suggest me how to achieve this. Am stuck ..

My customview code:

public class PageCurlView extends View {

    /** Our Log tag */
    private final static String TAG = "PageCurlView";


    private Context myAppContext;



    /** The context which owns us */
    private WeakReference<Context> mContext;


    /** LAGACY The current foreground */
    //private Bitmap mForeground;
    public View mForeground;

    /** LAGACY The current background */
    //private Bitmap mBackground;

    public View mBackground;

        /** LAGACY Current selected page */
    private int mIndex = 0;


    public Integer[] mViewIds = {
            R.layout.view_1,
            R.layout.view_2,
            R.layout.view_3,
            R.layout.view_4,
            R.layout.view_5,
            R.layout.view_6,
            R.layout.view_7,
            R.layout.view_8,
            R.layout.view_9,
            R.layout.view_10

       };

    private int mTotalViews = mViewIds.length;

    //Variables for inline sliders
    public ViewPager mInlinePager;
    public AwesomePagerAdapter mInlineAdapter;

    /**
     * Base
     * @param context
     */
    public PageCurlView(Context context) {
        super(context);
        init(context);
        ResetClipEdge();
    }

    /**
     * Construct the object from an XML file. Valid Attributes:
     * 
     * @see android.view.View#View(android.content.Context, android.util.AttributeSet)
     */
    public PageCurlView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);

        // Get the data from the XML AttributeSet
        {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PageCurlView);

            // Get data
            bEnableDebugMode = a.getBoolean(R.styleable.PageCurlView_enableDebugMode, bEnableDebugMode);
            mCurlSpeed = a.getInt(R.styleable.PageCurlView_curlSpeed, mCurlSpeed);
            mUpdateRate = a.getInt(R.styleable.PageCurlView_updateRate, mUpdateRate);
            mInitialEdgeOffset = a.getInt(R.styleable.PageCurlView_initialEdgeOffset, mInitialEdgeOffset);
            mCurlMode = a.getInt(R.styleable.PageCurlView_curlMode, mCurlMode);


            // recycle object (so it can be used by others)
            a.recycle();

        }

        ResetClipEdge();
    }

    /**
     * Initialize the view
     */
    private final void init(Context context) {

        myAppContext = context;


        // Cache the context
        mContext = new WeakReference<Context>(context);

        // Base padding
        setPadding(3, 3, 3, 3);

        // The focus flags are needed
        setFocusable(true);
        setFocusableInTouchMode(true);

        mMovement =  new Vector2D(0,0);
        mFinger = new Vector2D(0,0);
        mOldMovement = new Vector2D(0,0);


        // Set the default props, those come from an XML :D



        // Create some sample images

        LayoutInflater inflater = (LayoutInflater)  myAppContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view1 = inflater.inflate(mViewIds[0], null);   
        View view2 = inflater.inflate(mViewIds[1], null); 



        //Fix after coming back from vacation
        //For inline sliders

        mForeground = view1;
        mBackground = view2;

    }


    /**
     * Reset points to it's initial clip edge state
     */


    /**
     * Render the text
     * 
     * @see android.view.View#onDraw(android.graphics.Canvas)
     */
    //@Override
    //protected void onDraw(Canvas canvas) {
    //  super.onDraw(canvas);
    //  canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent, mTextPaint);
    //}

    //---------------------------------------------------------------
    // Curling. This handles touch events, the actual curling
    // implementations and so on.
    //---------------------------------------------------------------



    /**
     * Swap between the fore and back-ground.
     */
    @Deprecated
    private void SwapViews() {
        /*Bitmap temp = mForeground;
        mForeground = mBackground;
        mBackground = temp;*/

        View temp = mForeground;
        mForeground = mBackground;
        mBackground = temp;
    }

    /**
     * Swap to next view
     */
    private void nextView() { //Sushil need to uncomment
        int foreIndex = mIndex + 1;
        if(foreIndex >= /*mPages.size()*/mTotalViews) {
            //foreIndex = 0;
            foreIndex = mTotalViews-1;
        }
        int backIndex = foreIndex + 1;
        if(backIndex >= /*mPages.size()*/mTotalViews) {
            //backIndex = 0;
            backIndex = mTotalViews-1;
        }
        mIndex = foreIndex;

            setViews(foreIndex, backIndex);
    }

    /**
     * Swap to previous view
     */
    private void previousView() { //Sushil need to uncomment
        Log.i("Sushil", "....previousView()....");
        int backIndex = mIndex;
        int foreIndex = backIndex - 1;
        if(foreIndex < 0) {
            foreIndex = /*mPages.size()*/0;
        }
        mIndex = foreIndex;
        setViews(foreIndex, backIndex);

    }

    /**
     * Set current fore and background
     * @param foreground - Foreground view index
     * @param background - Background view index
     */
    private void setViews(int foreground, int background) {

        LayoutInflater inflater = (LayoutInflater)  myAppContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view1 = inflater.inflate(mViewIds[foreground], null);   
        View view2 = inflater.inflate(mViewIds[background], null); 

        mForeground = view1;//(WebView)mPages.get(foreground);
        mBackground = view2;//(WebView)mPages.get(background);
    }

    //---------------------------------------------------------------
    // Drawing methods
    //---------------------------------------------------------------

    @Override
    protected void onDraw(Canvas canvas) {
        // Always refresh offsets
        mCurrentLeft = getLeft();
        mCurrentTop = getTop();

        // Translate the whole canvas
        //canvas.translate(mCurrentLeft, mCurrentTop);

        // We need to initialize all size data when we first draw the view
        if ( !bViewDrawn ) {
            bViewDrawn = true;
            onFirstDrawEvent(canvas);
        }

        canvas.drawColor(Color.WHITE);

        // Curl pages
        //DoPageCurl();

        // TODO: This just scales the views to the current
        // width and height. We should add some logic for:
        //  1) Maintain aspect ratio
        //  2) Uniform scale
        //  3) ...
        Rect rect = new Rect();
        rect.left = 0;
        rect.top = 0;
        rect.bottom = getHeight();
        rect.right = getWidth();

        // First Page render
        Paint paint = new Paint();

        // Draw our elements
        drawForeground(canvas, rect, paint);
        drawBackground(canvas, rect, paint);

        drawCurlEdge(canvas);

        // Draw any debug info once we are done
        if ( bEnableDebugMode )
            drawDebug(canvas);

        // Check if we can re-enable input
        if ( bEnableInputAfterDraw )
        {
            bBlockTouchInput = false;
            bEnableInputAfterDraw = false;
        }

        // Restore canvas
        //canvas.restore();
        super.onDraw(canvas);
    }

    /**
     * Called on the first draw event of the view
     * @param canvas
     */
    protected void onFirstDrawEvent(Canvas canvas) {

        mFlipRadius = getWidth();

        ResetClipEdge();
        DoPageCurl();
    }

    /**
     * Draw the foreground
     * @param canvas
     * @param rect
     * @param paint
     */
    private void drawForeground( Canvas canvas, Rect rect, Paint paint ) {
        //canvas.drawBitmap(mForeground, null, rect, paint);
        //mForeground.loadUrl("file:///android_asset/WebContent/Section01.html");

        mForeground.layout(rect.left, rect.top, rect.right, rect.bottom);
        mForeground.draw(canvas);


        // Draw the page number (first page is 1 in real life :D 
        // there is no page number 0 hehe)
        //drawPageNum(canvas, mIndex);
    }

    /**
     * Create a Path used as a mask to draw the background page
     * @return
     */
    private Path createBackgroundPath() {
        Path path = new Path();
        path.moveTo(mA.x, mA.y);
        path.lineTo(mB.x, mB.y);
        path.lineTo(mC.x, mC.y);
        path.lineTo(mD.x, mD.y);
        path.lineTo(mA.x, mA.y);
        return path;
    }

    /**
     * Draw the background image.
     * @param canvas
     * @param rect
     * @param paint
     */
    private void drawBackground( Canvas canvas, Rect rect, Paint paint ) {
        Path mask = createBackgroundPath();

        // Save current canvas so we do not mess it up
        canvas.save();
        canvas.clipPath(mask);
        //canvas.drawBitmap(mBackground, null, rect, paint);
        //mBackground.loadUrl("file:///android_asset/WebContent/Section01.html");
        mBackground.layout(rect.left, rect.top, rect.right, rect.bottom);
        mBackground.draw(canvas);



        // Draw the page number (first page is 1 in real life :D 
        // there is no page number 0 hehe)
        drawPageNum(canvas, mIndex);

        canvas.restore();
    }

}

One of my layout file:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


<TextView 
    android:id="@+id/sampletextview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/red"
    android:text="VIEW 1" />

</RelativeLayout>

解决方案

"You're doing it wrong"

Your custom View contains bunch of another views, so logically it should be ViewGroup (or subclass of it) with proper implementation, not just View.

这篇关于充气和分配布局视图后,内部布局的元素没有见过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆