當前位置:首頁 » 安卓系統 » android取view高度

android取view高度

發布時間: 2022-11-26 11:31:47

㈠ android view怎麼獲取高度

在Android開發過程中,有時需要獲取View繪制前的高度或者寬度,一種的可能情形是初始化的時候讓某個View的Visible = Gone的,當觸發某個事件的時候需要它顯示並且希望有一些動畫效果。
這時候就要獲取這個View顯示前即繪制前的寬度或者高度。原理很簡單,View的繪制過程發生之前,會先執行onMeasure方法。那麼就可以利用反射來獲取需要的值。下面給出獲取高度的代碼,寬度同理。
private int getTargetHeight(View v) {
try {
Method m = v.getClass().getDeclaredMethod("onMeasure", int.class,
int.class);
m.setAccessible(true);
m.invoke(v, MeasureSpec.makeMeasureSpec(
((View) v.getParent()).getMeasuredWidth(),
MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED));
} catch (Exception e) {

}
return v.getMeasuredHeight();
}

拿到這個高度之後就可以做想做的動畫效果或者是其他的事情了。

㈡ android 自定義view的高度設置問題

GridView mGrid= (GridView) findViewById(R.id.gridview);
LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) mGrid.getLayoutParams(); // 取控制項mGrid當前的布局參數
linearParams.height = 75;// 當控制項的高強制設成75象素
mGrid.setLayoutParams(linearParams); // 使設置好的布局參數應用到控制項mGrid2

㈢ Android View何時能拿到寬高的值

一個View或ViewGroup中什麼什麼時候能拿到寬高的值?

width 表示 View 在屏幕上可顯示的區域大小;
measuredWidth 表示 View 的實際大小,包括超出屏幕范圍外的尺寸;

甚至有這樣的公式總結到:
getMeasuredWidth() = visible width + invisible width

getMeasuredWidth() 在執行setMeasuredDimension(一般在onMeasure方法中執行)後才有值;
getWidth()在onLayout方法執行後才有值。



Constructor : 構造方法,View初始化的時候調用,在這里是無法獲取其子控制項的引用的.更加無法獲取寬高了.

onFinishInflate : 當布局初始化完畢後回調,在這里可以獲取所有直接子View的引用,但是無法獲取寬高.

onMeasure : 當測量控制項寬高時回調,當調用了requestLayout()也會回調onMeasure.在這里一定可以通過getMeasuredHeight()和getMeasuredWidth()來獲取控制項的高和寬,但不一定可以通過getHeight()和getWidth()來獲取控制項寬高,因為getHeight()和getWidth()必須要等onLayout方法回調之後才能確定.

onSizeChanged : 當控制項的寬高發生變化時回調,和onMeasure一樣,一定可以通過getMeasuredHeight()和getMeasuredWidth()來獲取控制項的高和寬,因為它是在onMeasure方法執行之後和onLayout方法之前回調的.

onLayout : 當確定控制項的位置時回調,當調用了requestLayout()也會回調onLayout.在這里一定可以通過getHeight()和getWidth()獲取控制項的寬高,同時由於onMeasure方法比onLayout方法先執行,所以在這里也可以通過getMeasuredHeight()和getMeasuredWidth()來獲取控制項的高和寬.

addOnGlobalLayoutListener : 當View的位置確定完後會回調改監聽方法,它是緊接著onLayout方法執行而執行的,只要onLayout方法調用了,那麼addOnGlobalLayoutListener的監聽器就會監聽到.在這里getMeasuredHeight()和getMeasuredWidth()和getHeight()和getWidth()都可以獲取到寬高.

onWindowFocusChanged : 當View的焦點發送改變時回調,在這里getMeasuredHeight()和getMeasuredWidth()和getHeight()和getWidth()都可以獲取到寬高.Activity也可以通過重寫該方法來判斷當前的焦點是否發送改變了;需要注意的是這里View獲取焦點和失去焦點都會回調.



(部分內容參考於網路,如有不妥,請聯系刪除~)

㈣ android中怎麼獲取一個視屏在view中的有效高寬。

1.
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
2.
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);//this指當前activity
screenWidth =dm.widthPixels;
screenHeight =dm.heightPixels;
以上兩種方法在屏幕未顯示的時候,還是處於0的狀態,即要在setContentView調用之後才有效。
3.
還可以在onDraw中由canvas來獲得
screenWidth =canvas.getWidth();
screenHeight =canvas.getHeight();

㈤ android自定義view如何獲取父容器賜予的寬度和高度

自定義View,想要自定義給定寬和高,你要寫自定義屬性,然後在xml文件中指定寬高才會有效,同時當給定的寬和高的值是wrap_content 或 fill_parent 這類的,這時需要在自定義View中重寫onMeasure方法,進行控制項的寬高測量。

㈥ android webview 高度如何計算的

