没有引力滚动视图.如何将scrollview中的内容作为中心

Pad*_*mar 101 android scrollview

我希望scrollView中的内容为中心.

<ScrollView
    android:id="@+id/scroller"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="12dp"
    android:paddingBottom="20dp"
    android:scrollbarStyle="outsideOverlay"
    android:layout_gravity="center" >

    <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="check" 
        android:gravity="center_vertical|center_horizontal"/>

</ScrollView>
Run Code Online (Sandbox Code Playgroud)

注意:android:gravity scrollvew 没有属性.

任何溶胶: -

had*_*adi 168

我有同样的问题,最后想出来了.这是垂直的ScrollView.

把你的ScrollView内心放在一个RelativeLayout中心RelativeLayout.为了使这个工作,你ScrollView应该有

android:layout_height="wrap_content"
Run Code Online (Sandbox Code Playgroud)

这是最终代码的样子:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_centerVertical="true" >

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

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b1"/>
            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b2"/>
            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="b3"/>
        </LinearLayout>

    </ScrollView>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

  • 这个似乎对我来说比接受的答案更好.谢谢! (29认同)
  • 嘿它工作,但键盘可见时不滚动,我尝试添加清单`android:windowSoftInputMode ="stateHidden | adjustResize"`但是,不要滚动,请帮我 (3认同)
  • 不需要使用重`RelativeLayout`作为根视图.它可以是`FrameLayout`和`android:layout_gravity ="center_vertical"`用于`ScrollView` (3认同)
  • 这有点像黑客,所以虽然这很好但是_feels_错了. (2认同)

Gho*_*ost 145

这个怎么样?

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="12dp"
    android:paddingBottom="20dp"
    android:scrollbarStyle="outsideOverlay" >


    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:layout_gravity="center"
        android:gravity="center">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="check" 
            android:gravity="center_vertical|center_horizontal"/>
    </LinearLayout>

</ScrollView>
Run Code Online (Sandbox Code Playgroud)

  • 这有一个问题.如果您在ScrollView中的内容大于滚动视图,它仍将使其居中并切断顶部的内容(虽然不确定原因).意思是你无法在那里滚动. (93认同)
  • PatrickBoos是对的; 这似乎不起作用.使用下面的hadi答案,将scrollview垂直居中在RelativeLayout中. (6认同)
  • 是啊!那个怎么样!这非常适合我,所以非常感谢,亲切的先生. (2认同)

小智 90

我认为一个更优雅的解决办法是使用ScrollViewandroid:fillViewport属性.A ScrollView对它的内容视图(只能有一个)的处理方式略有不同,即使你设置match_parent(fill_parent)ScrollView它也不会给它的内容视图留出那么多的间距,而是默认行为是为了ScrollView包装它无论您为该视图指定了什么内容.什么android:fillViewport是告诉ScrollView拉伸其内容以填充视口(http://developer.android.com/reference/android/widget/ScrollView.html#attr_android:fillViewport).因此,在这种情况下,您LinearLayout将被拉伸以匹配视口,如果高度位于视口后面,那么它将是可滚动的,这正是您想要的!

当内容超出范围时,接受的答案将无法正常工作,ScrollView因为它仍然会首先使内容视图居中,导致其切断视图的一部分,并且ScrollView在另一个布局中居中但是感觉不对,除此之外我认为这也会导致一个lint错误(无用的父母或沿着这些线的东西).

尝试这样的事情:

<ScrollView
    android:id="@+id/scroller"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="12dp"
    android:paddingBottom="20dp"
    android:scrollbarStyle="outsideOverlay"
    android:fillViewport="true">

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

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="check" />

    </LinearLayout>

</ScrollView>
Run Code Online (Sandbox Code Playgroud)

请记住,它正在这里现在中心的原因是因为android:gravityLinearLayoutScrollView会伸展LinearLayout所以记住这一点取决于你添加到布局什么.

另一个好读ScrollView,虽然不是中心,但大约fillViewporthttp://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/

  • 这看起来是最正确的实现,不需要额外的视图. (10认同)
  • 工作比接受的答案更好,你节省了我很多时间!Brian Ethier真的很棒! (2认同)
  • 典型的 Stackoverflow .. 正确的解决方案通常位于中间的某个位置。 (2认同)

Abh*_*arg 11

在ScrollView中使用此属性

android:fillViewport="true"
Run Code Online (Sandbox Code Playgroud)

 <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="outsideOverlay"
android:fillViewport="true"
>
   <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content">

         <!--Your Code goes here-->

   </RelativeLayout>

</ScrollView>
Run Code Online (Sandbox Code Playgroud)

确保在ScrollView中使用Single Child,就像上面的例子是Relativelayout一样.

  • 这是应该接受的答案。...为什么这不是更受欢迎的答案? (2认同)

Pro*_*ode 9

对于@Patrick_Boss - 在我的应用程序中停止切割内容的原因是将LinearLayout的重力和layout_gravity更改为center_horizo​​ntal.

它对我有用......但不确定它是否适合你.

修改@ Ghost的答案 -

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="12dp"
    android:paddingBottom="20dp"
    android:scrollbarStyle="outsideOverlay" >


    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="check" 
            android:gravity="center_vertical|center_horizontal"/>
    </LinearLayout>

</ScrollView>
Run Code Online (Sandbox Code Playgroud)