簡體   English   中英

為 drawable 設置一個字符串值

[英]Set a string value for drawable

我該如何更換這個

    Drawable drawable = getResources().getDrawable(R.drawable.imagesample);

與字符串 img 的值? (String img 的值為 res drawable 中圖片的名稱)

我的代碼:

     String img = "sampleimage";


     // code for image in pop-up dialog

            LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
            View subView = inflater.inflate(R.layout.dialog, null);
            final ImageView subImageView = (ImageView) subView.findViewById(R.id.image);
            Drawable drawable = getResources().getDrawable(R.drawable.imagesample);
            subImageView.setImageDrawable(drawable);

    // code for a pop-up dialog
    
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Congratulations");
            builder.setMessage("Thanks for helping me!");
            builder.setView(subView);
            builder.setPositiveButton("Ok", null);
            builder.show();

不可能。 您正在嘗試做的事情根本不可能。

getDrawable方法接受一個int參數。 而你正試圖傳遞一個string 您不能使用錯誤類型的參數調用方法。

檢查https://developer.android.com/reference/android/content/res/Resources以獲取進一步參考。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM