Android studio4.0.1对应的gradle是哪个版本?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android studio4.0.1对应的gradle是哪个版本?相关的知识,希望对你有一定的参考价值。

参考技术A 我这边是 gradle-6.1.1-all.zip

无所谓对不对应的吧, 5.2 以上的应该都能用, AS 会有提示. build.gradle 里面就是 对应的AS 版本 4.0.1, 不清楚的时候, 新建一个 Project 看一下就可以了(这个方法好好用的), 升级的gradle版本的时候也不要手动修改, 一般右下角时不时会弹出更新的提示, 点更新就自动配置好了

如何在 Android Studio 中将文本设置为对应的图像

【中文标题】如何在 Android Studio 中将文本设置为对应的图像【英文标题】:How to set text to corresponding images in Android Studio 【发布时间】:2021-04-16 08:21:13 【问题描述】:

我正在开发一个 android 应用程序,我想在其中设置图像视图中的图像,并通过单击按钮从字符串数组中获取相应的文本。 我已经把我的图片放在了drawable文件夹中 字符串数组是:

private String[] captions =new String[] 
        /*0*/"man in white shirt and glasses is sitting at table",
        /*1*/"man in black shirt is playing the guitar",
        /*2*/"man in blue shirt is standing in front of water",
        /*3*/"two dogs are running through the snow",
        /*4*/"group of people are standing in front of crowd",
        /*5*/"two dogs are playing in the snow",
        /*6*/"group of people are sitting at the table",
        /*7*/"man in blue shirt is standing in front of building",
        /*8*/"man in blue shirt is standing in the water of the water",
        /*9*/"man in black shirt playing the guitar",
        /*10*/"man in black shirt and blue is sitting on the beach",
        /*11*/"group of people are standing in front of building",
        /*12*/"group of people are standing in the street",
        /*13*/"man in white shirt is holding his face",
        /*14*/"man in white shirt is cooking",
        /*15*/"two men are playing in the grass",
        /*16*/"man in black shirt and white is playing the guitar",
        
;

我可以在这里使用 if 条件。我在这个项目中使用视图绑定,这就是我访问 binding.buttonid 之类的按钮的原因。

 binding.detectCaption.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            //binding.displayView.setText("two dogs are running through the snow");
            if (binding.imageView==R.drawable.one)//This line shows error
                binding.displayView.setText(captions[0]);
            
           

        
    );

请告诉我该怎么做。

【问题讨论】:

【参考方案1】:

在您的情况下,您正在检查您的 ImageView 对象是否等于 drawable,它们不是相同的对象。

不错的解决方案是,在将 drawable 设置为 imageview 时,您还可以放置如下标签:

binding.imageView.setTag("Your tag");

我不确定你是如何设置这些图像的,但我假设一些整数,所以在这里,在你的标签中,你可以设置你的图像的整数(“1”代表“o​​ne.png”,“2”代表“两个.png" 等),然后你甚至不需要使用 if 条件,只需设置描述,如

binding.displayView.setText(captions[(int) binding.imageView.getTag()]);

【讨论】:

我使用手机的图库在 ImageView 中设置图像。但我无法获得该上传图像的信息。建议我如何获取此信息的方法,以便我可以轻松地在 if 条件下进行比较。 我不确定您到底在寻找什么。以下是检索图像名称的方法:***.com/questions/23777027/… 之后,您可以设置任何有助于识别标题的标签。在你的 if 中使用“getTag()”,而不是 ImageView 对象【参考方案2】:

问题是您试图将 ImageViewIntegerDrawable 的资源 ID)进行比较。

另一个更普遍的问题是 ImageView 的 id 与其资源(即 Drawable)的 id 不匹配,因此无论哪种方式,您的条件都有缺陷。

您可以做的是根据当前 Drawable 的 id 设置 ImageView 的 tag 并以此为基础,但这仅在您以编程方式设置资源或您真的需要时才有意义知道当前的 Drawable 是什么。如果不是这样,请使用@Dorian Pavetić 的答案。

// first set a drawable as example
binding.imageView.setImageResource(R.drawable.one);
// and now give the imageView a tag that matches the resource's id
binding.imageView.setTag(R.drawable.one);

// change the condition:
binding.detectCaption.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            //binding.displayView.setText("two dogs are running through the snow");
            Object tag = binding.imageView.getTag(); 
            if ( tag != null && (int) tag == R.drawable.one) 
                binding.displayView.setText(captions[0]);
        


    
);    

【讨论】:

在此方法中,您已经将图像设置为 ImageView,但在我的情况下,我设置了来自画廊的图像(与我在可绘制文件夹中上传的相同图像)。在这里,我无法获取通过图库上传的照片的名称或 ID。建议我如何获取这些信息。【参考方案3】:

您无法从 ImageView 获取可绘制的 id。 因此,您可以使用标签、自定义 ImageView 或任何其他方式。 要使用标签,@TimonNetherlands 和@Dorian Pavetić 已经提到过。

所以我描述一下自定义的 ImageView。

public class YourImageView extends ImageView 

    private int drawableId;

    public YourImageView(Context context) 
        super(context);
    

    public YourImageView(Context context, AttributeSet attrs) 
        super(context, attrs);
    

    public YourImageView(Context context, AttributeSet attrs, int defStyleAttr) 
        super(context, attrs, defStyleAttr);
    

    @Override
    public void setImageResource(int resId) 
        super.setImageResource(resId);
        this.drawableId = resId;
    

    public int getResourceId() 
        return drawableId;
    

【讨论】:

以上是关于Android studio4.0.1对应的gradle是哪个版本?的主要内容,如果未能解决你的问题,请参考以下文章

RecyclerView

不同版本 Gradle ,怎么和平共处

Android的简易圆形头像

Android开发百科全书②

洛谷 P3507 [POI2010]GRA-The Minima Game

如何在android中改变可绘制形状的颜色