2017-03-27 10 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

ありがとうございました! –

関連する問題