android WebView設定最大高度
阿新 • • 發佈:2018-12-30
當我們在dialog中嵌入webview來顯示網頁資訊時,如果網頁內容足夠長,則會出現dialog高度被撐滿屏,但是介於美觀問題,我們會試圖動態設定webview的最大高度,可是遺憾的是,谷歌並沒有給我們提供這個方法,聰明人想出了聰明的辦法,具體請看下面程式碼:
結果,世界和平了public class MyWebView extends WebView { private int mMaxHeight = -1; public MyWebView(Context context) { super(context); // TODO Auto-generated constructor stub } public MyWebView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public MyWebView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public MyWebView(Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) { super(context, attrs, defStyle, privateBrowsing); // TODO Auto-generated constructor stub } public void setMaxHeight(int height){ mMaxHeight = height; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub super.onMeasure(widthMeasureSpec, heightMeasureSpec); if(mMaxHeight>-1 && getMeasuredHeight()>mMaxHeight){ setMeasuredDimension(getMeasuredWidth(), mMaxHeight); } } }
友情提示:設定最大高度一定要在載入內容之前!!!
轉自:http://blog.csdn.net/handerhuo/article/details/48003625