簡體   English   中英

如何在 ScrollView 和 LinearLayout 中居中

[英]How to center in ScrollView and LinearLayout

請看下面的代碼

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

我需要將 LinearLayout 中的所有內容居中。 我必須使用滾動視圖,因為當應用程序 100% 完成時,如果不滾動,就會有很多信息無法一目了然地查看。 怎樣才能使這些中心? 請幫忙!

更新

使用滾動內的任何布局,沒有問題。 但我需要這些元素按相同的順序(一個接一個)並居中。

您需要將重力設置為以線性布局為中心

< LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="horizontal" >

這個對我有用:

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

嘗試在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>

#如果要在中心進行線性布局,則需要在線性布局中編寫android:gravity="center_horizo​​ntal"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM