2013-03-11 27 views
0

我在Android中使用了一個定製設計的CalendarView。雖然我在佈局中使用Scrollview,但是自定義設計的CalendarView未顯示在屏幕中。定製設計的視圖在使用ScrollView時消失了

什麼可能導致CalendarView消失?

我的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

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

     <Button 
      android:id="@+id/NextMonth" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:text="@string/NextMonth" /> 

     <Button 
      android:id="@+id/PreviousMonth" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:text="@string/PreviousMonth" /> 

     <TextView 
      android:id="@+id/MonthText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:text="Month" /> 

     <TextView 
      android:id="@+id/SundayText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/PreviousMonth" 
      android:layout_marginLeft="25dp" 
      android:text="@string/SundayText" 
      android:textSize="10sp" /> 

     <TextView 
      android:id="@+id/MondayText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/PreviousMonth" 
      android:layout_marginLeft="22dp" 
      android:layout_toRightOf="@+id/SundayText" 
      android:text="@string/MondayText" 
      android:textSize="10sp" /> 

     <TextView 
      android:id="@+id/TuesdayText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/PreviousMonth" 
      android:layout_marginLeft="20dp" 
      android:layout_toRightOf="@+id/MondayText" 
      android:text="@string/Tuesday" 
      android:textSize="10sp" /> 

     <TextView 
      android:id="@+id/WednesdayText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/PreviousMonth" 
      android:layout_marginLeft="22dp" 
      android:layout_toRightOf="@+id/TuesdayText" 
      android:text="@string/Wednesday" 
      android:textSize="10sp" /> 

     <TextView 
      android:id="@+id/Thursday" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/PreviousMonth" 
      android:layout_marginLeft="20dp" 
      android:layout_toRightOf="@+id/WednesdayText" 
      android:text="@string/ThurdayText" 
      android:textSize="10sp" /> 

     <TextView 
      android:id="@+id/Friday" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/PreviousMonth" 
      android:layout_marginLeft="22dp" 
      android:layout_toRightOf="@+id/Thursday" 
      android:text="@string/FridayText" 
      android:textSize="10sp" /> 

     <TextView 
      android:id="@+id/Saturday" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/PreviousMonth" 
      android:layout_marginLeft="27dp" 
      android:layout_toRightOf="@+id/Friday" 
      android:text="@string/SaturdayText" 
      android:textSize="10sp" /> 

     <com.example.calendar_module.CalendarView 
      android:id="@+id/calendar" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/NextMonth" /> 
    </RelativeLayout> 

</ScrollView> 

我CalenderView類文件:

package com.example.calendar_module; 

import java.util.Calendar; 

import android.app.ActionBar.LayoutParams; 
import android.content.Context; 
import android.content.res.Resources; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Rect; 
import android.graphics.drawable.Drawable; 
import android.util.AttributeSet; 
import android.util.MonthDisplayHelper; 
import android.view.MotionEvent; 
import android.widget.ImageView; 

public class CalendarView extends ImageView { 

private static int WEEK_TOP_MARGIN = 0; 
private static int WEEK_LEFT_MARGIN = 05; 
private static int CELL_WIDTH = 20; 
private static int CELL_HEIGH = 20; 
private static int CELL_MARGIN_TOP = 05; 
private static int CELL_MARGIN_LEFT = 29; 
private static float CELL_TEXT_SIZE; 

private static final String TAG = "CalendarView"; 
private String[] mDayString = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; 
private Calendar mRightNow = null; 
private Drawable mWeekTitle = null; 
private Cell mToday = null; 
private Cell[][] mCells = new Cell[6][7]; 
private Cell[] mDayCells = new Cell[7]; 
private OnCellTouchListener mOnCellTouchListener = null; 
MonthDisplayHelper mHelper; 
Drawable mDecoration = null; 

public interface OnCellTouchListener { 
    public void onTouch(Cell cell); 
    } 

public CalendarView(Context context) { 
    this(context, null); 
} 

public CalendarView(Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
} 

public CalendarView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    mDecoration = context.getResources().getDrawable(R.drawable.typeb_calendar_today);  
    initCalendarView(); 
} 

