【问题标题】:Android: can height of SlidingDrawer be set with wrap_content?Android:SlidingDrawer的高度可以用wrap_content设置吗?
【发布时间】:2010-09-06 21:53:29
【问题描述】:

我正在尝试实现一个SlidingDrawer,它将占据整个屏幕宽度,但其高度由其内容动态确定:换句话说,标准fill_parent 的宽度和wrap_content 的布局行为高度。这正是我在布局 XML 中指定的方式(见下文),但滑动抽屉始终打开到全屏高度。我的内容的高度各不相同,但通常只有屏幕高度的一半左右,所以我最终会在它下面留下一个很大的空隙。我想要的是将内容整齐地放在屏幕底部。

我已经尝试了我能想到的一切来修复它,但到目前为止没有任何效果。如果我将SlidingDrawer 的layout_height 设置为特定值(例如160dip),它可以工作,但这不是我需要的:它必须是动态的。当然,我确保所有子元素的高度也设置为wrap_content。

SlidingDrawer 上的文档对此有点含糊,我也找不到任何符合我要求的示例。如果有人能看出我哪里出错了,我真的很感谢你的帮助!

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ViewFlipper
        android:id="@+id/ImageFlipper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <ImageView
            android:id="@+id/imageView0"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="centerCrop" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="centerCrop" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="centerCrop" />

    </ViewFlipper>

    <SlidingDrawer
        android:id="@+id/infoDrawer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:handle="@+id/infoDrawerHandle"
        android:content="@+id/infoDrawerContent"
        android:allowSingleTap="false"
        android:layout_alignParentBottom="true"
        android:orientation="vertical" >

        <!-- Sliding drawer handle -->
        <ImageView
            android:id="@id/infoDrawerHandle"
            android:src="@drawable/info_handle_closed"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" />

        <!-- Sliding drawer content: a scroller containing a group of text views
        laid out in a LinearLayout -->
        <ScrollView
            android:id="@id/infoDrawerContent"
            android:background="@drawable/info_background"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fillViewport="false" >

            <LinearLayout
                android:id="@id/infoDrawerContent"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="5dip" >

                <TextView
                    android:id="@+id/infoTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#ffffff"
                    android:textSize="16dip"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/infoCreator"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#ffffff"
                    android:textSize="14dip"
                    android:textStyle="italic"
                    android:paddingBottom="10dip" />

                <TextView
                    android:id="@+id/infoDescription"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#ffffff"
                    android:textSize="14dip"
                    android:paddingBottom="10dip" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#ffcc00"
                    android:textSize="14dip"
                    android:textStyle="bold"
                    android:text="@string/heading_pro_tip" />

                <TextView
                    android:id="@+id/infoProTip"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#ffcc00"
                    android:textSize="14dip" />

            </LinearLayout>    

        </ScrollView>

    </SlidingDrawer>

</RelativeLayout>

