简体   繁体   English

Android以编程方式获取可见布局的高度

[英]Android Get height of the visible layout programmatically

I want to get the exact height of the visible layout programmatically as I shown in figure. 我希望以编程方式获得可见布局的确切高度,如图所示。 I used Sherlock fragment activity as main activity which contain different fragments. 我使用Sherlock片段活动作为主要活动,其中包含不同的片段。 I tried to get the height and width of the screen through Display and display matrix, but it give whole screen height and width.but i want to get only visible view of the child activity. 我试图通过显示和显示矩阵获得屏幕的高度和宽度,但它给出了整个屏幕的高度和宽度。但我想只获得子活动的可见视图。 please help me. 请帮我。 thanks in advance. 提前致谢。

在此输入图像描述

Main XML (of Tabbar) 主要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>

child xml (Xml of child Activity) 子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>

It will get height of the whole window 它将获得整个窗口的高度

Display display = getWindowManager().getDefaultDisplay();
Displayheight = display.getHeight();

Now take take height of ActionBar 现在采取ActionBar的高度

ActionBar actionBar = getActionBar();
actionBarHeight = actionBar.getHeight();

let's take one tabHost & it's height for ex. 让我们拿一个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();

Now subtract Height of actionbar & tabWidget from Window Height 现在从Window Height中减去actionbar&tabWidget的高度

DesiredArea = DisplayHeight - (actionBarHeight + height);

this might be the solution or you can use similar type of logic.. i'm not telling this vl definately work. 这可能是解决方案或你可以使用类似的逻辑..我不是说这个vl肯定工作。

Sorry for Disturb , I solved my question for getting some ideas through other answer but i see some deprecated method which is suggested to solve it. 对不起,我解决了我的问题,通过其他答案得到一些想法,但我看到一些不赞成的方法,建议解决它。

for actionbar And tabbar height for actionbar和tabbar height

     int tabHeight = mTabHost.getTabWidget().getHeight();
     int actionbarheight = getSupportActionBar().getHeight();//getActionbar() insted of for Api greater 13 than 

height of statusbar 状态栏的高度

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

for getting screen size 获取屏幕尺寸

     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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM