相对布局、SlidingDrawer 和 ListView
我正在尝试构建一个顶部有工具栏的 ListView Activity。此工具栏中最右边的工具将是水平 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”,则相对布局将填充整个屏幕,并且不会显示其他内容。
非常感谢对此的任何帮助
提前谢谢您
I am trying to build a ListView Activity with a toolbar at the top. The rightmost tool in this toolbar will be a handler to a horizontal SlidingDrawer which will provide a sliding EditText on top of the other tools.
First I tried to achieve this with LinearLayout and FrameLayout but the slider went as much as the space that was available minus the tools total width. Right now I have done what I want and works great with the following layout:
<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"/>
The problem is that I have to put a fixed value in RelativeLayout android:layout_height parameter in order for this to work. If I place "wrap_content" then the RelativeLayout fills the entire screen and nothing else is showing.
Any help on this is highly appreciated
Thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从相对布局中的视图中删除
android:layout_centerVertical="true"
。我还对您的滑块进行了一些更改以达到您想要的效果。对滑块的关键更改是:添加:
android:layout_alignTop="@+id/tool30"
和android:layout_alignBottom="@+id/tool30"
android:layout_alignParentRight="true"
和android:layout_centerVertical="true"
我在调试布局时在其他地方做了一些其他的修饰性更改。
Remove
android:layout_centerVertical="true"
from the views in your Relative Layout. I also made some changes to your slider to get to what you want. The crucial changes to the slider were:Add:
android:layout_alignTop="@+id/tool30"
andandroid:layout_alignBottom="@+id/tool30"
Remove
android:layout_alignParentRight="true"
andandroid:layout_centerVertical="true"
I made a few other cosmetic changes elsewhere while debugging the layout.
鉴于您已确定它是滑动抽屉,您可能想看看此讨论是否有任何意义(似乎您可以使用该信息)
SlidingDrawer 高度
如果不是,我会用这种类型的标准进行更多搜索。
Given that you have determined that it's the sliding drawer you may want to see if this discussion has any bearing (seems like you might be able to use the info)
SlidingDrawer height
If not I would search a bit more with that type of criteria.
尝试用 LinearLayout 包围relativelayout,您可以在其中放置:
android:layout_width="wrap_content"
android:layout_width="wrap_content
然后将RelativeLayout从固定的空间量更改为
fill_parent
您可以做的另一件事是使用 Droid Draw,它可以让您以视觉方式进行布局。
try surrounding the RelativeLayout with a LinearLayout in which you would put:
android:layout_width="wrap_content"
android:layout_width="wrap_content
then change the RelativeLayout from a fixed amount of space, to
fill_parent
the other thing you can do is to use Droid Draw which will allow you to lay it out visually.