1. 通過webview的didFinishLoad:方法,這個時候webview已經載入完成,可以獲得真實高度。
2. 通過js來獲取實際頁面的高度,獲取的時機也通過js來監聽。
3. 通過檢測webView.scrollView.contentSize,只要這個值發生改變,你馬上就能夠被通知。注冊觀察者的代碼類似這樣:
[webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew
context:&webViewContext]

然後在觀察者中實現- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context,你就隨時可以監測到webView的高度變化了

㈦ Android開發中怎樣獲取WebView的內容寬度高度

The extensive Android SDK allows you to do many great things with particular views like the WebView for displaying webpages on Android powered devices
Android SDK 的擴展通過使用特定的view允許你做許多事情比如WebView用來在Android手機上展示網頁
As of lately while I was experimenting with the Android SDK I was using a WebView in one of my activities
最近我在體驗Android SDK的時候在一個Activity中用到了WebView
From that particular WebView I needed to know the ContentHeight but also the ContentWidth
從WebView我不但想要知道ContentHeight還想知道ContentWidth
Now getting the contentHeight is easy like so:
現在的情況是獲取contentHeight很easy如下
webviewgetContentHeight();
Unfortunately getting the contentWidth from a WebView is rather more difficult since there is not a simple method like:
不幸的是從一個WebView獲取contentWidth是相當困難因為SDK中沒有一個像這樣的方法
// THIS METHOD DOES NOT EXIST!
webviewgetContentWidth();
There are ways to get the contentWidth of the rendered HTML page and that is through javascript If Javascript can get it for you then you can also have them in your Java code within your Android App
當然是有方法獲取contentWidth的就是通過Javascript來獲取如果你能夠支持Javascript那麼你就可以在你的Android 程序中使用java代碼來獲取寬度
By using a JavascriptInterface with your WebView you can let Javascript communicate with your Android App Java code by invoking methods on a registered object that you can embed using the JavascriptInterface
通過在你的WebView中使用JavascriptInterface通過調用你注冊的JavascriptInterface方法可以讓Javascript和你的Android程序的java代碼相互連通
So how does this work?
怎麼做呢?
For a quick example I created a simple Activity displaying a webview that loads a webpage wich displays a log message and a Toast message with the contentWidth wich was determined using Javascript Note that this happens AFTER the page was finished loading because before the page is finished loading the width might not be fully rendered Also keep in mind that if there is content loaded asynchronously that it doesnt affect widths (most likely only heights will be affected as the width is almost always fully declared in CSS files unless you have a % width webpage)
搭建一個快速的例子創建一個簡單的展示webView的Activity一個LogCat消息一個Toast消息用來顯示我們通過 Javascript獲取的寬度注意這些會在網頁完全載入之後顯示因為在網頁載入完成之前寬度可能不能夠正確的獲取到同時也要注意到如果是異 步載入這並不影響寬度(最多高度會受影響因為寬度總是在CSS文件中做了完全的定義除非在網頁中你用了%寬度)
Below is the code of the Activity Mainjava:
下面的代碼是Activity的代碼
package ;
import androidappActivity;
import androidosBundle;
import androitilLog;
import androidwebkitWebView;
import androidwebkitWebViewClient;
import androidwidgetToast;
publicclass Main extends Activity {
privatefinalstatic String LOG_TAG = "WebViewContentWidth";
privatefinal Activity activity = this;
privatestaticint webviewContentWidth = ;
privatestatic WebView webview;

/** Called when the activity is first created */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
superonCreate(savedInstanceState);
setContentView(Rlayoutmain);
webview = (WebView) findViewById(Ridwebview);
webviewgetSettings()setJavaScriptEnabled(true);
webviewsetSaveEnabled(true);
webviewaddJavascriptInterface(new JavaScriptInterface() "HTMLOUT");
webviewsetWebViewClient(new WebViewClient() {
@Override
publicvoid onPageFinished(WebView view String url) {
webviewloadUrl("javascript:windowHTMLOUTgetContentWidth(documentgetElementsByTagName(html)[]scrollWidth);");
}
});
webviewloadUrl(";);
}

class JavaScriptInterface {
publicvoid getContentWidth(String value) {
if (value != null) {
webviewContentWidth = IntegerparseInt(value);
Logd(LOG_TAG "Result from javascript: " + webviewContentWidth);
ToastmakeText( activity
"ContentWidth of webpage is: " +
webviewContentWidth +
"px" ToastLENGTH_SHORT)show();
}
}
}
}
Below is the XML layout used with the Activity wich only contains a simple WebView:
下面是Activity的Layout主要就是一個簡單的WebView
<?xml version="" encoding="utf"?>
<LinearLayout
xmlns:android=";
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
AndroidManifestxml layout:
AndroidManifestxml代碼
<?xml version="" encoding="utf"?>
<manifest
xmlns:android=";
package=""
android:versionCode="" android:versionName="">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name="Main"
android:label="@string/app_name">
<intentfilter>
<action android:name="androidintentactionMAIN" />
<category
android:name="androidintentcategoryLAUNCHER" />
</intentfilter>
</activity>
</application>
<usessdk android:minSdkVersion="" />
<usespermission android:name="androidpermissionINTERNET" />
</manifest>
You can also download the full source of Android Application WebViewContentWidth!

