프로그래밍 방식으로 ImageView 너비와 높이를 설정 하시겠습니까?
ImageView
의 너비와 높이를 프로그래밍 방식으로 어떻게 설정할 수 있습니까?
너무 늦었을 수 있지만 같은 문제를 가진 다른 사람들을 위해 ImageView의 높이를 설정하는 것이 좋습니다.
image_view.getLayoutParams().height = 20;
도움이 되었기를 바랍니다.
중대한. 레이아웃이 이미 '레이아웃'된 후 높이를 설정하는 경우 다음을 호출해야합니다.
image_view.requestLayout()
이미지보기가 동적이면 getLayout을 포함하는 응답이 null 예외로 실패합니다.
이 경우 올바른 방법은 다음과 같습니다.
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
iv.setLayoutParams(layoutParams);
작업을 수행하는이 간단한 방법 :
setContentView(R.id.main);
ImageView iv = (ImageView) findViewById(R.id.left);
int width = 60;
int height = 60;
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);
iv.setLayoutParams(parms);
다른 방법으로 화면 크기를 높이와 너비로 지정하려면 아래 코드를 사용하십시오.
setContentView(R.id.main);
Display display = getWindowManager().getDefaultDisplay();
ImageView iv = (LinearLayout) findViewById(R.id.left);
int width = display.getWidth(); // ((display.getWidth()*20)/100)
int height = display.getHeight();// ((display.getHeight()*30)/100)
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);
iv.setLayoutParams(parms);
당신에게 충분히 사용하기를 바랍니다.
이 게임을 pixel
및 dp
.
private int dimensionInPixel = 200;
픽셀 로 설정하는 방법 :
view.getLayoutParams().height = dimensionInPixel;
view.getLayoutParams().width = dimensionInPixel;
view.requestLayout();
dp 로 설정하는 방법 :
int dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
view.getLayoutParams().height = dimensionInDp;
view.getLayoutParams().width = dimensionInDp;
view.requestLayout();
이것이 당신을 도울 수 있기를 바랍니다.
너비 또는 높이를 match_parent
(부모만큼 크게) 또는 wrap_content
(자체 내부 콘텐츠에 맞도록 충분히 크게 ) 설정해야하는 경우 다음 ViewGroup.LayoutParams
두 가지 상수가 있습니다.
imageView.setLayoutParams(
new ViewGroup.LayoutParams(
// or ViewGroup.LayoutParams.WRAP_CONTENT
ViewGroup.LayoutParams.MATCH_PARENT,
// or ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT ) );
또는 Hakem Zaied의 대답 과 같이 설정할 수 있습니다 .
imageView.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
//...
모든 경우에 값을 설정할 수 있습니다.
demoImage.getLayoutParams().height = 150;
demoImage.getLayoutParams().width = 150;
demoImage.setScaleType(ImageView.ScaleType.FIT_XY);
버튼을 동적으로 설정하는 가장 쉽고 쉬운 방법은
Button index=new Button(this);
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, getResources().getDisplayMetrics());
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 42, getResources().getDisplayMetrics());
위의 높이와 폭은 픽셀에 픽셀 . 45는 dp 의 높이 이고 42는 dp 의 너비입니다 .
index.setLayoutParams(new <Parent>.LayoutParams(width, height));
따라서 예를 들어 TableLayout 내의 TableRow 내에 단추를 배치 한 경우 TableRow.LayoutParams로 가져야합니다.
index.setLayoutParams(new TableRow.LayoutParams(width, height));
LayoutParams 객체를 생성하고이를 imageView에 할당하기 만하면됩니다.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(150, 150);
imageView.setLayoutParams(params);
image_view.getLayoutParams().height
높이 값을 픽셀 단위로 반환합니다. 값을 올바르게 설정하려면 먼저 화면 크기를 가져와야합니다.
Display display = context.getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
int screen_height = outMetrics.heightPixels;
int screen_width = outMetrics.widthPixels;
After you get the screen dimensions, you can set the imageview width&height using proper margins.
view.getLayoutParams().height = screen_height - 140;
view.getLayoutParams().width = screen_width - 130 ;
In order to set the ImageView and Height Programatically, you can do
//Makesure you calculate the density pixel and multiply it with the size of width/height
float dpCalculation = getResources().getDisplayMetrics().density;
your_imageview.getLayoutParams().width = (int) (150 * dpCalculation);
//Set ScaleType according to your choice...
your_imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
In the scenario where the widget size needs to be set programmatically, ensure the below rules.
- Set
LayoutParam
for the Layout in which you are adding that view. In my case I am adding toTableRow
so I had to doTableRow.LayoutParams
- Follow this code
final float scale = getResources().getDisplayMetrics().density; int dpWidthInPx = (int) (17 * scale); int dpHeightInPx = (int) (17 * scale);
TableRow.LayoutParams deletelayoutParams = new TableRow.LayoutParams(dpWidthInPx, dpHeightInPx); button.setLayoutParams(deletelayoutParams); tableRow.addView(button, 1);
If you want to just fit the image in image view you can use" wrap content" in height and width property with scale-type but if you want to set manually you have to use LayoutParams.
Layoutparams is efficient for setting the layout height and width programmatically.
참고URL : https://stackoverflow.com/questions/3144940/set-imageview-width-and-height-programmatically
'Nice programing' 카테고리의 다른 글
계속하기 전에 쉘 스크립트를 잠시 일시 중지하려면 어떻게합니까? (0) | 2020.10.02 |
---|---|
SQL Server 2008 열 이름 바꾸기 (0) | 2020.10.02 |
Java 8에서 map과 flatMap 메소드의 차이점은 무엇입니까? (0) | 2020.10.02 |
Django 뷰에서 2 개 이상의 쿼리 셋을 결합하는 방법은 무엇입니까? (0) | 2020.10.02 |
MongoDB를 통해 CouchDB를 사용하거나 그 반대의 경우 (0) | 2020.10.02 |