当父视图不占满整个屏幕时,如何将片段中的项目居中显示

3

我在XML设计中遇到了一些问题。我已经在我的应用程序中添加了一个ViewPager,以及在应用程序顶部的一个工具栏。所以,我的问题是当我尝试将属于ViewPager的片段中的项目重力居中时,它们会被设置在ViewPager的中间,忽略了工具栏的高度。

因此,我想问一下,在父视图没有填满整个屏幕的情况下,是否可以使视图或片段中的项目居中显示。

我的活动XML如下:

<RelativeLayout 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:orientation="vertical">

<include layout="@layout/toolbar"
    android:id="@+id/toolbar"></include>

<com.example.android.ui.SlidingTabLayout
    android:id="@+id/module_selector_tabs"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@color/view_pager_top"
    android:layout_below="@id/toolbar"
    android:elevation="4dp"/>
<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/viewPager"
    android:layout_below="@id/module_selector_tabs"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

</RelativeLayout>

我想居中的项目在FragmentAdapter中动态添加到Viewpager的Fragment内。

目前我的应用程序看起来像这样,我只想在整个屏幕中居中显示计时器文本和CircleProgressBar,目前它们都在viewpager的中间:

enter image description here


你能发布一下你想要的屏幕截图吗? - Damini Mehra
添加了照片和更多的定义。 - Francisco Durdin Garcia
2个回答

2
这段话是关于Fragment而不是ViewPager的。您能否粘贴一下Fragment的布局呢?
从图片上看,我会使用一个垂直线性布局来放置两个子元素,第一个用于圆形,第二个用于按钮。
然后,您可以使用layout_weight来控制子元素的起始位置和结束位置。
更多关于layout_weight的信息: https://developer.android.com/guide/topics/ui/layout/linear.html#Weight 很抱歉,我现在没有布局编辑器。

1
最后,我将我的布局叠放在一起,以获得我想要的结果!
<RelativeLayout 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:background="@color/fragment_background"
android:orientation="vertical">


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/countdownLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/buttons_layout">

    <com.example.android.voicetimer.ui.CircleProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="@dimen/progressbar_size"
        android:layout_height="@dimen/progressbar_size"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:progressDrawable="@drawable/circular" />

    <Spinner
        android:id="@+id/timer_name_picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        style="@style/medium_light_hms"
        android:layout_above="@+id/timerText" />


    <com.example.android.voicetimer.ui.HmsView
        android:id="@+id/timerText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:gravity="center">

        <com.example.android.voicetimer.ui.ZeroTopPaddingTextView
            android:id="@+id/hours_ones"
            style="@style/medium_light_hms"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="none"
            android:singleLine="true"
            android:visibility="gone" />

        <TextView
            android:id="@+id/hours_label"
            style="@style/fragment_label"
            android:padding="0dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:ellipsize="none"
            android:singleLine="true"
            android:text="@string/hms_picker_hours_label"
            android:visibility="gone" />

        <com.example.android.voicetimer.ui.ZeroTopPaddingTextView
            android:id="@+id/minutes_tens"
            style="@style/medium_light_hms"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="none"
            android:singleLine="true"
            android:visibility="gone" />

        <com.example.android.voicetimer.ui.ZeroTopPaddingTextView
            android:id="@+id/minutes_ones"
            style="@style/medium_light_hms"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="none"
            android:singleLine="true"/>

        <TextView
            android:id="@+id/minutes_label"
            style="@style/fragment_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:ellipsize="none"
            android:singleLine="true"
            android:text="@string/hms_picker_minutes_label" />

        <com.example.android.voicetimer.ui.ZeroTopPaddingTextView
            android:id="@+id/seconds_tens"
            style="@style/medium_light_hms"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="none"
            android:singleLine="true" />

        <com.example.android.voicetimer.ui.ZeroTopPaddingTextView
            android:id="@+id/seconds_ones"
            style="@style/medium_light_hms"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="none"
            android:singleLine="true"/>

        <TextView
            android:id="@+id/seconds_label"
            style="@style/fragment_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:ellipsize="none"
            android:singleLine="true"
            android:text="@string/hms_picker_seconds_label" />
    </com.example.android.voicetimer.ui.HmsView>
</RelativeLayout>


<LinearLayout
    android:id="@+id/buttons_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:clipChildren="false">

    <ImageView
        android:id="@+id/btnDelete"
        android:layout_width="@dimen/delete_button_size"
        android:layout_height="@dimen/delete_button_size"
        android:layout_gravity="center"
        android:layout_margin="@dimen/icon_margin"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_delete_black"
        android:tint="@color/icon_tint" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接