【问题标题】:Horizontal Align elements inside a ViewPagerViewPager 中的水平对齐元素
【发布时间】:2026-02-01 20:55:01
【问题描述】:

所以我的问题如下...我有一个具有 3 个主要元素的 RelativeLayout。一列左侧有 2 个按钮,右侧有另一个按钮,中间有一个 ScrollView 布局,其中包含一个 ViewPager 和一个按钮。

按钮与它的父级对齐。但我无法让 ViewPager 自行对齐。

这是我的 XML 布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<LinearLayout
    android:id="@+id/leftButtons"
    android:weightSum="1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginLeft="10dip"
    android:orientation="vertical"
    android:layout_alignParentStart="true"
    >

    <Space
        android:layout_weight="0.5"
        android:layout_width="match_parent"
        android:layout_height="0dip" />
    <ImageButton
        android:id="@+id/leftArrowBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:contentDescription="@string/left_arrow"
        android:gravity="center_vertical|center_horizontal"
        android:src="@drawable/arrow_left_selector"
        android:layout_marginBottom="60dip"
        />

    <ImageButton
        android:id="@+id/leftArrowBtnAll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:contentDescription="@string/left_arrow"
        android:gravity="center_vertical|center_horizontal"
        android:src="@drawable/arrow_left_selector_all" />
</LinearLayout>

<ScrollView
    android:id="@+id/middleLayouts"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:layout_toRightOf="@+id/leftButtons"
    android:layout_toLeftOf="@+id/rightButtons"
    >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="35dip"
        android:gravity="center_horizontal"
        >

        <Button
            android:id="@+id/selectDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dip"
            android:background="@drawable/default_button_rounded"
            android:drawablePadding="35dip"
            android:drawableRight="@drawable/ic_date_light"
            android:gravity="center_vertical|center_horizontal"
            android:paddingBottom="5dip"
            android:paddingLeft="25dip"
            android:paddingRight="25dip"
            android:paddingTop="5dip"
            android:text="@string/monthly_statistics_"
            android:textColor="@color/light_bg" />

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
        />
    </LinearLayout>
</ScrollView>

<LinearLayout
    android:id="@+id/rightButtons"
    android:weightSum="1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginRight="10dip"
    android:orientation="vertical"
    android:layout_alignParentEnd="true"
    >
    <Space
        android:layout_weight="0.5"
        android:layout_width="match_parent"
        android:layout_height="0dip" />
    <ImageButton
        android:id="@+id/rightArrowbtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:contentDescription="@string/right_arrow"
        android:gravity="center_vertical|center_horizontal"
        android:src="@drawable/arrow_right_selector"
        android:layout_marginBottom="60dip"
        />

    <ImageButton
        android:id="@+id/rightArrowbtnAll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:contentDescription="@string/right_arrow"
        android:gravity="center_vertical|center_horizontal"
        android:src="@drawable/arrow_right_selector_all" />
</LinearLayout>

这是它的外观截图......

这是布局编辑器的屏幕截图,显示表格应该对齐......

我尝试了所有类型的布局和重力等。但我无法让它居中! :(

谢谢

【问题讨论】:

    标签: android android-layout android-viewpager scrollview android-relativelayout


    【解决方案1】:

    在我看来,滚动视图内的线性布局应该具有 match_parent 而不是 wrap_content 的宽度

    <ScrollView
        android:id="@+id/middleLayouts"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:layout_toRightOf="@+id/leftButtons"
        android:layout_toLeftOf="@+id/rightButtons"
        >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingTop="35dip"
            android:gravity="center_horizontal"
            >
    

    【讨论】:

    • 在我发帖之前,我尝试了所有类型的宽度和环绕内容。所以那不是我的问题。我用正在发生的事情回答了我的问题。无论如何谢谢")
    【解决方案2】:

    我终于明白了。我的问题是我使用TableLayout 创建表格,TableLayout 始终将其宽度设置为match_parent。因此,TableLayout 使用了整个宽度而不是居中。我从TableLayout 切换到带有垂直TableRowsLinearLayout,现在工作得很好。 :)

    【讨论】: