[英]How to get programmatically width and height of Relative - Linear layout in android?
[英]Android Get height of the visible layout programmatically
我希望以编程方式获得可见布局的确切高度,如图所示。 我使用Sherlock片段活动作为主要活动,其中包含不同的片段。 我试图通过显示和显示矩阵获得屏幕的高度和宽度,但它给出了整个屏幕的高度和宽度。但我想只获得子活动的可见视图。 请帮我。 提前致谢。
主要XML (Tabbar)
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="@dimen/fr_layout_width"
android:layout_height="@dimen/fr_layout_height"
android:layout_weight="0" />
<FrameLayout
android:id="@+android:id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="@dimen/fr_layout_height"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="@dimen/hdpi_tab_layout_height"
android:layout_weight="0"
android:background="@android:color/transparent"
android:gravity="bottom"
android:orientation="horizontal" />
</LinearLayout>
</TabHost>
</android.support.v4.widget.DrawerLayout>
子xml (子活动的Xml)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainscroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/topliner"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
它将获得整个窗口的高度
Display display = getWindowManager().getDefaultDisplay();
Displayheight = display.getHeight();
现在采取ActionBar的高度
ActionBar actionBar = getActionBar();
actionBarHeight = actionBar.getHeight();
让我们拿一个tabHost&它的高度为ex。
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, vTab1.class);
spec = tabHost.newTabSpec("First").setIndicator("First").setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, vTab2.class);
spec = tabHost.newTabSpec("Second").setIndicator("Second").setContent(intent);tabHost.addTab(spec);
int height = tabHost.getTabWidget().getHeight();
现在从Window Height中减去actionbar&tabWidget的高度
DesiredArea = DisplayHeight - (actionBarHeight + height);
这可能是解决方案或你可以使用类似的逻辑..我不是说这个vl肯定工作。
对不起,我解决了我的问题,通过其他答案得到一些想法,但我看到一些不赞成的方法,建议解决它。
for actionbar和tabbar height
int tabHeight = mTabHost.getTabWidget().getHeight();
int actionbarheight = getSupportActionBar().getHeight();//getActionbar() insted of for Api greater 13 than
状态栏的高度
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
int statusbarheight = getStatusBarHeight();
获取屏幕尺寸
Display display = getWindowManager().getDefaultDisplay();
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR2)
{
width = display.getWidth(); // deprecated
height = display.getHeight()- (tabHeight + actionbarheight + statusbarheight );
}
else {
Point size1 = new Point();
display.getSize(size1);
width = size1.x;
height = size1.y - (tabHeight + actionbarheight + statusbarheight);//visible layout height
}
int height = getWindow().getWindowManager().getDefaultDisplay().getHeight();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.