1

我想创建一个动态扩展的布局,它可能会反映用户的输入。首先,我创建了一个允许双向滚动的活动布局(这不是我的发明,我使用了在对另一个问题的评论中解释的想法并进行了一些改进):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clipChildren="false"
    android:clipToPadding="false"
    tools:ignore="UselessParent" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clipChildren="false"
        android:clipToPadding="false" >

        <RelativeLayout
            android:id="@+id/content"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clipChildren="false"
            android:clipToPadding="false" >

        </RelativeLayout>
    </FrameLayout>
</FrameLayout>

然后,我在下面创建了一个简单的 UI 布局、一个文本视图和一个垂直线性布局。这是我的 message_with_replies.xml:

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

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="300dp"
        android:layout_height="wrap_content" >

<!-- some text views and buttons here, 
    I've removed them to keep things simple,
    see the screenshot below -->

    </LinearLayout>

    <LinearLayout
        android:id="@+id/replies"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

请注意回复布局中的 10dp 边距以及与剪辑相关的属性。

所以我现在可以用 message_with_replies 布局膨胀一个项目并将其添加到活动的内容布局中,然后膨胀另一个项目并将其添加到第一个项目的回复线性布局中,依此类推,从而创建一个树状层次结构. 整个树在水平和垂直方向上都显示和滚动。

但是,看起来布局仍然受到屏幕尺寸的限制。我有两个问题:

1) 右边缘以外的项目看起来不错,但没有收到用户输入。2)底部边缘以外的布局混乱。

这是屏幕截图。从左到右:1)一切看起来都很好,2)哎呀,按钮在边缘,3)哎呀,底部搞砸了。

从左到右:1)一切看起来都很好,2)哎呀,按钮在边缘,3)哎呀,底部乱七八糟了。

看起来我错过了一些简单但隐藏的东西。如何使布局超出屏幕边缘?

4

1 回答 1

10

解决方案非常简单。

在活动布局中,RelativeLayout 节点(具有内容ID 的节点)应使用覆盖 onMeasure 方法的自定义布局类。

所以这里是活动布局的修复:

<com.sample.MyRelativeLayout
    android:id="@+id/content"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clipChildren="false"
    android:clipToPadding="false" >
</com.sample.MyRelativeLayout>

这是类的实现:

public class MyRelativeLayout extends RelativeLayout {
    public MyRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(0, 0);
    }
}

请注意,我不必计算任何东西,传递 (0, 0) 就可以了。

坦率地说,我还不了解此解决方案的所有优缺点,但它可以正常工作。到目前为止我注意到的唯一问题是我展开的项目越多,UI 响应越慢。

我将不胜感激任何意见或建议。

于 2012-09-03T14:52:50.733 回答