㈠ 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();