我的自定义视图正在被剪裁我的问题是,如果我将我的视图的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