我試圖以編程方式添加imageView,但不要在不同的屏幕上製作相同的大小。我嘗試了很多比例代碼,但沒有任何好的結果。在不同屏幕上的Android 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;
}
}
之前,所以你的問題是什麼? – BugaIulian
請點擊屏幕鏈接並聚焦紅色箭頭。 – mess34