Android编程:如何在程序中获取可见布局的高度?

3
我希望能够通过编程获取可见布局的准确高度,如图所示。 我使用了Sherlock片段活动作为主要活动,其中包含不同的片段。 我试图通过Display和display matrix来获取屏幕的高度和宽度,但它只提供整个屏幕的高度和宽度。 但我想要获取子活动的可见视图。 请帮帮我。 提前感谢您。 主XML(选项卡栏)
<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(子Activity的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>
3个回答

1
它将获取整个窗口的高度。
Display display = getWindowManager().getDefaultDisplay();
Displayheight = display.getHeight();

现在获取ActionBar的高度。
ActionBar actionBar = getActionBar();
actionBarHeight = actionBar.getHeight();

让我们以一个tabHost为例,看它的高度。

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

现在从窗口高度中减去操作栏和选项卡小部件的高度。
DesiredArea = DisplayHeight - (actionBarHeight + height);

这可能是解决方案,或者你可以使用类似的逻辑。我并不是说这一定会有效。


我花了一些时间解决了它。感谢您详细的解释。 - Mayur Raval

1

抱歉打扰了,我已经通过其他答案解决了我的问题,但是我看到了一些过时的方法建议用来解决它。

关于操作栏和选项卡高度

     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
    }

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

我之前尝试过,但它给出了整个屏幕的高度。顺便说一下,“getHeight();”自API 13以来已被弃用。 - Mayur Raval

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接