如何在 LinearLayout 中使文本中心顶部和屏幕的其他文本中心?

How to make a text center top and other text center of screen in LinearLayout?

我有两个textviews。我想制作一个 textview 顶部中心,可以用作应用程序的标题。第二个 textview 是用于显示细节的屏幕中心。我使用 ScrollViewLinearLayout 如下设置。但是,它不适用于第二个 textview。你能看看我的布局文件并告诉我解决它的方法吗?我需要使用ScrollViewLinearLayout,因为我不想修改java代码。

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

    <LinearLayout android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:orientation="vertical">
        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                >

        </LinearLayout>

        <TextView
            android:id="@+id/title_app"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:text="Main title-Center-top"
            android:layout_gravity="center"
            android:gravity="center_horizontal"
            android:textSize="20sp"
            android:layout_marginTop="20dp"
            />

        <TextView
                android:id="@+id/display"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:textSize="20sp"
                android:gravity="center"
                android:text="I want to center of screen"
            />

    </LinearLayout>    
</ScrollView>

你可以这样做

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

        <LinearLayout 
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:orientation="vertical">
         <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_weight="0"
                  android:orientation="horizontal">
         <TextView
                 android:id="@+id/title_app"
                 android:layout_width="fill_parent"
                 android:layout_height="50dp"
                 android:text="Main title-Center-top"
                 android:layout_gravity="center"
                 android:gravity="center_horizontal"
                 android:textSize="20sp"
                 android:layout_marginTop="20dp"
            />
        </LinearLayout>


        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">
        <TextView
                android:id="@+id/display"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:textSize="20sp"
                android:gravity="center"
                android:text="I want to center of screen"
            />
     </LinearLayout>


    </LinearLayout>

</ScrollView>

我觉得你的代码没问题。

就把android:fillViewport="true" on the ScrollView

你的问题似乎是 ScrollView 没有伸展到它的全高。

这是完整的 xml 代码,可以满足您的需求,我刚刚对其进行了测试并有效:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:fillViewport="true">

<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title_app"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Main title-Center-top"
        android:layout_gravity="center"
        android:gravity="center_horizontal"
        android:textSize="20sp"
        android:layout_marginTop="20dp"
        />

    <TextView
        android:id="@+id/display"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="20sp"
        android:gravity="center"
        android:text="I want to center of screen"
        />

</LinearLayout>