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>

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

4

3 回答 3

1

I really doubt you can do this directly in the layout file as a xml layout is not the place for such conditional logic. The only real situation where you could(I think) use the layout file would be if you knew the exact dimensions of the views involved plus the API level of the app would be 3.2, creating a lot of layouts for different situations(which I really doubt).

If you center one of the views in a RelativeLayout it will be centered no matter what, it doesn't have the notion of overlaping views at the xml layout file level. In code is another thing.

于 2012-09-10T15:59:10.253 回答
1

好吧,我正在完全改变这个答案。如果所需的对齐方式是垂直的,您可以将代码放在相对布局中,该布局将 _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>

希望能帮助到你

于 2012-09-10T14:59:14.053 回答
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>
于 2012-09-10T15:05:53.093 回答