2013-10-11 29 views
0

我正在写一个3D视图的自定义视图,从视图扩展。在这个自定义视图中,我为页面的前景和背景声明了两个视图。我已经为页面的每个前景和背景声明了不同的布局。每个布局都有一个Relativelayout和其中的一些元素。充气和分配布局后查看,布局内的元素看不到

在自定义视图中,我膨胀布局并将它们分配给前景和背景视图。

可以看到RelativeLayout,但布局中的元素没有显示出来。

有人可以请建议我如何实现这一点。我坚持..

我customview代码:

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(); 
    } 

} 

我的一个布局文件:

<?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> 

回答

-1

充气意见后组布局PARAMS。

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams([width], [heigth]); 
//assign additional params like below, above, to right or to left or others relative layout params 
inflatedView.setLayoutParams(lp); 
+0

它没有工作。实际上,如果我只是在XML中保留一个textview并删除RelativeLaout,它显示正常。但我需要RelativeLayout,因为我必须在其中放入更多元素。无论如何感谢您的帮助 – Sushil

2

“你这样做是错误的”

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

+0

感谢您的回答。是。我意识到它应该是RelativeLayout在我的情况或ViewGroup。但是我无法正确编写onLayout函数。你能指导我在我的案例中的一些代码示例。 – Sushil

+0

是的!请用一些代码指导。 –

+0

@FaizanMubasher只是检查文档:http://developer.android.com/reference/android/view/ViewGroup.html –