如何在父级中居视图并在Scroll视图内的Relative Layout内添加其下方的视图

时间:2018-03-28 08:12:56

标签: android android-layout android-scrollview android-relativelayout

我想制作一个相对布局,用户可以向下滚动并有许多文本视图 - 每个文本视图中的每一个都在屏幕中心开始的另一个下面。 为此,我将相对布局包装在滚动视图中,我有两个文本视图。其中一个文字视图为android:layout_centerInParent="true",另一个文字视图为android:layout_centerHorizontal="true"android:layout_below="first text view id"

问题是android:layout_below="first text view id"没有效果:我在屏幕中央看到第一个文本视图,屏幕顶部的另一个视图水平居中。

我的布局:

<?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:fillViewport="true"
    android:id= "@+id/chooseMapsScroll">

    <RelativeLayout
        android:id="@+id/chooseMaps"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/title"
        android:fillViewport="true"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="test center in parent"
            android:id="@+id/test_below"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="test center below"
            android:layout_below="@id/test_below"/>
    </RelativeLayout>
</ScrollView>

更新 我发现布局的背景图像是导致问题的原因。

证据:

That's the output I got without background image

That's the output I got with background image

2 个答案:

答案 0 :(得分:2)

尝试使用此: 让我知道是否有什么,否则这肯定会起作用 请尝试转而使用LinearLayout

<?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:fillViewport="true"
    android:id= "@+id/chooseMapsScroll">

    <LinearLayout
        android:id="@+id/chooseMaps"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/title"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test center in parent"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test center below"/>
    </LinearLayout>



</ScrollView>

答案 1 :(得分:0)

    <?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:fillViewport="true"
    android:id= "@+id/chooseMapsScroll">

    <LinearLayout
        android:id="@+id/chooseMaps"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/title"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test center in parent"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test center below"/>
    </LinearLayout>



</ScrollView>

试试这个,如果没有反馈的话。