Android Get height of the visible layout programma

2019-05-16 01:40发布

问题:

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. 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)

<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 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>

回答1:

It will get height of the whole window

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

Now take take height of ActionBar

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

let's take one tabHost & it's height for 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

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.



回答2:

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

     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
    }


回答3:

int height = getWindow().getWindowManager().getDefaultDisplay().getHeight();