㈧ android中如何計算scrollview的高度

安卓中的ScrollView組件只允許一個子View,可以利用這一個特性,獲取子View的高度即所要的ScrollView的整體高度, 方法如下:
scrollView.getChildAt(0).getHeight();
當然很多問題也可以在CSDN論壇中找到答案哈~

㈨ Android開發中怎樣獲取WebView的內容寬度高度

可以根據webview的getwidth和getHeight的寬度和高度,來獲取顯示的高度和寬度,要注意的是需要等待界面載入完才能獲取到。

㈩ 如何正確的獲得一個view的寬和高

我們都知道在onCreate()裡面獲取控制項的高度是0,這是為什麼呢?我們來看一下示例:
首先我們自己寫一個控制項,這個控制項非常簡單:



public class MyImageView extends ImageView {
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyImageView(Context context) {
super(context);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
System.out.println("onMeasure 我被調用了"+System.currentTimeMillis());
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
System.out.println("onDraw 我被調用了"+System.currentTimeMillis());
}
}

布局文件:

<com.test.MyImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/test" />

測試的Activity的onCreate():


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println("執行完畢.."+System.currentTimeMillis());
}

現在我們現在來看一下結果:

說明等onCreate方法執行完了,我們定義的控制項才會被度量(measure),所以我們在onCreate方法裡面通過view.getHeight()獲取控制項的高度或者寬度肯定是0,因為它自己還沒有被度量,也就是說他自己都不知道自己有多高,而你這時候去獲取它的尺寸,肯定是不行的.
有如下兩種方法可以解決這個問題:
----------------------------------

方法一:使用view的measure方法。
------------------------------

優點:可以立即獲得寬和高

缺點:人為的多了一次測量過程

這種方法適用於需要在onCreate完成之前就獲得一個view的寬和高的情況。
比如獲得一個LinearLayout寬和高


//寬
public int getViewWidth(LinearLayout view){
view.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
return view.getMeasuredWidth();
}
//高
public int getViewHeight(LinearLayout view){
view.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
return view.getMeasuredHeight();
}

這種方法的原理是直接調用一個view或者viewgroup的measure方法去測量,測量之後該view的getMeasuredHeight()就會返回剛才測量所得的高,getMeasuredWidth返回測量所得寬。本來在布局載入的過程中,view的measure方法一定會被系統調用,但這發生在我們所不知道的某個時間點,為了在這之前提前得到測量結果,我們主動調用measure方法,但是這樣做的好處是可以立即獲得寬和高,壞處是多了一次測量過程。
至於為什麼參數是LayoutParams.WRAP_CONTENT,那是因為我假設這個view的layout_width和layout_height為wrap_content,因為如果為一個確切的值,還有必要測量嗎?
-------------------------------------------------------------------
方法二:布局監聽類ViewTreeObserver的OnGlobalLayoutListener
-------------------------------------------------------------

當一個view的布局載入完成或者布局發生改變時OnGlobalLayoutListener可以監聽到,利用這點我們可以在布局載入完成的瞬間獲得一個view的寬高。

int mHeaderViewHeight;
mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {

mHeaderViewHeight = mHeaderView.getHeight();
getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
}
});

這種方法無法像第一種方法那樣通過一個函數返回值,因為他是基於listener的,OnGlobalLayoutListener的onGlobalLayout被回調之前是沒有值的。由於布局狀態可能會發生多次改變,因此OnGlobalLayoutListener的onGlobalLayout可能被回調多次,所以我們在 第一次獲得值之後就將listener注銷掉。

熱點內容
文件夾側邊條 發布:2025-03-10 12:50:22 瀏覽:383
液化天然氣存儲 發布:2025-03-10 12:47:28 瀏覽:717
壓縮機介質端 發布:2025-03-10 12:46:00 瀏覽:19
linux的測試 發布:2025-03-10 12:45:10 瀏覽:277
黑客說編程 發布:2025-03-10 12:23:57 瀏覽:841
伺服器無法登錄什麼時候才能登錄 發布:2025-03-10 12:17:20 瀏覽:888
微雲密碼是什麼 發布:2025-03-10 12:13:26 瀏覽:86
android上拉下拉 發布:2025-03-10 12:07:05 瀏覽:915
緩存大好 發布:2025-03-10 11:55:42 瀏覽:838
搭建java 發布:2025-03-10 11:47:00 瀏覽:524