【问题标题】:Android - centering Button inside an HorizontalScrollViewAndroid - Horizo​​ntalScrollView 中的居中按钮
【发布时间】:2016-04-05 13:29:20
【问题描述】:

我正在尝试使用HorizontalScrollView 创建一种图片库。 我想要一个垂直居中的按钮,但即使我在我的按钮中将gravity 指定为center_vertical,也无法让它工作,因为它总是粘在顶部。 这是我的布局:

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

        <LinearLayout
            android:id="@+id/container_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <HorizontalScrollView
                android:id="@+id/horizontal_scroll_view"
                android:layout_width="match_parent"
                android:layout_height="@dimen/cameraPlaceholderHeight">

                <LinearLayout
                    android:id="@+id/horizontal_scroll_view_container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/greyBackgroundColor"
                    android:gravity="start|center_vertical"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/camera_button"
                        android:layout_width="@dimen/take_picture_size"
                        android:layout_height="@dimen/take_picture_size"
                        android:layout_gravity="center_vertical"
                        android:background="@drawable/take_picture_rounded_button"
                        android:gravity="center_vertical"
                        android:src="@drawable/syi_camera_active"/>
                </LinearLayout>
            </HorizontalScrollView>

提前致谢!

【问题讨论】:

  • 将线性布局的高度设置为 match_parent
  • 非常感谢,我的错!

标签: android android-linearlayout horizontalscrollview


【解决方案1】:

在 Horizo​​ntalScrollView 内的 LinearLayout 上,使用

android:layout_gravity="center_vertical"

而不是

android:gravity="center_vertical"

【讨论】:

    【解决方案2】:

    模拟屏幕确实有助于找出您真正想要的内容。无论如何,从您的布局我可以理解,您需要在HorizontalScrollView 的中心放置一个相机按钮。

    在这种情况下,您可能会考虑使用RelativeLayout 包裹水平滚动视图,如下例所示

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <HorizontalScrollView
            android:id="@+id/horizontal_scroll_view"
            android:layout_width="match_parent"
            android:layout_height="@dimen/activity_horizontal_margin">
    
            <!-- Other items here --> 
        </HorizontalScrollView>
    
        <Button
            android:id="@+id/camera_button"
            android:layout_width="@dimen/activity_horizontal_margin"
            android:layout_height="@dimen/activity_horizontal_margin"
            android:layout_centerInParent="true"
            android:background="@drawable/com_facebook_button_blue"
            android:gravity="center_vertical"
            android:src="@drawable/com_facebook_button_blue" />
    </RelativeLayout>
    

    【讨论】:

      【解决方案3】:

      请在您的两个滚动视图中添加android:fillViewport="true"

      【讨论】:

        【解决方案4】:

        像这样在 Horizo​​ntalScrollView 中设置 LinearLayout:

        <HorizontalScrollView
            android:id="@+id/horizontal_scroll_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        
            <LinearLayout
                android:id="@+id/horizontal_scroll_view_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="start|center_vertical"
                android:orientation="horizontal">
        
                <Button
                    android:text="Button"
                    android:id="@+id/camera_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"/>
            </LinearLayout>
        </HorizontalScrollView>
        

        结果:

        顺便说一句:您可以去掉 Button 中的 android:layout_gravity="center_vertical" 属性。

        【讨论】: