2014-01-30 62 views
3

我想以编程方式获得可见布局的确切高度,如图所示。我用Sherlock片段活动作为包含不同片段的主要活动。我试图通过显示和显示矩阵来获取屏幕的高度和宽度,但它给出了整个屏幕的高度和宽度,但我只想得到儿童活动的可见视图。请帮帮我。提前致谢。Android以编程方式获取可见布局的高度

enter image description here

主要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(子活动的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> 

回答

1

对不起,我解决了我的问题,通过其他答案获得一些想法,但我看到一些建议解决它的不赞成使用的方法。

的动作条和TabBar高度

 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
(中的TabBar)
+0

我早些时候尝试过,但它给整个屏幕高度。顺便说一句'getHeight();'由于API 13 –

1

它会得到整个窗口

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

的高度现在采取采取动作条

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

减去动作条& tabWidget的高度,这可能是解决方案,也可以使用类似类型的逻辑..我不告诉这肯定VL工作。

+0

我已经解决了一段时间后弃用。感谢您的详细解释 –

相关问题