XML이 아닌 코드를 사용하여 ImageView의 여백을 설정하는 방법
ImageView
여백이있는 레이아웃에 알 수없는 뷰 를 추가하고 싶습니다 . XML에서는 다음
layout_margin
과 같이 사용할 수 있습니다 .
<ImageView android:layout_margin="5dip" android:src="@drawable/image" />
없다
ImageView.setPadding()
하지만,
ImageView.setMargin()
. 나는 그것이 라인에 있다고 생각
ImageView.setLayoutParams(LayoutParams)
하지만, 그것에 무엇을 공급 해야할지 확실하지 않습니다.아는 사람 있나요?
android.view.ViewGroup.MarginLayoutParams
방법이
setMargins(left, top, right, bottom)
있습니다. 직접 서브 클래스는 다음과 같습니다
FrameLayout.LayoutParams
,
LinearLayout.LayoutParams
그리고
RelativeLayout.LayoutParams
.예를 들어, 사용
LinearLayout
:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
imageView.setLayoutParams(lp);
여백을 픽셀 단위로 설정합니다. 확장하려면
context.getResources().getDisplayMetrics().density
image = (ImageView) findViewById(R.id.imageID);
MarginLayoutParams marginParams = new MarginLayoutParams(image.getLayoutParams());
marginParams.setMargins(left_margin, top_margin, right_margin, bottom_margin);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
image.setLayoutParams(layoutParams);
위의 모든 예는 실제로 것이다
교체
요구되지 않을 수 있습니다보기, 이미 존재하는 PARAMS을. 아래 코드는 기존 매개 변수를 바꾸지 않고 확장합니다.
ImageView myImage = (ImageView) findViewById(R.id.image_view);
MarginLayoutParams marginParams = (MarginLayoutParams) image.getLayoutParams();
marginParams.setMargins(left, top, right, bottom);
Kevin의 코드는 중복
MarginLayoutParams
객체를 만듭니다 . 간단한 버전 :
ImageView image = (ImageView) findViewById(R.id.main_image);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(image.getLayoutParams());
lp.setMargins(50, 100, 0, 0);
image.setLayoutParams(lp);
이미지 뷰 여백을 변경하고 다른 여백은 그대로 두십시오.
- 이 경우 이미지보기의 MarginLayoutParameters를 가져옵니다.
myImageView
MarginLayoutParams marginParams = (MarginLayoutParams) myImageView.getLayoutParams();
- 이제 변경하려는 여백을 변경하고 나머지는 그대로 두십시오.
marginParams.setMargins(marginParams.leftMargin, marginParams.topMargin, 150, //notice only changing right margin marginParams.bottomMargin);
dp로 여백을 지정하려는 경우이 방법을 사용할 수 있습니다.
private void addMarginsInDp(View view, int leftInDp, int topInDp, int rightInDp, int bottomInDp) {
DisplayMetrics dm = view.getResources().getDisplayMetrics();
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.setMargins(convertDpToPx(leftInDp, dm), convertDpToPx(topInDp, dm), convertDpToPx(rightInDp, dm), convertDpToPx(bottomInDp, dm));
view.setLayoutParams(lp);
}
private int convertDpToPx(int dp, DisplayMetrics displayMetrics) {
float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics);
return Math.round(pixels);
}
나는 이것을 간단히 사용하고 훌륭하게 작동합니다.
ImageView imageView = (ImageView) findViewById(R.id.image_id);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
layoutParams.setMargins(left, top, right, bottom);
imageView.setLayoutParams(layoutParams);
setMargins ()
의 단위는 dp가 아닌 픽셀입니다. dp로 여백을 설정하려면
values / dimens.xml
파일 안에 다음 과 같이 치수를 작성하십시오.
<resources>
<dimen name="right">16dp</dimen>
<dimen name="left">16dp</dimen>
</resources>
다음과 같이 액세스하십시오.
getResources().getDimension(R.dimen.right);
동적으로 레이아웃을 만들고 매개 변수를 setmargin ()으로 설정하면 imageView에서 직접 작동하지 않습니다.
ImageView im;
im = (ImageView) findViewById(R.id.your_image_in_XML_by_id);
RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(im.getLayoutParams());
layout.setMargins(counter*27, 0, 0, 0);//left,right,top,bottom
im.setLayoutParams(layout);
im.setImageResource(R.drawable.yourimage)
나를 위해 이것은 효과가 있었다 :
int imgCarMarginRightPx = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, definedValueInDp, res.getDisplayMetrics());
MarginLayoutParams lp = (MarginLayoutParams) imgCar.getLayoutParams();
lp.setMargins(0,0,imgCarMarginRightPx,0);
imgCar.setLayoutParams(lp);
kotlin을 사용하면 확장 기능을 만들어 단순화 할 수 있습니다
fun View.setMarginExtensionFunction(left: Int, top: Int, right: Int, bottom: Int) {
val params = layoutParams as ViewGroup.MarginLayoutParams
params.setMargins(left, top, right, bottom)
layoutParams = params
}
이제 필요한 것은보기이며이 확장 기능은 어디에서나 사용할 수 있습니다.
val imageView = findViewById(R.id.imageView)
imageView.setMarginExtensionFunction(0, 0, 0, 0)
샘플 코드는 여기에 있으며 매우 쉽습니다.
LayoutParams params1 = (LayoutParams)twoLetter.getLayoutParams();//twoletter-imageview
params1.height = 70;
params1.setMargins(0, 210, 0, 0);//top margin -210 here
twoLetter.setLayoutParams(params1);//setting layout params
twoLetter.setImageResource(R.drawable.oo);
Using a method similar to this might save you some headaches in some situations. If you have two passes of programmatical tinkering with margins it is safer to check if there are already some layoutParams set. If there are some margins already one should increase them and not replace them:
public void addMargins(View v, int left, int top, int right, int bottom) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) v.getLayoutParams();
if (params == null)
params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
int oldLeft = params.leftMargin;
int oldTop = params.topMargin;
int oldRight = params.rightMargin;
int oldBottom = params.bottomMargin;
params.setMargins(oldLeft + left, oldTop + top, oldRight + right, oldBottom + bottom);
v.setLayoutParams(params);
}
Here is an example to add 8px Margin on left, top, right, bottom.
ImageView imageView = new ImageView(getApplicationContext());
ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(
ViewGroup.MarginLayoutParams.MATCH_PARENT,
ViewGroup.MarginLayoutParams.WRAP_CONTENT
);
marginLayoutParams.setMargins(8, 8, 8, 8);
imageView.setLayoutParams(marginLayoutParams);
참고URL : https://stackoverflow.com/questions/3416087/how-to-set-margin-of-imageview-using-code-not-xml
'programing' 카테고리의 다른 글
기본 인터페이스 방법은 Android N부터 만 지원됩니다 (0) | 2020.05.25 |
---|---|
MySQL 대소 문자 구분 쿼리 (0) | 2020.05.25 |
SQL 문자열에 'if 절'을 어떻게 넣습니까? (0) | 2020.05.25 |
CSS를 사용하여 절대 div를 가로로 가운데에 배치하는 방법은 무엇입니까? (0) | 2020.05.25 |
숭고한 텍스트 3, 공백을 탭으로 변환 (0) | 2020.05.25 |