【问题讨论】:

    标签: android layout slidingdrawer


    【解决方案1】:

    SlidingDrawer 类的onMeasure() 方法基本上将布局模式覆盖为fill_parent,这就是layout_height="wrap_content" 不起作用的原因。

    要解决这个问题,您可以使用重新实现的 onMeasure() 方法扩展 SlidingDrawer,该方法支持 layout_width 和 layout_height 属性。然后,您可以通过将 &lt;SlidingDrawer ...&gt; 替换为 &lt;fully.qualified.package.ClassName ...&gt;,在 XML 布局中使用此自定义类。

    请注意,由于抽屉将不再填充父布局,因此您必须将其封闭在 LinearLayout 中,并将重力属性设置为抽屉应位于的边缘。

    下面是我为此目的创建的一个类和一个示例布局。

    WrappingSlidingDrawer 类:

    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.View;
    import android.widget.SlidingDrawer;
    
    
    public class WrappingSlidingDrawer extends SlidingDrawer {
    
        public WrappingSlidingDrawer(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
    
            int orientation = attrs.getAttributeIntValue("android", "orientation", ORIENTATION_VERTICAL);
            mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0);
            mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL);
        }
    
        public WrappingSlidingDrawer(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            int orientation = attrs.getAttributeIntValue("android", "orientation", ORIENTATION_VERTICAL);
            mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0);
            mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
            int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
            int widthSpecSize =  MeasureSpec.getSize(widthMeasureSpec);
    
            int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
            int heightSpecSize =  MeasureSpec.getSize(heightMeasureSpec);
    
            if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
                throw new RuntimeException("SlidingDrawer cannot have UNSPECIFIED dimensions");
            }
    
            final View handle = getHandle();
            final View content = getContent();
            measureChild(handle, widthMeasureSpec, heightMeasureSpec);
    
            if (mVertical) {
                int height = heightSpecSize - handle.getMeasuredHeight() - mTopOffset;
                content.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, heightSpecMode));
                heightSpecSize = handle.getMeasuredHeight() + mTopOffset + content.getMeasuredHeight();
                widthSpecSize = content.getMeasuredWidth();
                if (handle.getMeasuredWidth() > widthSpecSize) widthSpecSize = handle.getMeasuredWidth();
            }
            else {
                int width = widthSpecSize - handle.getMeasuredWidth() - mTopOffset;
                getContent().measure(MeasureSpec.makeMeasureSpec(width, widthSpecMode), heightMeasureSpec);
                widthSpecSize = handle.getMeasuredWidth() + mTopOffset + content.getMeasuredWidth();
                heightSpecSize = content.getMeasuredHeight();
                if (handle.getMeasuredHeight() > heightSpecSize) heightSpecSize = handle.getMeasuredHeight();
            }
    
            setMeasuredDimension(widthSpecSize, heightSpecSize);
        }
    
        private boolean mVertical;
        private int mTopOffset;
    }
    

    示例布局(假设 WrappingSlidingDrawer 在包 com.package 中):

    <FrameLayout android:layout_width="fill_parent"
                 android:layout_height="fill_parent">
        ... stuff you want to cover at full-size ...
        <LinearLayout android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:gravity="bottom"
                  android:orientation="vertical">
            <com.package.WrappingSlidingDrawer android:layout_width="fill_parent"
                               android:layout_height="wrap_content"
                               android:content="@+id/content"
                               android:handle="@+id/handle">
                ... handle and content views ...
            </com.package.WrappingSlidingDrawer>
        </LinearLayout>
    </FrameLayout>
    

    【讨论】:

    • 好东西!我为这个滑动抽屉问题苦苦挣扎了几个星期。非常感谢!
    • 太棒了:感谢您提供如此详细的答案,很抱歉我花了这么长时间才接受它!
    • 非常感谢您。如果可以的话,我会给你 +100。
    • 这很好,但是,我如何对齐某些东西以填充屏幕直到手柄?它现在的工作方式,线性布局是填充宽度和高度,所以我不能在屏幕上放置其他任何东西......有什么提示吗?我想要一个覆盖整个屏幕的东西,然后在它的底部,我想要一个只打开它需要显示的内容的句柄......换句话说 - 我想要将元素对齐到句柄上方 - 可以使用这种方法吗?
    • 请也阅读我的回答:这个解决方案有一个小问题。另请参阅 museofwater 的答案
    【解决方案2】:

    只需在您的 xml 中的滑动抽屉中设置为 pmargin

    android:layout_marginTop="50dip"
    

    【讨论】:

      【解决方案3】:

      seydhe 的回答有一个小问题。

      getAttributeIntValue 的第一个参数必须是完整的命名空间,而不仅仅是“android”。所以代码sn-p应该是:

      final String xmlns="http://schemas.android.com/apk/res/android";
      int orientation = attrs.getAttributeIntValue(xmlns, "orientation", SlidingDrawer.ORIENTATION_VERTICAL);
       mTopOffset = attrs.getAttributeIntValue(xmlns, "topOffset", 0);
      

      我无法让它与水平滑动抽屉一起使用,直到我意识到它没有找到方向属性并因此将其视为垂直。

      【讨论】:

      • 这不应该是一个答案。建议修改。
      【解决方案4】:

      最好在不硬编码字符串的情况下读取参数:

          int attrOrientation = android.R.attr.orientation;
          int attrTopOffset = android.R.attr.topOffset;
      
          int[] attrIds = new int [] {attrOrientation, attrTopOffset}; 
      
          TypedArray a = context.obtainStyledAttributes(attrs, attrIds);
          int orientation = a.getInt(0, SlidingDrawer.ORIENTATION_VERTICAL);
          topOffset = a.getDimension(1, 0);
          a.recycle(); 
      
          isVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL);
      

      另一个问题在 onMeasure 中。

      我使用了以下代码:

          if (isVertical) {
              int height = heightSpecSize - handle.getMeasuredHeight() - topOffset;
              getContent().measure(MeasureSpec.makeMeasureSpec(widthSpecSize, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
              heightSpecSize = handle.getMeasuredHeight() + topOffset + content.getMeasuredHeight();
              widthSpecSize = content.getMeasuredWidth();
              if (handle.getMeasuredWidth() > widthSpecSize) widthSpecSize = handle.getMeasuredWidth();
          } else {
              int width = widthSpecSize - handle.getMeasuredWidth() - topOffset;
              getContent().measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(heightSpecSize, MeasureSpec.UNSPECIFIED));
              widthSpecSize = handle.getMeasuredWidth() + topOffset + content.getMeasuredWidth();
              heightSpecSize = content.getMeasuredHeight();
              if (handle.getMeasuredHeight() > heightSpecSize) heightSpecSize = handle.getMeasuredHeight();
          }
      

      【讨论】:

      • +1 - 我使用第一个改进来避免硬编码。我必须做的一个小改动是如何获取 topOffset 值:topOffset = (int) a.getDimension(1, 0);
      【解决方案5】:

      不幸的是,您不能设置高度,而是相反。 topOffset 属性将决定滑动抽屉的高度,但它决定了要刮掉什么而不是它有多高。

      【讨论】:

      • 实际上,我确实设法通过为 SlidingDrawer 提供显式高度来设置高度(如上所述):看起来 topOffset 解决方案是实现相同目标的另一种方法。 (我也找到了这样做的例子,但到目前为止还没有使用 wrap_content。)
      • 这个家伙的面板小部件可以让您使用 wrap_content 设置高度以及其他好东西anddev.org/viewtopic.php?p=16622
      【解决方案6】:

      它对我有用:

      private SlidingDrawer rightSlidingPanel = null;
      
       @Override 
      public void onCreate( Bundle savedInstanceState )
      {
      ...
          rightSlidingPanel = (SlidingDrawer) findViewById( R.id.rightSlidingPanel );
          rightSlidingPanel.post( new Runnable() 
                  {
                      @Override
                      public void run()
                      {
                          rightSlidingPanel.getLayoutParams().width = findViewById( R.id.sliding_content2 ).getMeasuredWidth() + findViewById( R.id.sliding_handle ).getMeasuredWidth();
                      }
      
                  });
      }
      

      XML 布局:

      ...
          <SlidingDrawer
                  android:id="@+id/rightSlidingPanel"
                  android:layout_width="wrap_content"
                  android:layout_height="match_parent"
                  android:layout_alignParentRight="true"
                  android:layout_alignParentTop="true"
                  android:allowSingleTap="true"
                  android:animateOnClick="true"
                  android:content="@+id/sliding_content"
                  android:handle="@+id/sliding_handle"
                  android:orientation="horizontal" >
      
                  <Button
                      android:id="@+id/sliding_handle"
                      style="@style/toolbar_button"
                      android:layout_width="30dp"
                      android:layout_height="wrap_content"
                      android:height="40dp"
                      android:text="&lt;"
                      android:width="25dp" />
      
                  <LinearLayout
                      android:id="@+id/sliding_content"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:gravity="top"
                      android:orientation="vertical" >
      
                      <LinearLayout
                          android:id="@+id/sliding_content2"
                          android:layout_width="wrap_content"
                          android:layout_height="match_parent"
                          android:layout_gravity="center_vertical"
                          android:layout_weight="1"
                          android:gravity="center_horizontal" >
      
      ...
                      </LinearLayout>
                  </LinearLayout>
              </SlidingDrawer>
      ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-05
        • 2011-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-01
        • 2018-03-19
        相关资源
        最近更新 更多