【问题标题】:How to use retrieve image as drawable of radio button如何使用检索图像作为单选按钮的可绘制对象
【发布时间】:2021-06-17 00:30:35
【问题描述】:

我正在使用 RadioButton

 <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:id="@+id/optionC"
        android:textColor="@color/black"
        android:text="@string/opetion1"
        android:drawableRight="@drawable/ic_google_logo"
        android:textSize="18sp"
        android:elevation="3dp"
        android:translationZ="3dp"
        android:stateListAnimator="@null" />

现在我想将 drawableRihgt 设置为检索到的图像。我在 URL 的帮助下从实时数据库中获取。我的问题是我如何在 RadioButton 的 drawableRight 或 drawable 中设置检索到的图像

【问题讨论】:

    标签: javascript java android image image-processing


    【解决方案1】:

    您可以为此使用 Picasso 和 imageView。从 url 加载图像后,您可以将其显示在 RadioButton 的右侧。如果您想为此编写示例代码,我可以帮助您。

    例如:

    在 app/build.gradle 中,

    implementation 'com.squareup.picasso:picasso:2.5.2'
    

    在 res/layout/activity_main.xml 中,

       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       tools:context=".MainActivity">
    
       <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:id="@+id/optionC"
        android:textColor="@color/black"
        android:text="@string/opetion1"
        android:drawableRight="@drawable/ic_google_logo"
        android:textSize="18sp"
        android:elevation="3dp"
        android:translationZ="3dp"
        android:stateListAnimator="@null" />
    
       <ImageView
          android:id="@+id/image_view"
          android:layout_width="200dp"
          android:layout_toRightOf="@id/optionC"
          android:layout_height="300dp"/>
    
    </RelativeLayout>
    

    在 MainActivity.kt 中,

    import android.os.Bundle
    import android.widget.ImageView
    import androidx.appcompat.app.AppCompatActivity
    import com.squareup.picasso.Picasso
    
    class MainActivity : AppCompatActivity() {
       override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          val imageView: ImageView = findViewById(R.id.image_view)
          val url = "YOUR_URL"
          Picasso.with(this).load(url).into(imageView)
       }
    }
    

    【讨论】:

    • 我也有电台组,使用这个电台组不起作用
    • @SarthakTiwari 您可以将这些无线电组 id 放入数组中,然后为每个它们调用 load url。
    【解决方案2】:

    您可以从 Glide 获取 位图。之后将该位图设置为 单选按钮,如下所示::

    Glide.with(getApplicationContext())
                            .asBitmap()
                            .load(URL)
                              .into(new SimpleTarget<Bitmap>() {
                                @Override
                                public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
    Drawable drw = new BitmapDrawable(getResources(), resource);
    button.setCompoundDrawablesWithIntrinsicBounds(drw, 0, 0, 0);
                                }
    
                                  @Override
                                  public void onLoadFailed(@Nullable Drawable errorDrawable) {
                                      super.onLoadFailed(errorDrawable);
                                     
                                  }
                            });
    

    以下是依赖项

    implementation 'com.github.bumptech.glide:glide:4.12.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
    

    【讨论】:

    • 当我使用图像视图时,无线电组将不起作用
    猜你喜欢
    • 1970-01-01
    • 2013-09-25
    • 2013-09-03
    • 2017-01-27
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多