如果有足够的空间,请在布局中心查看

时间:2012-09-10 12:12:06

标签: android android-layout

我有一个布局,需要将其中一个视图放在中心。这很简单。问题是我还有一些其他视图需要定位到屏幕顶部。

问题:如果有足够的空间看起来不错,但是当屏幕太小时,顶部和中心元素重叠

问题:如果屏幕很小,如何将“居中”元素按下?

原始布局非常复杂,我准备了一个例子:

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

这是一个小模拟。中间的图片显示了它在小屏幕上的外观,并且两侧的图片显示了所需的情况。

3 个答案:

答案 0 :(得分:1)

好吧,我完全改变了这个答案。如果所需的对齐方式是垂直的,您可以将代码放在RelativeLayout中,它将_fill_parent_并且位于 theOneAtTheTop之下。在其中,您可以将想要的视图垂直居中对齐。

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

希望有所帮助

答案 1 :(得分:1)

我真的怀疑你可以直接在布局文件中这样做,因为xml布局不是这种条件逻辑的地方。您可以(我认为)使用布局文件的唯一真实情况是,如果您知道所涉及视图的确切尺寸以及应用程序的API级别为3.2,则会针对不同情况创建大量布局(我真的怀疑)。

如果你将RelativeLayout中的一个视图居中,它将无论什么都居中,它就没有在xml布局文件级别重叠视图的概念。在代码中是另一回事。

答案 2 :(得分: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>