【问题标题】:How to center a view in parent and add a view below it inside a Relative Layout inside a Scroll view如何在父视图中居中并在滚动视图内的相对布局中在其下方添加视图
【发布时间】:2018-03-28 08:12:55
【问题描述】:

我想制作一个相对布局,用户可以向下滚动并有许多文本视图 - 每个视图都在另一个从屏幕中心开始的下方。 为此,我将相对布局包装在滚动视图中,并且有两个文本视图。其中一个文本视图具有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

【问题讨论】:

  • android:layout_centerInParent="true" 将作为 android:layout_centerHorizontal="true"wrap_content height 一起使用。你能添加预期的输出吗?
  • 尝试清理项目并确保应用程序已正确重新安装。您的布局看起来像您解释的那样工作
  • @ADM 输出应该是相对布局中心的第一个视图和水平居中并低于第一个视图的第二个视图
  • 如果您在 android studio 中使用预览工具,请尝试强制刷新布局。看看这个image
  • @GiliJacobi 你能上传视图的截图吗?如果可能的话,你可以在实际设备上上传视图的截图而不是预览。由于 olegr 、 JyotiJk 、 ADM 和我已经验证了您在上面给出的代码按预期工作。

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


【解决方案1】:

尝试使用这个: 让我知道是否有任何事情,否则这肯定会起作用 尝试改用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>

【讨论】:

    【解决方案2】:
        <?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>
    

    试试这个,如果没有工作,请反馈。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-22
      • 1970-01-01
      • 1970-01-01
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多