導航:首頁 > 操作系統 > android得到寬度

android得到寬度

發布時間:2022-05-29 01:18:19

android怎麼獲取屏幕寬度

mScreenWidth = context.getResources().getDisplayMetrics().widthPixels; //得到屏幕寬度

㈡ android textview怎麼准確的獲取字元寬度

android取得textview中每個字元的寬度方法如下:
//方法入口
public float getCharacterWidth(TextView tv){
if(null == tv) return 0f;
return getCharacterWidth(tv.getText().toString(),tv.getTextSize()) * tv.getScaleX();
}

//獲取每個字元的寬度主方法:
public float getCharacterWidth(String text, float size){
if(null == text || "".equals(text))
return 0;
float width = 0;
Paint paint = new Paint();
paint.setTextSize(size);
float text_width = paint.measureText(text);//得到總體長度
width = text_width/text.length();//每一個字元的長度
return width;
}

㈢ android 代碼里怎麼設置控制項的寬度

在對應的控制項中使用android:layout_width標簽即可。

android:layout_width標簽中可以使用match_parent常量使控制項尺寸與其上級組件尺寸相同

可以使用wrap_content使控制項尺寸剛好包裹住內容

也可以使用px(像素)、pt(磅)、dp(密度)、sp(可伸縮像素)作為單位,從而設置控制項的寬度:

例如:

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one"
android:layout_gravity="right"/>

㈣ 如何獲取android模擬器的高度和寬度

// 獲取屏幕寬高(方法1)
int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕寬(像素,如:480px)
int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); // 屏幕高(像素,如:800p)
Log.e(TAG + " getDefaultDisplay", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
// 獲取屏幕密度(方法2)
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
float density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
int densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:120/160/240/320)
float xdpi = dm.xdpi;
float ydpi = dm.ydpi;
Log.e(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi);
Log.e(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI);
screenWidth = dm.widthPixels; // 屏幕寬(像素,如:480px)
screenHeight = dm.heightPixels; // 屏幕高(像素,如:800px)
Log.e(TAG + " DisplayMetrics(111)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
// 獲取屏幕密度(方法3)
dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:120/160/240/320)
xdpi = dm.xdpi;
ydpi = dm.ydpi;
Log.e(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi);
Log.e(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI);
int screenWidthDip = dm.widthPixels; // 屏幕寬(dip,如:320dip)
int screenHeightDip = dm.heightPixels; // 屏幕寬(dip,如:533dip)
Log.e(TAG + " DisplayMetrics(222)", "screenWidthDip=" + screenWidthDip + "; screenHeightDip=" + screenHeightDip);
screenWidth = (int)(dm.widthPixels * density + 0.5f); // 屏幕寬(px,如:480px)
screenHeight = (int)(dm.heightPixels * density + 0.5f); // 屏幕高(px,如:800px)
Log.e(TAG + " DisplayMetrics(222)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);

㈤ 如何在html中獲得android手機中瀏覽器的屏幕的寬度

在html中獲得android手機中瀏覽器的屏幕寬度的方法:
1、在網頁的<head>中增加以上這句話,可以讓網頁的寬度自動適應手機屏幕的寬度:

[html] view plain
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="format-detection" content="telephone=no" />
第一行:
width=device-width :表示寬度是設備屏幕的寬度
initial-scale=1.0:表示初始的縮放比例
minimum-scale=0.5:表示最小的縮放比例
maximum-scale=2.0:表示最大的縮放比例
user-scalable=yes:表示用戶是否可以調整縮放比例
第二行:
設定iphone端頁面全屏。
第三行:
取消數字被識別為電話號碼。
2、如果是想要一打開網頁,則自動以原始比例顯示,並且不允許用戶修改的話,則是:

[html] view plain
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
3、這樣可以把一些頁頭橫幅等的圖片的寬度都設置成style="width:100%",整個頁面在設備上看起來就是全屏的了。

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

Android開發時,從WebView,我不但想要知道ContentHeight,還想知道ContentWidth。不幸的是,從一個 WebView獲取contentWidth是相當困難,因為SDK中沒有一個像這樣的方法,所以本文為大家呈現了一種實用的解決此問題的方法。

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,如下:

webview.getContentHeight();
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!
webview.getContentWidth();
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 doesn't affect widths (most likely only heights will be affected as the width is almost always fully declared in CSS files unless you have a 100% width webpage).
搭建一個快速的例子:創建一個簡單的展示webView的Activity,一個LogCat消息,一個Toast消息,用來顯示我們通過 Javascript獲取的寬度。注意:這些會在網頁完全載入之後顯示,因為在網頁載入完成之前,寬度可能不能夠正確的獲取到。同時也要注意到,如果是異 步載入,這並不影響寬度(最多高度會受影響,因為寬度總是在CSS文件中做了完全的定義,除非在網頁中你用了100%寬度。)。
Below is the code of the Activity Main.java:
下面的代碼是Activity的代碼:

01 package com.pimmos.android.samples.webviewcontentwidth;
02 import android.app.Activity;
03 import android.os.Bundle;
04 import android.util.Log;
05 import android.webkit.WebView;
06 import android.webkit.WebViewClient;
07 import android.widget.Toast;
08 public class Main extends Activity {
09 private final static String LOG_TAG = "WebViewContentWidth";
10 private final Activity activity = this;
11 private static int webviewContentWidth = 0;
12 private static WebView webview;
13
14 /** Called when the activity is first created. */
15 @Override
16 public void onCreate(Bundle savedInstanceState) {
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.main);
19 webview = (WebView) findViewById(R.id.webview);
20 webview.getSettings().setJavaScriptEnabled(true);
21 webview.setSaveEnabled(true);
22 webview.addJavascriptInterface(new JavaScriptInterface(), "HTMLOUT");
23 webview.setWebViewClient(new WebViewClient() {
24 @Override
25 public void onPageFinished(WebView view, String url) {
26 webview.loadUrl("javascript:window.HTMLOUT.getContentWidth(document.getElementsByTagName('html')[0].scrollWidth);");
27 }
28 });
29 webview.loadUrl("http://www.pimmos.com/");
30 }
31
32 class JavaScriptInterface {
33 public void getContentWidth(String value) {
34 if (value != null) {
35 webviewContentWidth = Integer.parseInt(value);
36 Log.d(LOG_TAG, "Result from javascript: " + webviewContentWidth);
37 Toast.makeText( activity,
38 "ContentWidth of webpage is: " +
39 webviewContentWidth +
40 "px", Toast.LENGTH_SHORT).show();
41 }
42 }
43 }
44 }
Below is the XML layout used with the Activity wich only contains a simple WebView:
下面是Activity的Layout,主要就是一個簡單的WebView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/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>
AndroidManifest.xml layout:
AndroidManifest.xml代碼:

