【问题标题】:Center view in viewed screen已查看屏幕中的中心视图
【发布时间】:2016-08-27 13:53:06
【问题描述】:

我在整个网络上搜索,但没有看到有人问这个: 嗨,有一个 scroolview,我需要一个 view 才能在查看的屏幕中居中,我的意思是: 我设置了一个gravity:center attribute,但是当scrollview 变长时,view 不再可见,因为它位于scrollview 的中心,而不是在手机屏幕上。如何使view 以您所看到的屏幕大小居中?

编辑,XML

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.percent.PercentRelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#FFEFD5"
            android:id="@+id/dialog_layout"
            android:clickable="false"
            android:visibility="gone">

            <EditText
                app:layout_widthPercent="75%"
                app:layout_heightPercent="12%"
                android:id="@+id/testo"
                app:layout_marginLeftPercent="9%"
                app:layout_marginRightPercent="9%"
                android:background="#F5F5F5"
                app:layout_marginTopPercent="5%"
                android:hint="Please, insert the text for the new button."
                android:gravity="center"
                android:maxLength="19"/>


            <Button
                app:layout_widthPercent="75%"
                app:layout_heightPercent="12%"
                android:layout_below="@id/testo"
                app:layout_marginLeftPercent="9%"
                app:layout_marginRightPercent="9%"
                app:layout_marginBottomPercent="3%"
                android:text="Accept"
                android:id="@+id/accept"/>

        </android.support.percent.PercentRelativeLayout>

    </FrameLayout>

</ScrollView>

【问题讨论】:

  • 为什么要使用滚动视图?
  • 向我们展示相关的xml,否则没有人可以帮助你。
  • @XaverKapeller 已编辑; masoudvali 只是因为我需要它 :)
  • 那个 xml 中有很多地方是错误的。您希望哪个视图居中?
  • @XaverKapeller 为什么很多事情都错了?它对我有用;我想将所有PercentRelativeLayout居中

标签: android xml view scrollview


【解决方案1】:

试试这个

   <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">



        </FrameLayout>

    </ScrollView>
     <android.support.percent.PercentRelativeLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="#FFEFD5"
                android:id="@+id/dialog_layout"
                android:clickable="false"
                android:layout_centerInParent="true"
                android:visibility="gone">

                <EditText
                    app:layout_widthPercent="75%"
                    app:layout_heightPercent="12%"
                    android:id="@+id/testo"
                    app:layout_marginLeftPercent="9%"
                    app:layout_marginRightPercent="9%"
                    android:background="#F5F5F5"
                    app:layout_marginTopPercent="5%"
                    android:hint="Please, insert the text for the new button."
                    android:gravity="center"
                    android:maxLength="19"/>


                <Button
                    app:layout_widthPercent="75%"
                    app:layout_heightPercent="12%"
                    android:layout_below="@id/testo"
                    app:layout_marginLeftPercent="9%"
                    app:layout_marginRightPercent="9%"
                    app:layout_marginBottomPercent="3%"
                    android:text="Accept"
                    android:id="@+id/accept"/>

            </android.support.percent.PercentRelativeLayout>
</RelativeLayout>

【讨论】:

  • 为什么在scrollview 中插入centerInParent 属性?我只需要居中PercentRelativeLayout
  • Android Studio 警告我:“FrameLayout 中的布局参数无效”
  • 哥们在跑吗?
  • 这个答案有几个问题,不要使用它。请参阅我在问题上的 cmets 中所说的内容。
  • @XaverKapeller 我已经用你的改变尝试了他的建议(还没有删除fillViewPort);它有效,但不正确,我的意思是:现在PercentRelativeLayout 可见,因为scrollview 自动滚动到他的垂直一半,但PercentRelativeLayout 仍然保留在scrollview 的中心而不是中心屏幕......还有Halumz,不要粗鲁,没有必要
猜你喜欢
  • 2012-10-07
  • 1970-01-01
  • 2011-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多