简体   繁体   English

如何将视图(如在RelativeLayout中)与侄子视图对齐?

[英]How to align view (like in RelativeLayout) with a nephew view?

Related question . 相关问题 Answer: RelativeLayout can't do it. 答:RelativeLayout无法做到。 I'm asking how to do it anyway, with not just RL, or with something else. 我问的是无论如何,不​​仅 RL,还是其他。

General story: you have a complex layout that would be difficult to adjust, and along comes a request for something to be added, aligning with a nested view. 总的来说:您的布局复杂,难以调整,随之而来的是要添加一些与嵌套视图对齐的请求。
在此处输入图片说明

What is the best approach? 最好的方法是什么? A popup with a custom style? 具有自定义样式的弹出窗口? (not familiar with those yet)? (还不熟悉)? Spending days changing the whole hierarchy to a single RelativeLayout ? 花几天的时间将整个层次结构更改为一个RelativeLayout A custom Layout class as wrapper? 一个自定义的Layout类作为包装器?
AbsoluteLayout (deprecated) or FrameLayout with programmatically changed LayoutParams or margins? 通过编程更改了LayoutParams或边距的AbsoluteLayout (不建议使用)或FrameLayout? (this I'd rather avoid, I prefer not to touch onMeasure, etc) (这是我想避免的,我不想接触onMeasure等)

Simplified example (no relation to pic above): 简化示例(与上面的图片无关):
LinearLayout defines relative heights of the elements. LinearLayout定义元素的相对高度。 I don't know to do it with RelativeLayout. 我不知道要用RelativeLayout来做。
anExpandableView is something to be animated as sliding from under someBar (here; full-width, but perhaps it may need to align its width, as well as vertical position). anExpandableView是一种动画,可以从someBar下面someBar (此处为全角,但可能需要对齐其宽度以及垂直位置)。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/topStuff"
            layout="@layout/incl_topstuff"
            android:layout_width="match_parent"
            android:layout_weight="7"
            android:layout_height="0dip" />

        <include
            android:id="@+id/someBar"
            layout="@layout/incl_filters_and_stuff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <include
            android:id="@+id/bottomStuff"
            layout="@layout/incl_bottomstuff"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="10" />

    </LinearLayout>

    <include
        android:id="@+id/anExpandableView"
        layout="@layout/incl_filters"
        android:visibility="gone"
        android:layout_below="@id/someBar"/>

</RelativeLayout>

I know SO has an aversion to general questions, but I don't want an ad-hoc solution. 我知道SO可以避免一般性问题,但我不希望采用临时解决方案。 I am asking what to do in cases which would be solved if only a wrapping RelativeLayout would allow alignment to a view that is not a direct sibling. 我问如果仅包装RelativeLayout将允许对齐到不是直接同级的视图的情况下该怎么办。

Putting it simply, RelativeLayout can only measure and layout it's direct children based on each other, but I guess you already knew that. 简而言之,RelativeLayout只能基于彼此来度量和布局直接子对象,但是我想您已经知道这一点。

The only general solution would be to implement your own custom Layout class, which I wouldn't recommend. 唯一的通用解决方案是实现您自己的自定义Layout类,我不建议这样做。 If I had to guess why RelativeLayout does not traverse the entire layout hierarchy at it's level and below, it's probably for performance reasons. 如果我不得不猜测为什么RelativeLayout不能遍历其级别及以下的整个布局层次结构,则可能是出于性能原因。

Unfortunately if you're using RelativeLayouts and LinearLayouts and you want views to be dependent on each other you have to pick one approach and stick to it, either the flat hierarchy of RelativeLayout, or the nested one of LinearLayout. 不幸的是,如果您使用的是RelativeLayouts和LinearLayouts,并且希望视图相互依赖,则必须选择一种方法并坚持使用,即RelativeLayout的平面层次结构或LinearLayout的嵌套结构。

Based on your example, as far as I know, there is no way to implement weighted views with a RelativeLayout, so you're stuck with using a LinearLayout. 据我所知,根据您的示例,无法使用RelativeLayout实现加权视图,因此您不得不使用LinearLayout。

The easiest way to do what you want is to inflate your expandableView in code, align it with the bottom of the RelativeLayout, set it's height and position based on bottomStuff, and animate from there. 做您想要的事情的最简单方法是在代码中膨胀您的expandableView,使其与RelativeLayout的底部对齐,根据bottomStuff设置其高度和位置,并从那里进行动画处理。

If you really want to do it in xml, I can think of one somewhat hacky, ad-hoc approach, but which can can be generalized to mirroring the measurement and layout of any hierarchy with a bit of work. 如果您真的想在xml中执行此操作,则可以想到一种有点特别的临时方法,但是可以将其推广到通过任何工作来镜像任何层次结构的度量和布局。

Create a parallel but invisible LinearLayout that is a sibling of the first one. 创建一个平行但不可见的LinearLayout,它是第一个的同级。 Give it an empty view with weight 7 on top, an invisible copy of someBar in the middle, then your expandable view under that with weight 10. To have it slide up, either animate the height of the invisible someBar and the weight of the empty view on top towards 0, or remove them/set them to gone and set animateLayoutChanges on your LinearLayout. 给它一个空的视图,顶部有权重7,中间是someBar的无形副本,然后是权重为10的可扩展视图。要使其向上滑动,请为不可见的someBar的高度和空容器的权重设置动画在顶部朝0的方向查看,或将其删除/将其设置为消失并在LinearLayout上设置animateLayoutChanges。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM