【问题标题】:View widthXheight equals to the available space of the screen视图宽度X高度等于屏幕的可用空间
【发布时间】:2011-02-18 18:14:50
【问题描述】:

我正在创建一个layout,并在顶部对齐页眉,在底部对齐页脚,并希望 View 占据中间的所有其余可用空间。

如何在不将高度属性显式传递给每个视图的情况下做到这一点?

我的实际 XML 是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent" android:layout_height="match_parent"
     orientation="vertical">

     <ImageView 
         android:id="@+id/header" 
         android:layout_width="match_parent"
         android:layout_height="wrap_content" 
         android:src="@drawable/library_header"
         android:scaleType="fitXY" />

     <ViewFlipper 
         android:id="@+id/content"
         android:layout_height="0dip" 
         android:layout_width="match_parent"
         android:layout_weight="1.0" />

     <LinearLayout 
         android:layout_width="match_parent"
         android:layout_height="44px" 
         android:layout_alignParentBottom="true"
         android:background="@drawable/library_footer">

         <Button 
             android:id="@+id/l_coverflow" 
             android:layout_width="44px"
             android:layout_height="34px" 
             android:background="@drawable/library_coverflow_deselected"
             android:layout_gravity="center" android:layout_marginLeft="12px" />

         <Button 
              android:id="@+id/l_grid" 
              android:layout_width="36px"
              android:layout_height="32px" 
              android:background="@drawable/library_grid_deselected"
              android:layout_marginLeft="12px" 
              android:layout_gravity="center"
              android:paddingLeft="10px" />

         <Button 
              android:id="@+id/l_list"
              android:layout_width="40px"
              android:layout_height="32px" 
              android:background="@drawable/library_list_deselected"
              android:layout_marginLeft="12px" 
              android:layout_gravity="center" />
     </LinearLayout>
</LinearLayout>

【问题讨论】:

  • 你是用LinearLayout还是RelativeLayout作为容器?

标签: android android-layout layout android-linearlayout


【解决方案1】:

很简单(为了简洁,我省略了 android: 命名空间前缀):

<LinearLayout
  orientation="vertical"
  layout_width="fill_parent"
  layout_height="fill_parent">

  <!-- Header goes here with height=wrap_content -->

  <MiddleView
    layout_width="fill_parent"
    layout_height="0dip"
    layout_weight="1.0" />

  <!-- Footer goes here with height=wrap_content -->

</LinearLayout>

【讨论】:

  • 现在只有标题出现在我的屏幕上。
  • 它是 android:orientation="vertical",你忘了 android: 部分
猜你喜欢
  • 2013-01-02
  • 1970-01-01
  • 1970-01-01
  • 2013-11-17
  • 1970-01-01
  • 2012-11-01
  • 2011-09-09
  • 2014-12-21
  • 1970-01-01
相关资源
最近更新 更多