2014-01-30 158 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

我已經解決了一段時間後棄用。感謝您的詳細解釋 –

相關問題