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)
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)
小智 90
我认为一个更优雅的解决办法是使用ScrollView
的android: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:gravity
对LinearLayout
自ScrollView
会伸展LinearLayout
所以记住这一点取决于你添加到布局什么.
另一个好读ScrollView
,虽然不是中心,但大约fillViewport
是http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/
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一样.
对于@Patrick_Boss - 在我的应用程序中停止切割内容的原因是将LinearLayout的重力和layout_gravity更改为center_horizontal.
它对我有用......但不确定它是否适合你.
修改@ 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)
归档时间: |
|
查看次数: |
51501 次 |
最近记录: |