티스토리 뷰
Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.
Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.
아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.
Android set height and width of Custom view programmatically
안드로이드에서 사용자 지정 뷰의 높이와 너비를 프로그래밍 방식으로 설정하기
문제 내용
I have created a custom view named Graphview
. Here is the structure for the GraphView class.
저는 GraphView라는 커스텀 뷰를 만들었습니다. GraphView 클래스 구조는 다음과 같습니다.
public class GraphView extends View {
public GraphView(Context context, float[] values, String title, String[] horlabels, String[] verlabels, boolean type) {
super(context);
........
}
..................
.................
}
I have added the view in a tablerow using addview()
. It is working fine. Now I want to set height and width for the GraphView
. How to do that?
저는 GraphView라는 사용자 지정 뷰를 만들었습니다. addView()를 사용하여 테이블 행에 뷰를 추가했는데, 잘 작동합니다. 이제 GraphView의 높이와 너비를 설정하려고 합니다. 어떻게 해야 할까요?
높은 점수를 받은 Solution
You can set height and width like this:
이렇게 높이와 너비를 설정할 수 있습니다:
myGraphView.setLayoutParams(new LayoutParams(width, height));
가장 최근 달린 Solution
If you're using Kotlin, you can also use the following code which applies your given lambda on the current layout params:
만약 Kotlin을 사용한다면, 다음과 같은 코드를 사용할 수 있습니다. 이 코드는 현재 레이아웃 파라미터에 지정된 람다를 적용합니다.
someView.updateLayoutParams {
height = 200
}
출처 : https://stackoverflow.com/questions/5042197/android-set-height-and-width-of-custom-view-programmatically
'개발 > 안드로이드' 카테고리의 다른 글
Activity의 content view를 가져오기 (0) | 2023.02.17 |
---|---|
ViewModel 생성자에 매개변수 추가하기 (0) | 2023.02.16 |
안드로이드 WebView에서 JavaScript가 작동하지 않을 때 해결 방법 (0) | 2023.02.16 |
Android MVVM ViewModel에서 Context 얻기 (0) | 2023.02.15 |
Android - WebView에서 화면 회전 시 새로 고침 막는 방법 (0) | 2023.02.14 |