【发布时间】:2014-07-08 04:28:52
【问题描述】:
我正在尝试以编程方式添加 imageView,但不要在不同的屏幕上设置相同的尺寸。我尝试了很多比例代码,但没有任何好的结果。
这是我的代码:
public class Explore extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.tab, container, false);
LinearLayout linearLayout= new LinearLayout(getActivity());
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
final float scale = view.getContext().getResources().getDisplayMetrics().density;
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 250, getResources().getDisplayMetrics());
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 82, getResources().getDisplayMetrics());
int lastWidth = (int) (width * scale + 0.5f);
int lastHeight = (int) (height * scale + 0.5f);
ImageView imageView = new ImageView(getActivity());
UrlImageViewHelper.setUrlDrawable(imageView, "https://example.com/example.jpg");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(lastWidth, lastHeight);
imageView.setLayoutParams(lp);
linearLayout.addView(imageView);
return linearLayout;
}
}
【问题讨论】:
-
那么你的问题是什么?
-
请点击屏幕链接并聚焦红色箭头。