
如果在onCreate直接写
iLayout_main=(LinearLayout) thisfindViewById(RidiLayout_main);
int height = iLayout_maingetMeasuredHeight();
获取的高度永远是0,因此此时控件正在创建中所以获取不到正确的结果
但是如果这样写
iLayout_main=(LinearLayout) thisfindViewById(RidiLayout_main);
vtoaddOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int height = iLayout_maingetMeasuredHeight();
}
}
这用就可以 在创建的时候根据情况设置控件高度 宽度等属性了!
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 compimmosandroidsampleswebviewcontentwidth;
import androidappActivity;
import androidosBundle;
import androidutilLog;
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="compimmosandroidsampleswebviewcontentwidth"
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开发include获取内部控件代码:
sublayoutxml
<xml version="10" encoding="utf-8">
<LinearLayout xmlns:android=">
Android是一种基于Linux的自由及开放源代码的 *** 作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。Android *** 作系统最初由Andy Rubin开发,主要支持手机。
appium android获取元素方式:
1 1元素标签(tagname) 通过UI的控件类型ListtextFieldsList=driverfindElements(BytagName( textfield ));
2 元素的位置(xpath) 具有一定约束的路径抽象标示, 基于XPath方式;
3 元素的值(name) driverfindElement(Byname( Save ))click();通过元素的文本, 标签, 或者id标示;
4 元素的IDdriverfindElement(Byid(comexampleandroidcontactmanager:id/contactNameEditText ))getText();
我们可以通过使用类DisplayMetrics来获取手机屏幕的分辨率大小。DisplayMetrics类是获取手机屏幕各种属性的关键类。下面通过例子来展示如何获取手机屏幕的分辨率。
在布局文件mainxml中添加一个TextView对象,一个Button对象。其中TextView对象用来显示获得的分辨率值,Button对象是当点击时获取分辨率。mainxml的代码如下:
<xml version="10" encoding="utf-8">
<LinearLayout xmlns:android=">
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="手机分辨率为:"/>
<Button
android:id="@+id/btnOK"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="获取手机分辨率"/>
</LinearLayout>
在TestActivity中的代码如下:
public class TestActivity extends Activity {
private TextView tv;
private Button btn;
//获取手机屏幕分辨率的类
private DisplayMetrics dm;
public void onCreate(Bundle savedInstanceState) {
superonCreate(savedInstanceState);
setContentView(Rlayoutmain);
tv = (TextView)findViewById(Ridtv);
btn = (Button)findViewById(RidbtnOK);
btnsetOnClickListener(new ViewOnClickListener() {
public void onClick(View v) {
dm = new DisplayMetrics();
getWindowManager()getDefaultDisplay()getMetrics(dm);
//获得手机的宽度和高度像素单位为px
String strPM = "手机屏幕分辨率为:" + dmwidthPixels+" "+dmheightPixels;
tvsetText(strPM);
}
});
}
}
运行程序后,当我们点击Button按钮时,可以看到效果图:
获取 RecyclerView 的滑动距离:
滑动到一定程度后清零是因为 getChildAt(0) 获得的是第一个可见view 用LinearLayoutManager的recyclerview测试了一下确实如此。
如果LayoutManager用的是LinearLayoutManager 可以用下面的办法,还能向下滑动多少 @return / private int getDistance() { LinearLayoutManager layoutManager = (LinearLayoutManager) getLayoutManager(); View firstVisibleItem = thisgetChildAt(0); int firstItemPosition = layoutManagerfindFirstVisibleItemPosition(); int itemCount = layoutManagergetItemCount(); int recyclerviewHeight = thisgetHeight(); int itemHeight = firstVisibleItemgetHeight(); int firstItemBottom = layoutManagergetDecoratedBottom(firstVisibleItem); return (itemCount - firstItemPosition - 1) itemHeight - recyclerviewHeight + firstItemBottom; }
已滑动的距离:
private int getScrolledDistance() { LinearLayoutManager layoutManager = (LinearLayoutManager) getLayoutManager(); View firstVisibleItem = thisgetChildAt(0); int firstItemPosition = layoutManagerfindFirstVisibleItemPosition(); int itemHeight = firstVisibleItemgetHeight(); int firstItemBottom = layoutManagergetDecoratedBottom(firstVisibleItem); return (firstItemPosition + 1) itemHeight - firstItemBottom; }
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 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 Mainjava:
下面的代码是Activity的代码:
Mainjava
01 package compimmosandroidsampleswebviewcontentwidth;
02 import androidappActivity;
03 import androidosBundle;
04 import androidutilLog;
05 import androidwebkitWebView;
06 import androidwebkitWebViewClient;
07 import androidwidgetToast;
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 superonCreate(savedInstanceState);
18 setContentView(Rlayoutmain);
19 webview = (WebView) findViewById(Ridwebview);
20 webviewgetSettings()setJavaScriptEnabled(true);
21 webviewsetSaveEnabled(true);
22 webviewaddJavascriptInterface(new JavaScriptInterface(), "HTMLOUT");
23 webviewsetWebViewClient(new WebViewClient() {
24 @Override
25 public void onPageFinished(WebView view, String url) {
26 webviewloadUrl("javascript:windowHTMLOUTgetContentWidth(documentgetElementsByTagName(’html’)[0]scrollWidth);");
27 }//(">
解决方案:重写BaseAdapter,然后自行获取ListView中每行输入的EditText值
大概算法:重写BaseAdaptergetView函数,用一个数组存储EditText中的值,根据position即数组下标,在getView
中动态更新EditText和动态获取EditText中的值因为ListView中的item是复用的,如果不动态清空或动态获取EditText中
值,就会出现数据紊乱,或者没数据那么在生成EditText的时候要监控其值的变化存储下来
使用方法:
private List<Map<String, Object>> mCheckItemList = new ArrayList<Map<String, Object>>();
mAdapter = new ListEditorAdapter(this, mCheckItemList);
m_lvListViewsetAdapter(mAdapter);
直接改变mCheckItemList中的数据,然后调用mAdapternotifyDataSetChanged();就可以更新ListView的数据
当然,直接从mCheckItemList也可以获得用户在EditText中输入的数据比较方便
其他:
ListView list= (ListView)findViewById(Ridlist);//获得listview
for (int i = 0; i < listgetChildCount(); i++) {
LinearLayout layout = (LinearLayout)listgetChildAt(i);// 获得子item的layout
EditText et = (EditText) layoutfindViewById(Ridet);// 从layout中获得控件,根据其id
// EditText et = (EditText) layoutgetChildAt(1)//或者根据位置,在这我假设TextView在前,EditText在后
Systemoutprintln("the text of "+i+"'s EditText:----------->"+etgetText());
}
以上就是关于android怎么获取activity的高度全部的内容,包括:android怎么获取activity的高度、如何获取webView的高度、android开发 include时如何获取内部控件等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)