使用 Picasso 加载并链接到布局文件后 ImageView 图像不显示

ImageView image not showing after loading with Picaso and linking to layout file

我想显示一个单选按钮,并在它旁边显示一个 ImageView,其中包含由 Picaso 加载的图像。我尝试将 ImageView 对象加载到相对布局中的单选按钮旁边,但只显示单选按钮而不显示图像。我是 android 的新手,非常感谢您的帮助。谁能指出问题所在?

MainActivity.java

package com.example.test.imageslider;



import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioButton;
import android.view.View;
import android.view.ViewGroup;
import com.squareup.picasso.Picasso;


public class MainActivity extends AppCompatActivity {

    ViewPager viewPager;

    private String[] imageUrls = {"https://homepages.cae.wisc.edu/~ece533/images/airplane.png",
            "https://homepages.cae.wisc.edu/~ece533/images/arctichare.png",
            "https://homepages.cae.wisc.edu/~ece533/images/baboon.png"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //viewPager = (ViewPager) findViewById(R.id.viewPager);

        //ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this);

        //viewPager.setAdapter(viewPagerAdapter);

        ImageView imageView;

        //this will set the image to your ImageView in your xml
        imageView = (ImageView)findViewById(R.id.myImage);

        Picasso.get()
                .load(imageUrls[0])
                .fit()
                .centerCrop()
                .into(imageView);
        RadioGroup ll = new RadioGroup(this);
        RadioButton rdbtn = new RadioButton(this);
        rdbtn.setId(View.generateViewId());
        ll.addView(rdbtn);
        ((ViewGroup) findViewById(R.id.radiogroup)).addView(ll);
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.test.imageslider.MainActivity">

    <!--<androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="190dp"
        android:layout_marginBottom="8dp"/> -->


    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    </RadioGroup>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myImage" />
</RelativeLayout>

尝试更改布局,因为单选按钮与图像视图重叠

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent" 
   android:orientation="vertical"

      >
<RadioGroup
    android:id="@+id/radiogroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

</RadioGroup>

<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:id="@+id/myImage" />
 </LinearLayout>

您的 ImageView 似乎与 RadioGroup 重叠了!这是我对 activity_main.xml 文件的推荐:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.test.imageslider.MainActivity">

    <!--<androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="190dp"
        android:layout_marginBottom="8dp"/> -->


    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    </RadioGroup>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myImage"
        android:layout_toEndOf="@+id/radiogroup" />
</RelativeLayout>/>

您可以自行编辑。

希望有用!

P.S:我已经从 IDE 中编辑了 activity_main.xml 文件,可能有一些拼写错误,如果你发现了一些,请更正它:)

您的 ImageView 似乎与 RadioGroup 重叠!这是我对 activity_main.xml 文件的建议:app:srcCompat="@android:drawable/sym_def_app_icon" 将替换为您的图片。

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="190dp"
        android:layout_marginBottom="8dp"/>


    <RadioGroup
        android:layout_below="@id/viewPager"
        android:id="@+id/radiogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Demo"/>
        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Demo"/>
        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Demo"/>
        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Demo"/>



    </RadioGroup>


    <ImageView
        android:layout_marginTop="20dp"
        android:layout_below="@id/radiogroup"
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@android:drawable/sym_def_app_icon"
        android:contentDescription="TODO" />
</RelativeLayout>

Give height and weight to ImageView. I have tested it and works fine,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <!--<androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="190dp"
        android:layout_marginBottom="8dp"/> -->


    <RadioGroup
            android:id="@+id/radiogroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

    </RadioGroup>

    <ImageView
            android:id="@+id/myImage"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_toEndOf="@+id/radiogroup"/>
</RelativeLayout>