如果有足够的空间,视图应该在布局中心显示

9
我有一个布局,需要把其中一个视图放在中心位置。这很容易实现。但问题是我还有其他一些视图需要放在屏幕顶部。
问题:如果有足够的空间,它看起来很好,但当屏幕太小时,顶部和中心元素会重叠。
问题:如果屏幕太小,如何将“居中”元素向下推?
原始布局非常复杂,我准备了一个示例:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/theOneAtTheTop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Some long text which may overlap the lower view. Some long text which may overlap the lower view."
    android:layout_alignParentTop="true"
    />


<TextView
    android:id="@+id/theOneInTheMiddlep"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Some long text which may overlap the upper view. Some long text which may overlap the upper view. "
    android:layout_centerInParent="true"
    />


</RelativeLayout>

这是一个小型的模拟。中间的图片显示了在小屏幕上的情况,两侧的图片则展示了期望达到的效果。


请放置一个屏幕截图和布局代码。 - Mohammad Ersan
我已经发布了XML,一分钟内会准备好一张图片。原始的XML太复杂了,很难理解要点。 - Michał Klimczak
我担心这是一个类似于http://stackoverflow.com/questions/3242801/android-layout-alignment-issue的问题,当时没有得到任何答案...我还无法找到解决方案... - Orabîg
只是一种直觉:你没有充分利用相对布局...再努力一点。把所有东西都放在相对布局中。 - Radu
好的(如果你不是在讽刺),所有的东西都在一个RelativeLayout中,我怀疑嵌套它们也没有帮助... - Michał Klimczak
3个回答

1

我真的怀疑你能否直接在布局文件中完成这个操作,因为 XML 布局不是这种条件逻辑的地方。唯一真正可以使用布局文件的情况(我认为)是如果您知道涉及视图的确切尺寸以及应用程序的 API 级别为 3.2,需要为不同情况创建许多布局(我真的很怀疑)。

如果您将其中一个视图居中放置在 RelativeLayout 中,它将始终居中,无论如何,它没有在 xml 布局文件级别上重叠视图的概念。在代码中则是另一回事。


是的,我的想法也一样。但这个平台曾经多次让我感到惊讶(大多数情况下都是不好的方式),所以我仍然认为这个问题值得被问到。谢谢! - Michał Klimczak

1

好的,我完全改变了这个答案。如果所需的对齐方式是垂直的,则可以将代码放置在RelativeLayout中,它将_fill_parent_并位于上面的那个控件之。在里面,您可以放置要垂直居中对齐的视图。

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

    <TextView
        android:id="@+id/theOneAtTheTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Some long text which may overlap the lower view. Some long text which may overlap the lower view." />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/theOneAtTheTop" >

        <TextView
            android:id="@+id/theOneInTheMiddlep"
            android:layout_width="200sp"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="Some long text which may overlap the upper view. long text which may overlap the upper view. Some long text which may overlap the upper view.  " />
    </RelativeLayout>

</RelativeLayout>

希望能有所帮助


说实话,我看不出你的代码和我的有什么区别。 - Michał Klimczak
1
抱歉,这是因为我贴错了代码(实际上是您之前的代码,未经修改)。 - Virgílio Santos
我的答案和Vinay的类似,这让我想问:您希望视图在水平方向还是垂直方向上居中? - Virgílio Santos
垂直方向。问题在于,layout_centerInParent将覆盖layout_below,反之亦然,具体取决于哪个出现在代码中的位置最后。 - Michał Klimczak
好的,这改变了一些东西,我编辑了答案以匹配问题。需要注意的是,我只是将centerVertical与RelativeLayout父元素对齐,但也可以使用其他对齐方式。 - Virgílio Santos

0
只需将 android:layout_below="@+id/theOneAtTheTop" 添加到第二个 TextView 中,就可以了。

<TextView
    android:id="@+id/theOneAtTheTop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Some long text which may overlap the lower view. Some long text which may overlap the lower view.Some long text which may overlap the lower view. Some long text which may overlap the lower view.Some long text which may overlap the lower view. Some long text which may overlap the lower view.Some long text which may overlap the lower view. Some long text which may overlap the lower view.Some long text which may overlap the lower view. Some long text which may overlap the lower view.Some long text which may overlap the lower view. Some long text which may overlap the lower view."
    android:layout_alignParentTop="true"
    />


<TextView
    android:id="@+id/theOneInTheMiddlep"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/theOneAtTheTop"
    android:text="Some long text which may overlap the upper view. Some long text which may overlap the upper view. "
    android:layout_centerInParent="true"
    />


</RelativeLayout>

这将覆盖 layout_centerInParent,所以如果屏幕足够大,theOneInTheMiddle 将不会在中心。 - Michał Klimczak
正确的,我的错。忘记了偏好设置。 - Vinay W

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接