【问题标题】:Horizontal scroll view expanding when views added programmatically以编程方式添加视图时水平滚动视图扩展
【发布时间】:2014-06-23 06:09:12
【问题描述】:

以编程方式将视图添加到水平滚动视图时遇到问题。我有一个带有虚线背景的水平滚动视图,并为其周围的线性布局提供了清晰的颜色背景以帮助理解。

这是之前的视图

然后我在拖放事件后向滚动视图添加一个视图:

MemoNodeView mn = new MemoNodeView(this);
mn.setMemo(m);
int time = (int) Math.round(pixelsPerMilliSecond()*m.getTimeLength());
mn.setMemoSize(time, timelineHeight);
timeline.addView(mn, new ViewGroup.MarginLayoutParams(time, timelineHeight));

这导致视图垂直扩展:

我试图通过根据它应该具有的高度重新设置滚动视图内的线性布局的布局参数来解决这个问题:

ViewGroup.LayoutParams p = timeline.getLayoutParams();
p.height = timelineHeight;
timeline.setLayoutParams(p);

导致滚动视图仍在展开但内容大小合适的情况:

然后我尝试调整滚动视图本身的大小,以使其大小与应有的大小相匹配:

ViewGroup.LayoutParams p2 = timelineScrollView.getLayoutParams();
p2.height = timelineHeight;
timelineScrollView.setLayoutParams(p2);

它给出了:

保持滚动视图的大小正确,但其他视图的大小缩小并且位置稍有错误。我想要的是让滚动视图保持与第一张图像中的相同,并且没有任何东西可以移动。这可能是一些基本的东西,但我一生都无法弄清楚我做错了什么来给出这种行为。

该区域的 xml 如下(请注意,颜色/文本视图是用于尝试帮助使问题在视觉上突出的占位符):

`<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/black_frame"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#ffffff" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Small Text" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="3"
        android:orientation="vertical"
        android:paddingLeft="@dimen/toggle_draw_padding"
        android:paddingRight="@dimen/toggle_draw_padding" >


        <HorizontalScrollView
            android:id="@+id/horizontal_scroll_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/timeline_dots_bitmap" 
            android:fillViewport="true">

            <LinearLayout
                android:id="@+id/timeline_view"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:background="#F0AA4444" >
            </LinearLayout>
        </HorizontalScrollView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#ffffff" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Small Text" />
    </LinearLayout>
</LinearLayout>`

关于我在这里做错了什么有什么想法吗?

【问题讨论】:

    标签: android android-linearlayout android-custom-view horizontalscrollview


    【解决方案1】:

    我发现了导致问题的原因。我会在这里发帖,以防其他人有类似的问题,这也让他们精神崩溃......

    具有白色背景的线性布局具有layout_height="match_parent",当添加其他视图时会出现奇怪的行为。将两个布局更改为layout_height="0dip" 解决了这个问题。如下图:

    `

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:background="#ffffff" >
    
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Small Text" />
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/LinearLayout1"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="3"
            android:orientation="vertical"
            android:paddingLeft="@dimen/toggle_draw_padding"
            android:paddingRight="@dimen/toggle_draw_padding" 
            >
    
    
            <HorizontalScrollView
                android:id="@+id/horizontal_scroll_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/timeline_dots_bitmap" 
                android:fillViewport="true"
                android:layout_margin="0dip"
                android:padding="0dip">
    
                <LinearLayout
                    android:id="@+id/timeline_view"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:background="#F0AA4444" 
                    android:layout_margin="0dip"
                    android:padding="0dip">
                </LinearLayout>
            </HorizontalScrollView>
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:background="#ffffff" >
    
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Small Text" />
        </LinearLayout>
    </LinearLayout>`
    

    我希望这可以帮助任何以某种方式犯我所犯错误的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-12
      相关资源
      最近更新 更多