private void initCalendarView() { 
    mRightNow = Calendar.getInstance(); 
    // prepare static vars 
    Resources res = getResources(); 
    WEEK_TOP_MARGIN = (int) res.getDimension(R.dimen.week_top_margin); 
    WEEK_LEFT_MARGIN = (int) res.getDimension(R.dimen.week_left_margin); 

    CELL_WIDTH = (int) res.getDimension(R.dimen.cell_width); 
    CELL_HEIGH = (int) res.getDimension(R.dimen.cell_heigh); 
    CELL_MARGIN_TOP = (int) res.getDimension(R.dimen.cell_margin_top); 
    CELL_MARGIN_LEFT = (int) res.getDimension(R.dimen.cell_margin_left); 

    CELL_TEXT_SIZE = res.getDimension(R.dimen.cell_text_size); 
    // set background 
     //  setImageResource(R.drawable.background); 
    mWeekTitle = res.getDrawable(R.drawable.calendar_week); 

    mHelper = new MonthDisplayHelper(mRightNow.get(Calendar.YEAR), mRightNow.get(Calendar.MONTH)); 

    } 

private void initCells() { 

    class _calendar { 
     public int day; 
     public boolean thisMonth; 
     public _calendar(int d, boolean b) { 
      day = d; 
      thisMonth = b; 
     } 
     public _calendar(int d) { 
      this(d, false); 
     } 
    }; 
    _calendar tmp[][] = new _calendar[6][7]; 

    for(int i=0; i<tmp.length; i++) { 
     int n[] = mHelper.getDigitsForRow(i); 
     for(int d=0; d<n.length; d++) { 
      if(mHelper.isWithinCurrentMonth(i,d)) 
       tmp[i][d] = new _calendar(n[d], true); 
      else 
       tmp[i][d] = new _calendar(n[d]); 

     } 
    } 

    Calendar today = Calendar.getInstance(); 
    int thisDay = 0; 
    mToday = null; 
    if(mHelper.getYear()==today.get(Calendar.YEAR) && mHelper.getMonth()==today.get(Calendar.MONTH)) { 
     thisDay = today.get(Calendar.DAY_OF_MONTH); 
    } 


    //  // build cells 
    Rect Bound = new Rect(CELL_MARGIN_LEFT, CELL_MARGIN_TOP, CELL_WIDTH+CELL_MARGIN_LEFT, CELL_HEIGH+CELL_MARGIN_TOP); 
    //  for(int i=0 ; i < 7 ; i++) 
    //  { 
    //   
    //   mDayCells[i] = new Cell(mDayString[i],new Rect(Bound),CELL_TEXT_SIZE); 
    //   Bound.offset(CELL_WIDTH, 0); 
    //    
    //  } 
//  
//  Bound.offset(0, CELL_HEIGH);  // move to next row and first column 
//  Bound.left = CELL_MARGIN_LEFT; 
//  Bound.right = CELL_MARGIN_LEFT+CELL_WIDTH; 
//  

    for(int week=0; week<mCells.length; week++) { 
     for(int day=0; day<mCells[week].length; day++) 
     { 

      if(tmp[week][day].thisMonth) { 
       if(day==0 || day==6) 
        mCells[week][day] = new RedCell(tmp[week][day].day, new Rect(Bound), CELL_TEXT_SIZE); 
       else 
        mCells[week][day] = new Cell(tmp[week][day].day, new Rect(Bound), CELL_TEXT_SIZE); 
      } else { 

       mCells[week][day] = new GrayCell(tmp[week][day].day, new Rect(Bound), CELL_TEXT_SIZE); 
      } 

      Bound.offset(CELL_WIDTH, 0); // move to next column 

      // get today 
      if(tmp[week][day].day==thisDay && tmp[week][day].thisMonth) { 
       mToday = mCells[week][day]; 
       mDecoration.setBounds(mToday.getBound()); 
      } 
     } 
     Bound.offset(0, CELL_HEIGH); // move to next row and first column 
     Bound.left = CELL_MARGIN_LEFT; 
     Bound.right = CELL_MARGIN_LEFT+CELL_WIDTH; 

    }  
    } 

