使用 Picasso 将图像加载到以编程方式添加的 ImageView [不工作]
Use Picasso to load image into programmatically added ImageView [NOT WORKING]
我正在尝试将 ImageView 添加到 RelativeLayout,然后使用 Picasso 将图像加载到此 ImageView。但是,这是行不通的。图片的 URL 有效。我不确定问题是出在 Picasso 上还是未添加 ImageView。
setContentView(R.layout.activity_detailed_view);
ImageView methodImage = new ImageView(getApplicationContext());
RelativeLayout detailsLayout = (RelativeLayout)findViewById(R.id.details_layout);
TextView transportationText = (TextView) detailsLayout.findViewById(R.id.transportationText);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(5, 5);
params.addRule(RelativeLayout.RIGHT_OF, transportationText.getId());
params.addRule(RelativeLayout.ALIGN_TOP, transportationText.getId());
params.addRule(RelativeLayout.END_OF, transportationText.getId());
methodImage.setLayoutParams(params);
detailsLayout.addView(methodImage);
Picasso.with(getApplicationContext()).load(img_url).placeholder(R.drawable.ic_launcher).fit().centerCrop().into(methodImage);
activity_detailed_view.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:orientation="vertical"
android:weightSum="1"
android:id="@+id/details_layout">>
<ImageView
android:layout_width="match_parent"
android:layout_height="275dp"
android:id="@+id/image" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/name2"
android:layout_below="@id/image"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/date2"
android:layout_below="@id/name2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/creator_name2"
android:layout_below="@id/date2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@id/venues"
android:layout_below="@+id/creator_name2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/transportationText"
android:layout_below="@id/venues"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/categories"
android:layout_below="@id/transportationText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
</RelativeLayout>
已更新只需更改此行
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
第二次调整ImageView的大小使用Picasso
函数resize(int,int)
Picasso.with(getApplicationContext())
.load(img_url)
.resize(5,5)
.placeholder(R.drawable.ic_launcher)
.fit().centerCrop().into(methodImage);
不要使用android:layout_below="@+id/something"
这样使用android:layout_below="@id/something"
。我们只在第一次声明id时使用@+id,当我们引用id时我们使用@id/something
我正在尝试将 ImageView 添加到 RelativeLayout,然后使用 Picasso 将图像加载到此 ImageView。但是,这是行不通的。图片的 URL 有效。我不确定问题是出在 Picasso 上还是未添加 ImageView。
setContentView(R.layout.activity_detailed_view);
ImageView methodImage = new ImageView(getApplicationContext());
RelativeLayout detailsLayout = (RelativeLayout)findViewById(R.id.details_layout);
TextView transportationText = (TextView) detailsLayout.findViewById(R.id.transportationText);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(5, 5);
params.addRule(RelativeLayout.RIGHT_OF, transportationText.getId());
params.addRule(RelativeLayout.ALIGN_TOP, transportationText.getId());
params.addRule(RelativeLayout.END_OF, transportationText.getId());
methodImage.setLayoutParams(params);
detailsLayout.addView(methodImage);
Picasso.with(getApplicationContext()).load(img_url).placeholder(R.drawable.ic_launcher).fit().centerCrop().into(methodImage);
activity_detailed_view.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:orientation="vertical"
android:weightSum="1"
android:id="@+id/details_layout">>
<ImageView
android:layout_width="match_parent"
android:layout_height="275dp"
android:id="@+id/image" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/name2"
android:layout_below="@id/image"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/date2"
android:layout_below="@id/name2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/creator_name2"
android:layout_below="@id/date2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@id/venues"
android:layout_below="@+id/creator_name2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/transportationText"
android:layout_below="@id/venues"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/categories"
android:layout_below="@id/transportationText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
</RelativeLayout>
已更新只需更改此行
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
第二次调整ImageView的大小使用Picasso
函数resize(int,int)
Picasso.with(getApplicationContext())
.load(img_url)
.resize(5,5)
.placeholder(R.drawable.ic_launcher)
.fit().centerCrop().into(methodImage);
不要使用android:layout_below="@+id/something"
这样使用android:layout_below="@id/something"
。我们只在第一次声明id时使用@+id,当我们引用id时我们使用@id/something