I have a custom view that is sort of a vertical progress bar. It's just a custom view, drawing onto the canvas to fill from bottom to top with the percent complete. In my xml, I have text above, and text below the custom control (there are two of these, one on the left, one on the right, here's the right):
<RelativeLayout android:layout_width="150dp"
android:layout_height="fill_parent" android:layout_alignParentRight="true">
<TextView 开发者_开发知识库android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/bar_right_text" android:text="Right"
android:layout_alignParentTop="true" android:gravity="center_horizontal" />
<com.myApp.VBarView android:id="@+id/right_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/bar_right_text"/>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/bar_right_time_text" android:text="Time Remaining" android:layout_below="@id/right_bar" android:gravity="center_horizontal" />
</RelativeLayout>
In my custom view, I check the height in every way I can think of, but I always get a height that goes all the way to the bottom of the screen
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// h here is 489
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
int myHt = getHeight();
int myMeasHt = getMeasuredHeight();
Rect canvasRct = canvas.getClipBounds();
// all of these (myHt, myMeasHt, and canvasRct.getHeight()) are all 489
}
So far, I have tried reversing the views in the layout, so the top text is first (top aligned), the bottom text is second (bottom aligned), and the custom control is last (layout_above the bottom text), with both match_parent and wrap_content, to no avail. I tried changing to a linear layout, still with no better result.
In the case shown in code, the top text is displayed, but the bottom text is gone. When I changed the order so the custom view was layout_above the bottom text, it just moved the drawing area up so the bottom text was visible, but covered the top text. Note that when I say the bottom text is gone, I mean gone, not just covered up. My final test was to hardcode the layout_height to "350dp" in the layout.
In all cases, I always get a height of 489 on this particular device, even with the height hard-coded. I can not figure out what I need to do to get the middle part of the screen as something I can draw on and leave the rest alone. Are custom views assumed to be the last thing in a layout? The only way I can get this to work is to change the order so the custom view is last in the layout so that the bottom of the custom area is on top of the bottom text, the bottom text is still displayed, and then manually add about 150 to the top (i.e. don't write into zero on the canvas provided to the onDraw()) so it doesn't cover the top text. Fortunately, the graphic I'm drawing in is semi-transparent, so I can see whether the text is covered or just gone.
Does anyone have more info on how this is supposed to be working? Did I manage to make the explanation of what I did more confusing than the actual problem?
Thanks, gb
Someone suggested offline that instead of just placing the custom field "above" or "below" one of the other fields, do both. Keep it as the last item in the XML, and set android:layout_above
and android:layout_below
. I did that, and now my heights come in correctly.
精彩评论