解決了這個。
用引腳背景填充TextView並將位置動態設置爲TextView。
public Drawable createFromView(int positionNumber)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.drawable.pin_icon, null, false);
TextView tv = (TextView) view.findViewById(R.id.pin_background);
tv.setText(" "+ (positionNumber+1)); // +1 since position is starting from 0
tv.setDrawingCacheEnabled(true);
tv.layout(0, 0, 50, 50);
tv.buildDrawingCache();
Bitmap b = Bitmap.createBitmap(tv.getDrawingCache());
tv.setDrawingCacheEnabled(false);
Drawable d = new BitmapDrawable(b);
return d;
}
這真的很棒,就是我一直在尋找的東西。你有沒有機會也有一個佈局xml文件的代碼片段? – kfox
@kfox for R.drawable.pin_icon。它不過是一個包含一個TextView的線性佈局。線性佈局背景就是您的針腳。只需將TextView居中對齊即可。讓我知道你是否仍然需要它,我會提供給你。 – Harshad
如果R.drawable.pin_icon是具有文本視圖的線性佈局。這是否意味着R.id.pin_background是一個包含引腳圖像的線性佈局? –