这是我的Popup
班,
public class Popup {
public void showDialog(Activity activity, String url){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.popup_playerstats);
ImageView imageFirst= (ImageView) dialog.findViewById(R.id.img_First);
ImageView dialogButton = (ImageView) dialog.findViewById(R.id.close);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
我的片段的onClick方法是,
public void onClick(final View v) {
Popup alert = new Popup();
switch (v.getId()) {
case R.id.button_1:
alert.showDialog(getActivity(),url);
break;
case R.id.button_2:
alert.showDialog(getActivity(),url1);
break;
default:
// some code here
break;
}
}
我需要在ImageView url
上使用String变量url1
或imageFirst
和setimage。
帮我怎么做?
答案 0 :(得分:1)
首先将 Glide 依赖关系添加到 build.gradle
部分。
dependencies {
// glide
compile 'com.github.bumptech.glide:glide:3.7.0'
}
然后
ImageView imageFirst= (ImageView) dialog.findViewById(R.id.img_First);
Glide.with(activity).load(url)
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageFirst);
答案 1 :(得分:0)
在dependencies
compile 'com.github.bumptech.glide:glide:3.7.0'
并用于在ImageView中设置
Glide.with(mContext)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.dontAnimate()
.placeholder(ContextCompat.getDrawable(mContext, R.mipmap.ocassion_placeholder))
.into(imageFirst);
答案 2 :(得分:0)
在build.gradle文件中添加依赖项
compile 'com.github.bumptech.glide:glide:3.7.0'
使用Dialog
扩展您的弹出类public class Popup extends Dialog{
private Context mContext;
private ImageView imageFirst;
private String url;
public Popup(Context context, String url) {
super(context);
mContext = context;
this.url = url;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.popup_playerstats);
imageFirst= (ImageView) findViewById(R.id.img_First);
Glide.with(mContext)
.load(url)
.error(R.drawable.error)
.into(imageFirst);
ImageView dialogButton = (ImageView) findViewById(R.id.close);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
}
在您的活动中,onClickEvent
public void onClick(final View v) {
Popup popup;
switch (v.getId()) {
case R.id.button_1:
popup = new Popup(mContext, url);
break;
case R.id.button_2:
popup = new Popup(mContext, url1);
break;
default:
// some code here
break;
popup.setCancelable(false);
popup.show();
}
}
答案 3 :(得分:0)
添加另一个变量以传递给showDialog方法,以从onClick方法传递图像的位置ID。然后在showDialog方法中,使用此id将其设置为图像资源。
的onClick:
public void onClick(final View v) {
Popup alert = new Popup();
switch (v.getId()) {
case R.id.button_1:
alert.showDialog(getActivity(),url,R.drawable.img1);
break;
case R.id.button_2:
alert.showDialog(getActivity(),url1,R.drawable.img2);
break;
default:
// some code here
break;
}
}
的ShowDialog:
public class Popup {
public void showDialog(Activity activity, String url, int id){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.popup_playerstats);
ImageView imageFirst= (ImageView) dialog.findViewById(R.id.img_First);
imageFirst.setImageResource(id);
ImageView dialogButton = (ImageView) dialog.findViewById(R.id.close);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}