在 appwidget中我们可以使用button,但是却不能是button disable 为什么呢
RemoteViews 不能控制一个button可用和不可用的状态,但是可以控制它的显示与隐藏
因为我们可以利用这个造假
<Button android:id="@+id/startbutton" android:text="Start" android:visibility="visible"></Button> <Button android:id="@+id/startbutton_disabled" android:text="Start" android:clickable="false" android:textColor="#999999" android:visibility="gone"></Button> <Button android:id="@+id/stopbutton" android:text="Stop" android:visibility="gone"></Button> <Button android:id="@+id/stopbutton_disabled" android:text="Stop" android:clickable="false" android:textColor="#999999" android:visibility="visible"></Button>
然后呢
当点击startbutton的时候
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget); remoteView.setViewVisibility(R.id.startbutton, View.GONE); remoteView.setViewVisibility(R.id.startbutton_disabled, View.VISIBLE); remoteView.setViewVisibility(R.id.stopbutton, View.VISIBLE); remoteView.setViewVisibility(R.id.stopbutton_disabled, View.GONE); AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteView);
当点击stop button的时候
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget); remoteView.setViewVisibility(R.id.startbutton, View.VISIBLE); remoteView.setViewVisibility(R.id.startbutton_disabled, View.GONE); remoteView.setViewVisibility(R.id.stopbutton, View.GONE); remoteView.setViewVisibility(R.id.stopbutton_disabled, View.VISIBLE); AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteView);
其实通过一个android:clickable="false" ,还有buuton的隐藏转换 造成了视觉的欺骗
有时候我们要做一个这样子的图标,看上去很麻烦
其实很简单只要一层窗户纸
关键的是利用好.9.png格式的图片作为背景
扩展一下就可以是特殊的注释了啊
Bitmap lookup = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8); // // 确保每个路径都有一个索引值 lookup.eraseColor(0xFF000000); Canvas canvas = new Canvas(lookup); Paint paint = new Paint(); // paint.setAntiAlias(false); paint.setStyle(Paint.Style.FILL); for(int i=0;i<paths.size();i++) { paint.setColor(i<<24); // 颜色的alpha值0xXX000000 canvas.drawPath(paths.get(i), paint); }
其实呢在标准的api中是没有这个方法可以调用的,只能通过自己的算法实现
这里呢 是在画的每条线的点都作为一个索引值 然后取索引值就可以了
2.
public static void autoScaleTextViewTextToHeight(TextView tv)
{
final float initSize = tv.getTextSize();
//get the width of the view's back image (unscaled)....
float minViewHeight;
if(tv.getBackground()!=null)
{
minViewHeight = tv.getBackground().getIntrinsicHeight();
}
else
{
minViewHeight = 10f;//some min.
}
final float maxViewHeight = tv.getHeight() - (tv.getPaddingBottom()+tv.getPaddingTop())-12;// -12 just to be sure
final String s = tv.getText().toString();
//System.out.println(""+tv.getPaddingTop()+"/"+tv.getPaddingBottom());
if(minViewHeight >0 && maxViewHeight >2)
{
Rect currentBounds = new Rect();
tv.getPaint().getTextBounds(s, 0, s.length(), currentBounds);
//System.out.println(""+initSize);
//System.out.println(""+maxViewHeight);
//System.out.println(""+(currentBounds.height()));
float resultingSize = 1;
while(currentBounds.height() < maxViewHeight)
{
resultingSize ++;
tv.setTextSize(resultingSize);
tv.getPaint().getTextBounds(s, 0, s.length(), currentBounds);
//System.out.println(""+(currentBounds.height()+tv.getPaddingBottom()+tv.getPaddingTop()));
//System.out.println("Resulting: "+resultingSize);
}
if(currentBounds.height()>=maxViewHeight)
{
//just to be sure, reduce the value
tv.setTextSize(resultingSize-1);
}
}
}
以上是群里提出的解答方式 我并没有实际测 只是确实可行的