2017-01-12 4 views
0

Я работаю над Android-приложением, где я хочу выровнять 2 линейных макета в строке с представлением изображения между этими двумя макетами. Я не могу использовать редактор макетов студии Android для достижения этой цели.Выравнивание 2 линейных макета и просмотр изображений внутри Относительная компоновка

Код для макета:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/white" 
    android:orientation="vertical"> 

    <RelativeLayout 
     android:id="@+id/nav_header_container" 
     android:layout_width="match_parent" 
     android:layout_height="160dp" 
     android:background="@color/colorPrimary"> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/rounded_corner" 
      android:orientation="vertical" 
      android:layout_marginEnd="17dp" 
      android:layout_centerVertical="true" 
      android:layout_alignParentEnd="true"> 
      <TextView 
      android:text="DEFG" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/textView8" 
      android:layout_gravity="center" 
      /> 
     <TextView 
      android:text="TextView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/textView16" 
      tools:text="1250" 
      android:textSize="30sp" 
      android:textColorLink="?attr/colorBackgroundFloating" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:background="@drawable/rounded_corner" 
      android:layout_marginEnd="30dp" 
      android:layout_toStartOf="@+id/profile_image"> 

      <TextView 
       android:text="ABCD" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/textView3" 
       android:layout_gravity="center" 
       /> 

      <TextView 
       android:text="TextView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/textView13" 
       tools:text="1250" 
       android:textSize="30sp" 
       android:textColorLink="?attr/colorBackgroundFloating" /> 
     </LinearLayout> 

     <ImageView 
      android:id="@+id/profile_image" 
      android:layout_width="60dp" 
      android:src="@drawable/face" 
      android:layout_height="60dp" 
      android:layout_centerHorizontal="true" /> 

    </RelativeLayout> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/drawerList" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="16dp"/> 

</LinearLayout> 

Выходной ток:

enter image description here

Ожидаемый результат:

2 линейных макетов в ряд с точки зрения изображения на центр относительной компоновки (с G Reen фон)

enter image description here

Я делаю что-то неправильно с расположением макета? Как реализовать такой сценарий?

+2

Кажется, вы новичок в андроиде развития, жаль, что я не буду отправлять ответ! Я хочу, чтобы вы играли с макетом и кодом xml, это поможет вам научиться этому отлично! –

+0

Вы должны использовать вес в линейном макете –

+0

@PareshMayani sir, Какой комментарий: p –

ответ

0

Изменение ширины RelativeLayout в LinearLayout (по горизонтали) набор для 0dp и вес 1, таким образом, они будут иметь одинаковую ширину. И использовать gravity положить вещи в center

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/white" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:id="@+id/nav_header_container" 
     android:layout_width="match_parent" 
     android:layout_height="160dp" 
     android:background="@color/colorPrimary"> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:background="@drawable/rounded_corner" 
      android:orientation="vertical" 
      android:layout_marginEnd="17dp" 
      android:layout_weight="1" 
      android:gravity="center"> 
      <TextView 
       android:text="DEFG" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/textView8" 
       android:layout_gravity="center" 
       /> 
      <TextView 
       android:text="TextView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/textView16" 
       tools:text="1250" 
       android:textSize="30sp" 
       android:textColorLink="?attr/colorBackgroundFloating" /> 
     </LinearLayout> 

     <ImageView 
      android:id="@+id/profile_image" 
      android:layout_width="0dp" 
      android:src="@drawable/rounded_corner" 
      android:layout_height="match_parent" 
      android:layout_centerHorizontal="true" 
      android:layout_weight="1" 
      android:layout_gravity="center" /> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:background="@drawable/rounded_corner" 
      android:layout_marginEnd="30dp" 
      android:layout_weight="1" 
      android:gravity="center"> 

      <TextView 
       android:text="ABCD" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/textView3" 
       android:layout_gravity="center" 
       /> 

      <TextView 
       android:text="TextView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/textView13" 
       tools:text="1250" 
       android:textSize="30sp" 
       android:textColorLink="?attr/colorBackgroundFloating" /> 
     </LinearLayout> 

    </LinearLayout> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/drawerList" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="16dp"/> 

</LinearLayout> 
0

Используя вес до вас LinearLayout может достичь этого.

Проверь этот код, но перед копированием оклейки узнать логику веса в LinearLayout:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/white" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:id="@+id/nav_header_container" 
     android:layout_width="match_parent" 
     android:layout_height="160dp" 
     android:background="#ffffff" 
     android:orientation="horizontal"> 

     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="4" 
      android:gravity="center" 
      android:orientation="vertical"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:src="@drawable/rounded_corner" /> 

      <TextView 
       android:id="@+id/textView8" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:text="DEFG" /> 

      <TextView 
       android:id="@+id/textView16" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/textView8" 
       android:layout_centerHorizontal="true" 
       android:text="TextView" 
       android:textColorLink="?attr/colorBackgroundFloating" 
       android:textSize="30sp" 
       tools:text="1250" /> 
     </RelativeLayout> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="center" 
      android:layout_weight="2" 
      android:gravity="center"> 

      <ImageView 
       android:id="@+id/profile_image" 
       android:layout_width="60dp" 
       android:layout_height="60dp" 
       android:layout_gravity="center" 
       android:src="@drawable/face" /> 
     </LinearLayout> 

     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="4" 
      android:gravity="center" 
      android:orientation="vertical"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:src="@drawable/rounded_corner" /> 

      <TextView 
       android:id="@+id/textView3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:text="ABCD" /> 

      <TextView 
       android:id="@+id/textView13" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/textView3" 
       android:layout_centerHorizontal="true" 
       android:text="TextView" 
       android:textColorLink="?attr/colorBackgroundFloating" 
       android:textSize="30sp" 
       tools:text="1250" /> 
     </RelativeLayout> 


    </LinearLayout> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/drawerList" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="16dp" /> 

</LinearLayout> 
Смежные вопросы