如何以ScrollView和LinearLayout为中心

时间:2022-10-17 19:17:50

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. 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. How can make these center? Please help!

我需要将所有内容集中在LinearLayout中。我必须使用scrollview,因为当应用程序100%完成时,会有很多信息无法一目了然地无法滚动查看。怎么能让这些中心?请帮忙!

UPDATE

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.

使用滚动内的任何布局,没有概率。但我需要这些元素以相同的顺序(一个接一个)并居中。

3 个解决方案

#1


16  

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" >

#2


3  

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"

#3


0  

Try using layout_gravity on your LinearLayout to center_horizontal:

尝试在LinearLayout上使用layout_gravity到center_horizo​​ntal:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_height="wrap_content"
    android:orientation="horizontal"/>

#1


16  

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" >

#2


3  

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"

#3


0  

Try using layout_gravity on your LinearLayout to center_horizontal:

尝试在LinearLayout上使用layout_gravity到center_horizo​​ntal:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_height="wrap_content"
    android:orientation="horizontal"/>