【问题标题】:Android Get height of the visible layout programmaticallyAndroid以编程方式获取可见布局的高度
【发布时间】:2014-02-22 15:20:14
【问题描述】:

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

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

【问题讨论】:

    标签: android xml android-fragments height tabbar


    【解决方案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();
    

    现在从窗口高度中减去 actionbar 和 tabWidget 的高度

    DesiredArea = DisplayHeight - (actionBarHeight + height);
    

    这可能是解决方案,或者您可以使用类似类型的逻辑.. 我不是说这个 vl 肯定有效。

    【讨论】:

    • 我在一段时间后解决了它。感谢您详细解释
    【解决方案2】:

    抱歉打扰,我解决了我的问题,通过其他答案获得了一些想法,但我看到了一些建议解决它的过时方法。

    对于actionbar和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
        }
    

    【讨论】:

      【解决方案3】:
      int height = getWindow().getWindowManager().getDefaultDisplay().getHeight();
      

      【讨论】:

      • 我之前尝试过,但它给出了整个屏幕的高度。顺便说一句'getHeight();'自 API 13 起已弃用
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      • 1970-01-01
      • 2016-11-14
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      • 2018-03-17
      相关资源
      最近更新 更多