代码之家  ›  专栏  ›  技术社区  ›  Lorenzo B

为水平滚动视图设置约束

  •  1
  • Lorenzo B  · 技术社区  · 6 年前

    我有如下布局。

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:maxHeight="50dp"
        tools:context=".navigation.NavigationFragment">
    
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/horizontal_scroll_container"
            app:layout_constraintLeft_toLeftOf="parent">
    
            <HorizontalScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >
    
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button1XXX"
                        style="?android:attr/borderlessButtonStyle"/>
    
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button2"
                        style="?android:attr/borderlessButtonStyle"/>
    
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button3"
                        style="?android:attr/borderlessButtonStyle"/>
    
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button4"
                        style="?android:attr/borderlessButtonStyle"/>
    
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button5"
                        style="?android:attr/borderlessButtonStyle"/>
    
                </LinearLayout>
            </HorizontalScrollView>
        </FrameLayout>
    
        <FrameLayout
            android:id="@+id/search_layout"
            android:layout_width="58dp"
            android:layout_height="match_parent"
            android:background="@color/colorAccent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toRightOf="@id/horizontal_scroll_container"/>
    
    </android.support.constraint.ConstraintLayout>
    

    我想实现的是让滚动视图在粉红色的正方形下滚动以显示最新的按钮( Button 5 在这种情况下)。

    enter image description here

    你能帮我找出我做错了什么吗?我试着把这两个限制都放进去 horizontal_scroll_container HorizontalScrollView 但似乎什么都没用。

    另外,我不是android开发人员,所以对我要有耐心;)

    1 回复  |  直到 6 年前
        1
  •  2
  •   Pawel Laskowski    6 年前

    你能做的就是约束你的 FrameLayout 从右到左 search_layout 所以它们不会重叠。最后一条路 Button 将完全可见。

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/horizontal_scroll_container"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/search_layout">
    

    这个 width 已更改为 0dp ( MATCH_CONSTRAINT )所以 ScrollView 获取所有可用的水平空间。

    事实上, 框架布局 根本不需要容器。 HorizontallScrollView 框架布局 所以没必要把它们窝起来。同样的结果也可以通过 HorizontalScrollView 作为 ConstraintLayout 以下内容:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:maxHeight="50dp"
        tools:context=".navigation.NavigationFragment">
    
        <HorizontalScrollView
            android:id="@+id/horizontal_scroll"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@id/search_layout">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
    
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button1"
                    style="?android:attr/borderlessButtonStyle"/>
    
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button2"
                    style="?android:attr/borderlessButtonStyle"/>
    
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button3"
                    style="?android:attr/borderlessButtonStyle"/>
    
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button4"
                    style="?android:attr/borderlessButtonStyle"/>
    
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button5"
                    style="?android:attr/borderlessButtonStyle"/>
    
            </LinearLayout>
        </HorizontalScrollView>
    
        <FrameLayout
            android:id="@+id/search_layout"
            android:layout_width="58dp"
            android:layout_height="match_parent"
            android:background="@color/colorAccent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toRightOf="@id/horizontal_scroll"/>
    
    </android.support.constraint.ConstraintLayout>