2017-03-27 2 views
0

PercentRelativeLayout의 높이를 백분율로 설정하고 싶습니다.프로그래밍 방식으로 PercentRelativeLayout에서 percentHeight를 설정하는 방법

PercentRelativeLayout layout = new PercentRelativeLayout(this); 
     PercentRelativeLayout.LayoutParams llp = new PercentRelativeLayout.LayoutParams(PercentRelativeLayout.LayoutParams.MATCH_PARENT, 50%); 
     layout.setLayoutParams(llp); 

     myLayout.addView(layout); 

MatchParent 또는 Wrap_Content가 아닌 LayoutParams에서 percentHeight (50 %)를 사용한다고 가정합니다.

답변

1

이 코드 :

LinearLayout rootLayout = (LinearLayout) findViewById(R.id.root); 
PercentRelativeLayout percentRelativeLayout = new PercentRelativeLayout(this); 
percentRelativeLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent)); 
rootLayout.addView(percentRelativeLayout); 

Button button = new Button(this); 
percentRelativeLayout.addView(button); 
PercentRelativeLayout.LayoutParams params = (PercentRelativeLayout.LayoutParams) button.getLayoutParams(); 
PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo(); 
info.heightPercent = 0.5f; 

이 출력이 뜻 :

PercentRelativeLayout takes half the screen height

+1

나는 코드를 시도했지만 작동하지 않습니다. 이 줄 어디 엔 오류가 있습니다.'PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo(); ' –

+0

@elbertrivas 업데이트 됨. – azizbekian

+0

정말 고마워요! –

관련 문제