【问题标题】:WebView inside layout which has percentage height except LinearlayoutWebView 内部布局,除 Linearlayout 外,具有百分比高度
【发布时间】:2020-01-31 13:26:06
【问题描述】:
我想将我的 webview 实现放入任何布局中,该布局具有百分比或任何其他内容,使我的 webview 根据它们的大小出现在各种屏幕上。例如,我希望 Web 显示也出现在 30% 的屏幕上。除了 Linearlayout 和 PercentageLayout(已过时),因为我不想更改 xml 根目录。如何实现这个目标?
例如,textview 有 maxline 并且使用 maxline 我们也可以在屏幕上显示文本而无需更改 xml 根目录。
【问题讨论】:
标签:
android
android-webview
percentage
【解决方案1】:
您可以像这样动态地将高度设置为RelativeLayout(Webview的父级)
int heightInDp=300;
float scale =getResources().getDisplayMetrics().density;
int heightInPx = (int) (heightInDp * scale + 0.5f);
RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,heightInPx);
rl_parent.setLayoutParams(layout);