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

我有一个布局,需要放置在中心的意见之一。 这很容易。 问题是,我也有一些其他的意见,需要定位到屏幕的顶部。

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

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

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

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

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

Solutions Collecting From Web of "如果有足够的空间,请在布局的中心查看"

我真的怀疑你可以直接在布局文件中做这个,因为XML布局不是这种条件逻辑的地方。 你可以(我认为)使用布局文件的唯一的真实情况是,如果你知道所涉及的视图的确切尺寸,应用程序的API级别将是3.2 ,为不同的情况创build了很多布局(我真的怀疑)。

如果以RelativeLayout中的一个视图为中心,则无论如何它都将居中,它没有在xml布局文件级别覆盖视图的概念。 在代码中是另一回事。

那么,我完全改变这个答案。 如果所需的alignment是垂直的,你可以把你的代码放在一个RelativeLayout中,这个_Fill_parent_将会 OneAtTheTop 下面 。 在里面,你可以把你想要的视图垂直居中alignment。

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

希望能帮助到你

只需在第二个TextView中添加android:layout_below="@+id/theOneAtTheTop"即可。

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