Android-水平滚动视图中的居中按钮

Android-水平滚动视图中的居中按钮,android,android-linearlayout,horizontalscrollview,Android,Android Linearlayout,Horizontalscrollview,我正在尝试使用水平滚动视图创建一种图像库。 我希望有一个垂直居中的按钮,但即使我在按钮中指定重力为垂直居中,也无法使其工作,因为它总是粘在顶部。 以下是我的布局: <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:animateLayoutChanges="true">

我正在尝试使用
水平滚动视图
创建一种图像库。 我希望有一个垂直居中的按钮,但即使我在按钮中指定
重力
垂直居中
,也无法使其工作,因为它总是粘在顶部。 以下是我的布局:

        <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>


提前谢谢

在水平滚动视图内的线性布局上,使用

android:layout_gravity="center_vertical"
而不是

android:gravity="center_vertical"

一个模拟屏幕将真正有助于找出你到底想要什么。无论如何,从您的布局我可以理解,您需要在
水平滚动视图的中心放置一个摄像头按钮

在这种情况下,您可能会考虑使用
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>


请在两个滚动视图中添加
android:fillViewport=“true”
在水平滚动视图中设置线性布局,如下所示:

<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>

结果是:


顺便说一句:你可以去掉按钮中的安卓:layout\u gravity=“center\u vertical”属性。

将线性布局的高度设置为与父母匹配。谢谢,我的错!