android - 在 kotlin 中,如何创建个人资料图片并将联系人姓名的首字母显示为离线短信应用中的个人资料图片?
问题描述
我必须创建离线应用程序,我想将个人资料图片附在联系人姓名旁边,并将第一个字母作为个人资料图片。但我不明白如何编码。
我已经搜索过但一无所获。
1.
class Inbox : AppCompatActivity(){
private val requestReceiveSms: Int =1
private val requestReadSms: Int = 2
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.readsms)
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.SEND_SMS) !=
PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this, arrayOf(android.Manifest.permission.SEND_SMS,android.Manifest.permission.READ_CONTACTS),
requestReadSms
)
} else {
refreshSmsInbox()
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,
grantResults: IntArray) {
if(requestCode == requestReadSms) refreshSmsInbox()
}
private fun refreshSmsInbox() {
val smsList = ArrayList<SmsData>()
val cursor = contentResolver.query(Uri.parse("content://sms/inbox"),null,null,null,null)
if(cursor!!.moveToFirst()){
val nameID = cursor.getColumnIndex("address")
val messageID = cursor.getColumnIndex("body")
val dateID = cursor.getColumnIndex("date")
do{
val dateString = cursor.getString(dateID)
smsList.add(SmsData(cursor.getString(nameID),cursor.getString(messageID),Date(dateString.toLong()).toString()))
}while (cursor.moveToNext())
}
cursor.close()
val adapter = ListAdapter(this, smsList)
sms_list_view.adapter = adapter
}
}
文件
2.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/sms_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
tools:ignore="Suspicious0dp"></ListView>
</LinearLayout>
预期的
个人资料图片以及联系人姓名。
实际结果
只是联系人姓名。
解决方案
您可以TextView
在 ListView 的项目布局中放置一个。现在我们为此设置一个圆形彩色背景TextView
。首先,我们drawable
为 TextView 定义一个背景,
profile_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="@android:color/holo_blue_bright"/>
</shape>
</item>
</selector>
TextView
我们在 ListView 的项目布局中将此背景设置为 a 。
listview_item_layout.xml
<TextView
android:id="@+id/profile_text"
android:layout_width="56dp"
android:layout_height="56dp"
android:background="@drawable/sample"
/>
现在,我们需要将 the 的第一个字母设置nameId
为 this TextView
,
val firstLetter = nameId.subString( 0 , 1 ).toUpperCase()
profile_text.text = firstLetter
推荐阅读
- c# - 无法在本地运行 Cosmos DB 更改源触发 Azure 函数
- angular - Angular 4 Rxjs 主题订阅不起作用
- angular - 如何以角度重定向到身份服务器登录?
- docker - Docker 安装:ImportError: cannot import name '_gi' from 'gi' (/usr/lib/python3/dist-packages/gi/__init__.py)
- c++ - 由于先前安装了早期版本,Makefile 无法安装代码
- sql - postgreSQL 查询似乎在无限循环上运行
- r - 在 R 中将子集设置为正数时出现负下标错误
- php - 如何设置联系表格 7 插件以在没有电子邮件的情况下从来宾用户那里获取消息
- c++ - CMake - 此项目中使用了以下变量,但它们设置为 NOTFOUND
- google-app-engine - AppEngine 队列中每个实例运行 10 个任务的硬性限制