[英]How to center in ScrollView and LinearLayout
Please have a look at the following code请看下面的代码
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DisplayResult" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/zodiac1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/aries"
/>
<TextView
android:id="@+id/orTxt"
android:text="Or"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/zodiac2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/gemini"
/>
</LinearLayout>
</ScrollView>
I need to center everything inside the LinearLayout.我需要将 LinearLayout 中的所有内容居中。 I must use scrollview because when the app is 100% done, there will be lot of information which cannot be viewed by a single glance, without scrolling.
我必须使用滚动视图,因为当应用程序 100% 完成时,如果不滚动,就会有很多信息无法一目了然地查看。 How can make these center?
怎样才能使这些中心? Please help!
请帮忙!
UPDATE更新
use any layout inside the scroll, no prob.使用滚动内的任何布局,没有问题。 But I need those elements to be in the same order (one after another) and centered.
但我需要这些元素按相同的顺序(一个接一个)并居中。
you need to set gravity as centered for linear layout您需要将重力设置为以线性布局为中心
< LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal" >
This one worked for me:这个对我有用:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
Try using layout_gravity on your LinearLayout
to center_horizontal
:尝试在
LinearLayout
上使用 layout_gravity 到center_horizontal
:
<LinearLayout
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DisplayResult"
scrollbars="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/zodiac1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/aries"
/>
<TextView
android:id="@+id/orTxt"
android:text="Or"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/zodiac2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/gemini"
/>
</LinearLayout>
</ScrollView>
#If you want to Linear layout in a center you need to write android:gravity="center_horizontal" in your linear layout. #如果要在中心进行线性布局,则需要在线性布局中编写android:gravity="center_horizontal" 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.