簡體   English   中英

Android以編程方式獲取可見布局的高度

[英]Android Get height of the visible layout programmatically

我希望以編程方式獲得可見布局的確切高度,如圖所示。 我使用Sherlock片段活動作為主要活動,其中包含不同的片段。 我試圖通過顯示和顯示矩陣獲得屏幕的高度和寬度,但它給出了整個屏幕的高度和寬度。但我想只獲得子活動的可見視圖。 請幫我。 提前致謝。

在此輸入圖像描述

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

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

它將獲得整個窗口的高度

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

現在采取ActionBar的高度

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

讓我們拿一個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();

現在從Window Height中減去actionbar&tabWidget的高度

DesiredArea = DisplayHeight - (actionBarHeight + height);

這可能是解決方案或你可以使用類似的邏輯..我不是說這個vl肯定工作。

對不起,我解決了我的問題,通過其他答案得到一些想法,但我看到一些不贊成的方法,建議解決它。

for actionbar和tabbar height

     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
    }
int height = getWindow().getWindowManager().getDefaultDisplay().getHeight();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM