2014-12-16 3 views
2

У меня есть несколько видов (рассмотрим образы). Одно изображение (большее) должно быть центрировано по горизонтали. Другой вид (меньше) должен быть слева от большего вида, но он должен быть расположен по горизонтали в пространстве слева от большего вида.Выравнивать центр обзора по горизонтали относительно другого вида

Одно огромное ограничение: все эти виды должны находиться внутри одного и того же макета (я думаю, что относительный макет подходит для этого случая).

enter image description here

Но я не могу добиться того, что мне нужно. Любая помощь очень ценится.

ответ

0

Кажется, как это может быть сделано только программно.

Основная идея:

  • Создать FrameLayout и поставить все необходимые взгляды внутри него.
  • Рассчитать абсолютное положение на экране для каждого вида (положение левого верхнего угла для каждого вида). Для большого вида это будет что-то вроде: left = (screenWidth - viewWidth)/2, right = (screenHeight - viewHeight)/2 (возможно, также должен учитываться высота statusBar).
  • Поместите каждый вид в его положение, установив FrameLayout.LayoutParams с определенными leftMargin, topMargin - верхняя и левая координаты.
0

Попробуйте XML ...

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" > 

     <View 
      android:id="@+id/smallView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" /> 

     <View 
      android:id="@+id/smallView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" /> 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" > 

     <View 
      android:id="@+id/bigView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" /> 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" > 

     <View 
      android:id="@+id/smallView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" /> 

     <View 
      android:id="@+id/smallView4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" /> 

    </RelativeLayout> 

</LinearLayout> 
+0

Спасибо за вашу попытку. Более понятно использовать для внутренних относительных макетов «выровнять родительский левый» и «слева» от центра. К сожалению, мне нужно, чтобы все представления были в одном макете. Похоже, я могу сделать это только программно – krossovochkin