我有一個自定義的車輪選取器視圖我正在如上所述。是否可以將藍色矩形突出顯示的文本着色爲白色,即使只是部分?所以如果文字上有50%,它會將其中一半改爲白色,一半改爲黑色?
編輯:
我用Java代碼(移植到C#)從這裏:https://code.google.com/archive/p/android-wheel/。
基本上,藍色的矩形是一個xml定義的形狀,我放在代碼中的可滾動文本後面。
然後我繪製矩形,並在上面的項目,使用此代碼:
/**
* Draws items
* @param canvas the canvas for drawing
*/
private void drawItems(Canvas canvas)
{
canvas.Save();
int top = (currentItem - firstItem) * getItemHeight() + (getItemHeight() - this.Height)/2;
canvas.Translate(PADDING, -top + scrollingOffset);
itemsLayout.Draw(canvas);
canvas.Restore();
}
/**
* Draws rect for current value
* @param canvas the canvas for drawing
*/
private void drawCenterRect(Canvas canvas)
{
int center = this.Height/2;
int offset = (int)(getItemHeight()/2 * 1.2);
centerDrawable.SetBounds(0, center - offset, this.Width, center + offset);
centerDrawable.Draw(canvas);
}
從我所知道的,矩形本身不具有文本屬性設置像高亮或ColorPrimaryInverse性質。
編輯2:
從我可以告訴我需要使用弗拉基米爾代碼在被覆蓋的的onDraw爲每個彌補我的控制TextViews的。這是我到目前爲止:
protected override void OnDraw(Canvas canvas)
{
Rect rect = new Rect();
this.GetDrawingRect(rect);
Paint mpaint = new Paint();
mpaint.Color = Color.Black;
mpaint.SetStyle(Style.Fill);
canvas.Save();
canvas.ClipRect(rect, Region.Op.Difference);
this.SetTextColor(Color.Black);
base.OnDraw(canvas);
canvas.Restore();
mpaint.Color = Color.White;
canvas.Save();
canvas.ClipRect(rect, Region.Op.Replace); // lets draw inside center rect only
this.SetTextColor(Color.White);
base.OnDraw(canvas);
canvas.Restore();
}
但這隻會將所有元素的文本顏色更改爲白色。我覺得我在這附近,任何幫助將不勝感激!
你問是否可以給文本(如04:00)兩種顏色(白色和黑色)?我認爲只能用不同顏色對文本的部分/字符進行着色。您可以嘗試設置突出顯示的顏色,如[這裏建議](http://stackoverflow.com/questions/3451347/change-the-color-of-highlighted-text-in-android),看看整個文本是否改變,或只有突出部分。你也可以檢查[textColorPrimaryInverse](http://developer.android.com/reference/android/R.attr.html#textColorPrimaryInverse)。 – ishmaelMakitla
是的,確切地說。我會研究這些屬性,但我不認爲我的代碼/ xml是以這種方式設置的,這使得可能。我想我會編輯我的問題,以顯示我迄今爲止所做的工作。 – Chucky