【问题标题】:Relative Layout, SlidingDrawer and ListView相对布局、SlidingDrawer 和 ListView
【发布时间】:2011-07-12 17:08:54
【问题描述】:

我正在尝试构建一个顶部带有工具栏的 ListView 活动。此工具栏中最右边的工具将是水平 SlidingDrawer 的处理程序,它将在其他工具之上提供滑动 EditText。 首先,我尝试使用 LinearLayout 和 FrameLayout 来实现这一点,但滑块的大小与可用空间减去工具总宽度一样多。现在我已经完成了我想做的事情,并且在以下布局中效果很好:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:id="@+id/laytoptoolbarhost"
    android:layout_width="fill_parent"
    android:layout_height="55dip"
    android:background="@drawable/toptoolbar_gradient"
    android:padding="5dip">
    <View android:id="@+id/tool10"
        android:layout_width="32dip"
        android:layout_height="32dip"
        android:paddingRight="3dip"
        android:background="#00FF00"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true">
    </View>     
    <View android:id="@+id/tool20"
        android:layout_width="32dip"
        android:layout_height="32dip"
        android:paddingRight="3dip"
        android:background="#00FF00"
        android:layout_toRightOf="@+id/tool10"
        android:layout_centerVertical="true">
    </View>
    <View android:id="@+id/tool30"
        android:layout_width="32dip"
        android:layout_height="32dip"
        android:paddingRight="3dip"
        android:background="#00FF00"
        android:layout_toRightOf="@+id/tool20"
        android:layout_centerVertical="true">
    </View>             
    <SlidingDrawer android:id="@+id/slidesearch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:handle="@+id/sliderhandle"
        android:content="@+id/txtsearch">
        <ImageView android:id="@+id/sliderhandle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/btn_collapse_states">
        </ImageView>
        <EditText android:id="@+id/txtsearch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp">
        </EditText>
    </SlidingDrawer>
</RelativeLayout>
<ListView android:id="@id/android:list"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:drawSelectorOnTop="false"/>
 <TextView android:id="@id/android:empty"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:gravity="center"
           android:textSize="20sp"
           android:text="@string/lbl_norecords"/>    

问题是我必须在 RelativeLayout android:layout_height 参数中放置一个固定值才能使其正常工作。如果我放置“wrap_content”,那么 RelativeLayout 会填满整个屏幕,并且不会显示其他任何内容。

非常感谢您对此的任何帮助

提前谢谢你

【问题讨论】:

  • 这很奇怪,我怀疑其中一个孩子的身高导致了问题,但在我看来他们都还不错。有时相对的布局会导致意想不到的后果。您是否尝试过使用空的相对布局(工具栏)或者我猜只有一个孩子,否则它的高度将为零
  • 我试过了,wrap_content 值按预期工作。所以我要一次放一个元素,看看哪里出错了。谢谢
  • 好的,当我添加 SlidingDrawer 时,RelativeLayout 开始乱七八糟。我真的被困在这里了......
  • 我希望您可以添加图像,以便我可以看到抽屉的外观,但是您可以将抽屉的高度设置为您使用的任何可绘制对象吗?
  • 这是高度为 55dip link 的良好布局,这是 wrap_content link 的样子非常感谢您的宝贵时间

标签: android android-relativelayout slidingdrawer


【解决方案1】:

从您的相对布局中的视图中删除 android:layout_centerVertical="true"。我还对您的滑块进行了一些更改,以达到您想要的效果。滑块的关键变化是:

  1. 添加:android:layout_alignTop="@+id/tool30"android:layout_alignBottom="@+id/tool30"

  2. 删除 android:layout_alignParentRight="true"android:layout_centerVertical="true"

在调试布局时,我在其他地方进行了一些其他外观更改。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RelativeLayout
        android:id="@+id/laytoptoolbarhost"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:layout_weight="0">
        <View
            android:id="@+id/tool10"
            android:layout_width="32dip"
            android:layout_height="32dip"
            android:paddingRight="3dip"
            android:background="#00FF00"
            android:layout_alignParentLeft="true"
            >
        </View>
        <View
            android:id="@+id/tool20"
            android:layout_width="32dip"
            android:layout_height="32dip"
            android:paddingRight="3dip"
            android:background="#FFFF00"
            android:layout_toRightOf="@+id/tool10"
            >
        </View>
        <View
            android:id="@+id/tool30"
            android:layout_width="32dip"
            android:layout_height="32dip"
            android:paddingRight="3dip"
            android:background="#00FFFF"
            android:layout_toRightOf="@+id/tool20"
            >
        </View>
        <SlidingDrawer
            android:id="@+id/slidesearch"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:layout_alignTop="@+id/tool30"
            android:layout_alignBottom="@+id/tool30"
            android:handle="@+id/sliderhandle"
            android:content="@+id/txtsearch">
            <ImageView
                android:id="@+id/sliderhandle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/alarm_clock">
            </ImageView>
            <EditText
                android:id="@+id/txtsearch"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textSize="20sp">
            </EditText>
        </SlidingDrawer>
    </RelativeLayout>
    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:drawSelectorOnTop="false" />
    <TextView
        android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="20sp"
        android:text="nothing" 
        />
</LinearLayout>

【讨论】:

  • 你解决我问题的方法真的让我大开眼界。非常感谢您的参与。你绝对是曼德尔 :-)
【解决方案2】:

鉴于您已经确定这是滑动抽屉,您可能想看看这个讨论是否有任何意义(似乎您可以使用该信息)

SlidingDrawer height

如果不是,我会使用该类型的条件进行更多搜索。

【讨论】:

  • 回答您之前关于设置手柄高度(以像素为单位)的评论,它也不起作用。所以我会尝试你的答案建议
【解决方案3】:

尝试使用 LinearLayout 围绕 RelativeLayout,您将在其中放置:
android:layout_width="wrap_content"
android:layout_width="wrap_content

然后将RelativeLayout从固定数量的空间更改为fill_parent
您可以做的另一件事是使用Droid Draw,它可以让您在视觉上进行布局。

【讨论】:

  • 最坏的情况,只需使用 droid draw 进行布局。我将编辑答案以发布链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多