我的自定義視圖正在被剪裁我的問題是,如果我將我的視圖的Y軸設置爲除0之外的任何東西,它的繪製區域將從頂部剪切。使用日誌痕跡我發現視圖邊界(及其可繪製邊界)總是正確的位置。我試過clipChildren = false,但是這給了我一個陌生的行爲,其中繪製區域和視圖邊界不同步。這裏是所有我認爲相關的代碼如果我的自定義視圖位置不是(0,0)
//onMeasure
@Override
public void onMeasure(int w, int h){
int width = MeasureSpec.getSize(w);
//minHeight is 48dp scaled for density
setMeasuredDimension(width, minHeight);
}
//onDraw
//Note that i've ommited the log statements, however they return the correct
//coordinates for the view Rect
@Override
public void onDraw(Canvas c){
c.drawRect(trackRect, tempPaint);
}
//onLayout
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
trackRect.set(left, top, right, bottom);
mTrack.setBounds(trackRect); //irrelevant atm
}
//XML CODE
//BELOW draws a perfect rectangle with a width of match_parent and a height
//of 48dp
<?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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false" >
<com.bryanstudios.bryan.clock.LocationSwitch
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
//BELOW will cause clipping
<?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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false">
<com.bryanstudios.bryan.clock.LocationSwitch
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
//BELOW causes the view to disappear entirely
<?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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false">
<com.bryanstudios.bryan.clock.LocationSwitch
android:align_parent_bottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
我最好的猜測是我的onMeasure沒有做好。還要再次注意,無論我在哪裏定位視圖(通過w /邊距或LayoutParam屬性),視圖的邏輯定位都是準確的,但繪製的區域不是。
這很有趣,因爲我實際上是關於定位是相對於父母,但沒有做很多大聲笑。謝謝你的幫助,湯姆! – whompum
不客氣。 :)如果您對我的答案滿意,請點擊'回答'。 – Tom
哪裏是適當的地方來初始化視圖Rect然後呢? – whompum