<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pimmos.android.samples.webviewcontentwidth"
android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
You can also download the full source of Android Application - WebViewContentWidth!

㈦ 怎樣獲取Android手機屏幕的大小

要獲取手機屏幕大小,其實太簡單,只要用尺量取屏幕對角尺寸(單位:mm)。然後用這個尺寸除以25.4,就得到英寸了。譬如:127mm/25.4=5.0 英寸。

㈧ android如何獲取控制項寬度

用getWidth()方法,可以獲取像素單位的寬度。

android的控制項一般是繼承的android.View這個類,所以可以直接用View#getWidth()方法獲取控制項寬度。另外這個方法是final方法,無法被子類覆蓋,所以可以安心調用

㈨ Android開發 怎樣獲取屏幕的寬高是多少厘米

我們需要獲取Android手機或Pad的屏幕的物理尺寸,以便於界面的設計或是其他功能的實現。下面就介紹講一講如何獲取屏幕的物理尺寸
下面的代碼即可獲取屏幕的尺寸。
在一個Activity的onCreate方法中,寫入如下代碼:

[java] view plain print?
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
int width = metric.widthPixels; // 屏幕寬度(像素)
int height = metric.heightPixels; // 屏幕高度(像素)
float density = metric.density; // 屏幕密度(0.75 / 1.0 / 1.5)
int densityDpi = metric.densityDpi; // 屏幕密度DPI(120 / 160 / 240)

DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
int width = metric.widthPixels; // 屏幕寬度(像素)
int height = metric.heightPixels; // 屏幕高度(像素)
float density = metric.density; // 屏幕密度(0.75 / 1.0 / 1.5)
int densityDpi = metric.densityDpi; // 屏幕密度DPI(120 / 160 / 240)

但是,需要注意的是,在一個低密度的小屏手機上,僅靠上面的代碼是不能獲取正確的尺寸的。比如說,一部240x320像素的低密度手機,如果運行上述代碼,獲取到的屏幕尺寸是320x427。因此,研究之後發現,若沒有設定多解析度支持的話,Android系統會將240x320的低密度(120)尺寸轉換為中等密度(160)對應的尺寸,這樣的話就大大影響了程序的編碼。所以,需要在工程的AndroidManifest.xml文件中,加入supports-screens節點,具體的內容如下:
[html] view plain print?
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:resizeable="true"
android:anyDensity="true" />

<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:resizeable="true"
android:anyDensity="true" /> 這樣的話,當前的Android程序就支持了多種解析度,那麼就可以得到正確的物理尺寸了。

[java] view plain print?
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.TextView;
public class TextCanvasActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(new MyView(this));

//定義DisplayMetrics 對象
setContentView(R.layout.main);
DisplayMetrics dm = new DisplayMetrics();
//取得窗口屬性
getWindowManager().getDefaultDisplay().getMetrics(dm);

//窗口的寬度
int screenWidth = dm.widthPixels;

//窗口高度
int screenHeight = dm.heightPixels;
TextView textView = (TextView)findViewById(R.id.tv1);
textView.setText("屏幕寬度: " + screenWidth + "\n屏幕高度: " + screenHeight);
}
}

㈩ android如何獲取屏幕寬度

現在獲取屏幕寬度一般都是像素。
可以用以下代碼:
WindowManager wm = (WindowManager) getContext()
.getSystemService(Context.WINDOW_SERVICE);

int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
或者是:
WindowManager wm = this.getWindowManager();

int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();

閱讀全文

與android得到寬度相關的資料

熱點內容
自己購買雲主伺服器推薦 瀏覽:422
個人所得稅java 瀏覽:761
多餘的伺服器滑道還有什麼用 瀏覽:192
pdf劈開合並 瀏覽:29
不能修改的pdf 瀏覽:752
同城公眾源碼 瀏覽:489
一個伺服器2個埠怎麼映射 瀏覽:298
java字元串ascii碼 瀏覽:79
台灣雲伺服器怎麼租伺服器 瀏覽:475
旅遊手機網站源碼 瀏覽:332
android關聯表 瀏覽:946
安卓導航無聲音怎麼維修 瀏覽:333
app怎麼裝視頻 瀏覽:431
安卓系統下的軟體怎麼移到桌面 瀏覽:96
windows拷貝到linux 瀏覽:772
mdr軟體解壓和別人不一樣 瀏覽:904
單片機串列通信有什麼好處 瀏覽:340
游戲開發程序員書籍 瀏覽:860
pdf中圖片修改 瀏覽:288
匯編編譯後 瀏覽:491