【发布时间】:2017-04-05 12:54:59
【问题描述】:
我的问题是,如果我将视图的 Y 轴设置为除 0 以外的任何值,则它的绘制区域会从顶部被剪裁。使用日志跟踪我发现视图边界(及其可绘制的边界)始终是正确的位置。我试过clipChildren=false,但这给了我一个奇怪的行为,即绘制区域和视图边界不同步。这是我认为的所有相关代码
//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 属性),视图的逻辑定位都是准确的,但绘制的区域却不是。
【问题讨论】: