相关文章推荐
文武双全的足球  ·  ASP.NET Core 搭配 Nginx ...·  2 年前    · 
活泼的罐头  ·  安裝 NuGet 用戶端工具 | ...·  2 年前    · 
粗眉毛的铁板烧  ·  如何在不刷新整个页面的情况下刷新React组件?·  2 年前    · 
越狱的橙子  ·  Python处理Excel第三方库的常用方法 ...·  2 年前    · 
聪明伶俐的围巾  ·  oracle字符转数字排序-掘金·  2 年前    · 
Code  ›  android - 如何将 int 转换为 drawable? -
position
https://segmentfault.com/q/1010000043181562
飘逸的李子
3 年前
segmentfault segmentfault
注册登录
问答 博客 标签 活动
发现
✓ 使用“Bing”搜本站 使用“Google”搜本站 使用“百度”搜本站 站内搜索
注册登录
  1. 首页
  2. 问答
  3. Stack Overflow 翻译
  4. 问答详情

如何将 int 转换为 drawable?

社区维基
1
发布于
2022-12-27
新手上路,请多包涵

我创建了一个带有图标列表的对话框,即可绘制对象。我创建了一个网格视图来显示这些图标。

现在我想获取选定的图标并将其设置为我的活动中的图像视图。

那么我怎样才能将它转换成可绘制的,以便我可以在我的活动的图像视图上设置选定的图标呢?

对话代码:

  private void showAlertDialog() {
        GridView gridView = new GridView(this);
        gridView.setGravity(Gravity.CENTER);
        gridView.setPadding(0,20,0,20);
        int[] mThumbIds = {
                R.drawable.roundicons05,R.drawable.roundicons08,R.drawable.roundicons02,R.drawable.roundicons03,R.drawable.roundicons04,
                R.drawable.roundicons16,R.drawable.roundicons37,R.drawable.roundicons06,R.drawable.roundicons07,R.drawable.roundicons05,
                R.drawable.roundicons09,R.drawable.roundicons10,R.drawable.roundicons11,R.drawable.roundicons12,R.drawable.roundicons13,
                R.drawable.roundicons14,R.drawable.roundicons15,R.drawable.roundicons16,R.drawable.roundicons17,R.drawable.roundicons18,
                R.drawable.roundicons19,R.drawable.roundicons20,R.drawable.roundicons22,
        gridView.setAdapter(new ImageAdapter(CheckListActivity.this,mThumbIds));
        gridView.setNumColumns(4);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // do something here
             //   icon.setImageDrawable(id);
                imageId = position;
                Drawable drawable = getResources().getDrawable(imageId);
                icon.setImageDrawable(drawable);
        final MaterialDialog dialog = new MaterialDialog.Builder(CheckListActivity.this)
                .customView(gridView, false)
                .title("Select Icon")
                .negativeText("CANCEL")
                .canceledOnTouchOutside(true)
                .build();
        dialog.show();

我这样试过

  imageId = position;
  Drawable drawable = getResources().getDrawable(imageId);
  icon.setImageDrawable(drawable);

但这会引发未发现资源异常。

原文由 user6265109 发布,翻译遵循 CC BY-SA 4.0 许可协议

Stack Overflow 翻译 android int drawable android-gridview
阅读 173
2 个回答
得票 最新
社区维基
1
发布于
2022-12-27
✓ 已被采纳

而不是使用 position 作为 ResourceID(不是),而是在 — 获取 mThumbIds 数组的 positions 。所以你的代码应该是这样的:

 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // do something here
    imageId = mThumbIds[position];
    Drawable drawable = getResources().getDrawable(imageId);
    icon.setImageDrawable(drawable);

我希望这有帮助。

原文由 ishmaelMakitla 发布,翻译遵循 CC BY-SA 3.0 许可协议

社区维基
1
发布于
2022-12-27

getDrawable (int) 方法已弃用。现在您可以轻松使用

    icon.setImageResource(imageId);
 
推荐文章
文武双全的足球  ·  ASP.NET Core 搭配 Nginx 的真实IP问题 - 晓晨Master - 博客园
2 年前
活泼的罐头  ·  安裝 NuGet 用戶端工具 | Microsoft Learn
2 年前
粗眉毛的铁板烧  ·  如何在不刷新整个页面的情况下刷新React组件?
2 年前
越狱的橙子  ·  Python处理Excel第三方库的常用方法_xlwt与openpyxl的区别-CSDN博客
2 年前
聪明伶俐的围巾  ·  oracle字符转数字排序-掘金
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号