开发者

How do I center and Android view, while allowing to move away from other view it would overlap with?

开发者 https://www.devze.com 2023-01-23 21:22 出处:网络
I have two views, both of a specific width.I would like them laid out on a horizontal line as follows:

I have two views, both of a specific width. I would like them laid out on a horizontal line as follows:

  • View 1 should be centred.
  • View 2 is pinned to the right edge of the display.

However, if this arrangement would cause the views to overlap (ie. the screen is too narrow)

  • View 2 is pinned to the right edge of the display.
  • The right edge of View 1 sho开发者_JS百科uld touch the left edge of View 2

Can anyone think of a layout that can achieve this?

I've tried using a LinearLayout, but that results in one of two things: View 2 is compressed (with ugly text wrapping), or View 1 can't be centred.

A RelativeLayout appears to get closer to the solution, but again I have to choose between two poor solutions - either the views overlap or View 1 can't be centred.


This might have been solved by now.. but posting the solution(May not be optimized), hoping it will help someone. Here both the views are of same size(used the same image for both). As needed, those will not overlap on each other and will satisfy the position constraints also.

<ImageView
    android:id="@+id/ImageView_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/image"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/view_1" />
<RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_toRightOf="@id/ImageView_1">
    <ImageView
        android:id="@+id/ImageView_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image"
        android:layout_alignParentRight="true"
        android:contentDescription="@string/view_2" />
</RelativeLayout>

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号