我对 android 编程很陌生,但是我有一些人为我编写了一个小部件,但他们做的事情与我想要的有点不同......他们在小部件显示为位图而不是文本视图。这并没有给我我想要的控制权。让我发布我所拥有的代码,看看是否有人可以提供帮助。这是它的 .java 代码:
String strHourFormat = "hh:mm";
String strDateFormat = "MM.dd.yy";
SimpleDateFormat fHour = new SimpleDateFormat(strHourFormat);
SimpleDateFormat fDate = new SimpleDateFormat(strDateFormat);
Date date = new Date();
String am = date.getHours() > 11 ? "PM":"AM";
setContentView(R.layout.widget_ui);
TextView day = new TextView(this);
day = (TextView) findViewById(R.id.day);
day.setText("" + fDate.format( date));
day.setTypeface(Typeface.createFromAsset(this.getAssets(), "roboto_thin.ttf"));
///////////
WidgetBiggs.previousState = WidgetBiggs.getWifiState(getApplication());
SoundDebug.previousState = SoundDebug.getSoundState(getApplication());
// Update each requested appWidgetId
RemoteViews widgetView = WidgetBiggs.buildUpdate(getApplication(), -1);
for(int wid:widgetIds){
//final RemoteViews widgetView = new RemoteViews(getPackageName(),R.layout.widget_ui);
widgetView.setImageViewBitmap( R.id.img_Hour, updateFontText(""+fHour.format(date),40,100,84,100,60));
widgetView.setImageViewBitmap( R.id.img_AM, updateFontText(""+am,25,40,84,40,60));
widgetView.setImageViewBitmap( R.id.img_Day, updateFontText("" + fDate.format( date),38,150,40,150,40));
//widgetView.setImageViewBitmap( R.id.img_Weather, updateFontText(getWeatherGoogle().toString(),25,120,30,120,30));
中间还有一些其他的东西......
private Bitmap updateFontText(String time, int size, int width, int height, int widthText, int heightText)
{
Bitmap myBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
Canvas myCanvas = new Canvas(myBitmap);
Paint paint = new Paint();
Typeface clock = Typeface.createFromAsset(this.getAssets(),"roboto_thin.ttf");
paint.setAntiAlias(true);
paint.setSubpixelText(true);
paint.setTypeface(clock);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setTextSize(size);
paint.setTextAlign(Align.RIGHT );
myCanvas.drawText(time, widthText, heightText, paint);
return myBitmap;
这是我开始的 XML:
<TextView
android:id="@+id/day"
android:visibility="visible"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal|center_vertical"
android:textSize="32dp">
</TextView>
这导致我的应用程序失控。如果你看到任何突出的东西,请告诉我。我想把位图文本拿出来,把textview文本放进去。谢谢!