在 iOS 中,我们有一个功能可以通过在图标的右上角显示一个小数字来为用户更新带有新待处理消息的应用程序图标,同样我想知道我们是否有方法更新一个android 中的 ImageView/ImageButton(这里我不是要更新主屏幕上的图标,而是要更新我的应用程序内的图标)。
例如,图片显示红色背景图标,其中有待阅读的消息数量为红色,如果没有消息,则显示为灰色背景。
谢谢,
最佳答案
我对这个 SO 很感兴趣,因为我也不知道该怎么做。所以我开始研究它,这就是我是如何做到的。
icon.pngres/drawable/shapecount.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="#ff2233" />
</shape>
res/layout/my_widget_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:orientation="vertical"
android:background="@null"
android:id="@+id/rlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/icon"
android:src="@drawable/icon"
android:layout_margin="0dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="50" android:textSize="9dp" android:textStyle="bold"
android:background="@drawable/shapecount"
android:textColor="#FFFFFF"
android:paddingLeft="3dp" android:paddingRight="3dp"
android:layout_margin="0dp"
android:layout_alignBottom="@+id/rlayout"
android:id="@+id/txtCount" />
</RelativeLayout>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="My App Name" android:textSize="9dp" android:textStyle="bold"
android:background="@drawable/shapecount"
android:textColor="#FFFFFF"
android:paddingLeft="3dp" android:paddingRight="3dp"
android:layout_margin="0dp"
android:layout_alignBottom="@+id/rlayout"
android:id="@+id/txtAppName" />
</LinearLayout>
主页小部件实际上是一个普通 View ,您可以像为 Activity 一样在 XML 文件中构建它。虽然有一些规则要遵循。看这个link指南
在此link其中向您展示了如何构建一个AppWidget,您可以看到以下代码:
// Get the layout for the App Widget and attach an on-click listener to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
R.layout.appwidget_provider_layout 是您的主页小部件的布局,它将是 my_widget_layout.xml
从那个 View ,您可以为任何计数 TextView 设置您想要的值:
int count = 50;//Or whatever value you set to it.
views.setTextViewText(R.id.txtCount,Integer.toString(count));
然后您只需更新小部件本身:
appWidgetManager.updateAppWidget(appWidgetId, views);
注意:
updatePeriodMillis不允许小部件每 30 分钟请求更新一次以上,所以我结合使用 BroadcastReceiver 和服务
编辑:
如果您想要的计数应该在 Activity 而不是 AppWidget 中,请参见下面的 Activity 测试。它使用与上面相同的 XML:
public class TestActivity extends Activity {
/** Called when the activity is first created. */
final Random gen = new Random();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView t = (TextView) findViewById(R.id.txtCount);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
int a = gen.nextInt(20);
t.setText(Integer.toString(a));
}
});
}
}
,new Date(), 3000L);
}
}
关于Android:是否可以用数字更新 ImageView/ImageButton 以显示新消息的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5569695/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/