@Override 
public void onLayout(boolean changed, int left, int top, int right, int bottom) { 
    //  Rect re = getDrawable().getBounds(); 
//  WEEK_LEFT_MARGIN = CELL_MARGIN_LEFT = (right-left - re.width())/2; 
//  mWeekTitle.setBounds(WEEK_LEFT_MARGIN, WEEK_TOP_MARGIN, WEEK_LEFT_MARGIN+mWeekTitle.getMinimumWidth(), WEEK_TOP_MARGIN+mWeekTitle.getMinimumHeight()); 

    initCells(); 
    super.onLayout(changed, left, top, right, bottom); 
} 
@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    setMeasuredDimension(CELL_WIDTH, CELL_HEIGH); 
} 

public void setTimeInMillis(long milliseconds) { 
    mRightNow.setTimeInMillis(milliseconds); 
    initCells(); 
    this.invalidate(); 
} 

public int getYear() { 
    return mHelper.getYear(); 
} 

public int getMonth() { 
    return mHelper.getMonth(); 
} 

public void nextMonth() { 
    mHelper.nextMonth(); 
    initCells(); 
    invalidate(); 
} 

public void previousMonth() { 
    mHelper.previousMonth(); 
    initCells(); 
    invalidate(); 
} 

public boolean firstDay(int day) { 
    return day==1; 
} 

public boolean lastDay(int day) { 
    return mHelper.getNumberOfDaysInMonth()==day; 
} 

public void goToday() { 
    Calendar cal = Calendar.getInstance(); 
    mHelper = new MonthDisplayHelper(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)); 
    initCells(); 
    invalidate(); 
} 

public Calendar getDate() { 
    return mRightNow; 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    if(mOnCellTouchListener!=null){ 
     for(Cell[] week : mCells) { 
      for(Cell day : week) { 
       if(day.hitTest((int)event.getX(), (int)event.getY())) { 
        mOnCellTouchListener.onTouch(day); 
       }      
      } 
     } 
    } 
    return super.onTouchEvent(event); 
} 

public void setOnCellTouchListener(OnCellTouchListener p) { 
    mOnCellTouchListener = p; 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    // draw background 
    super.onDraw(canvas); 
    mWeekTitle.draw(canvas); 

    // draw cells 
    for(Cell[] week : mCells) { 
     for(Cell day : week) { 
      day.draw(canvas);   
     } 
    } 

    // draw today 
    if(mDecoration!=null && mToday!=null) { 
     mDecoration.draw(canvas); 
    } 
} 

public class GrayCell extends Cell { 
    public GrayCell(int dayOfMon, Rect rect, float s) { 
     super(dayOfMon, rect, s); 
     mPaint.setColor(Color.LTGRAY); 
    }   
} 

private class RedCell extends Cell { 

    public RedCell(int dayOfMon, Rect rect, float s) { 
     super(dayOfMon, rect, s); 
     mPaint.setColor(0xdddd0000); 
    }   

} 


} 
+0

我猜你什麼時候不使用ScrollView它有效? – Neil 2013-03-11 10:30:13

+0

你可以在這裏張貼你想要存檔的截圖嗎?我認爲你只需要用一個或兩個線性佈局來重構你的佈局。 – 2013-03-11 10:38:29

+0

@FestusTamakloe:我無法上傳圖片。我已經更新了問題中的CalendarView類。 – venkat 2013-03-11 12:15:35

回答

0

我認爲,height您的自定義view的等於== 0,嘗試登錄它。如果它是正確的,只需覆蓋onMeasure併爲您的view設置大小。

和esle:爲什麼在xml中height == match_parent?你能想象它在ScrollView的樣子嗎?)

UPD:好的,在你的情況下height == 0。你必須override onMeasure()方法在自定義view這樣的:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    setMeasuredDimension(WIDTH, HEIGHT); 
} 

凡寬度和高度的像素值。

祝你好運!

+0

@llya Demidov:你是對的。在記錄日曆視圖的高度時,我將Height設置爲0。你能告訴我我現在應該在OnMeasure中做什麼? – venkat 2013-03-11 10:57:19

+0

我更新了我的帖子。如果您的日曆具有靜態大小,只需將您的常量作爲寬度和高度。當然最好的方法是將這些常量存儲在xml中作爲維度。你的看法應該取決於屏幕尺寸。 – 2013-03-11 10:57:51

+0

我已經更新了問題中的CalendarView類。我在該類中添加了SetMeasuredDimension(CELL_WIDTH,CELL_HEIGH)行。但是我仍然遇到同樣的問題:( – venkat 2013-03-11 12:04:16