0

я наткнулся на странный вопрос, ниже мой макет:Android layout_below в RelativeLayout

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
> 

<ImageView 
    android:src="@color/colorAccent" 
    android:layout_width="match_parent" 
    android:layout_height="200dp"/> 
<ImageView 
    android:id="@+id/image" 
    android:src="@mipmap/ic_launcher" 
    android:layout_centerInParent="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

<TextView 
    android:text="text" 
    android:layout_below="@+id/image" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
</RelativeLayout> 

enter image description here

TextView не ниже ImageView, как и ожидалось, это ниже исходного положения ImageView.

Если я установил layout_height из RelativeLayout в 200dp или match_parent, он отлично работает.

Как это решить?

+0

Ну, это относительная схема.Вам нужно указать, где каждый элемент будет относиться к другому. Чтобы выровнять все элементы в строке или столбце, используйте LinearLayout – Imdad

+0

, почему вы добавили первый вид изображения? Если вы хотите изменить цвет фона макета, есть другой способ. –

+0

Используйте match_parent вместо wrap_content для корневого макета. –

ответ

0

Простым решением для вас является использование android:layout_height="match_parent" в корневой компоновке.

Если вы используете wrap_content для вашего корневого макета, android:layout_centerInParent и android:layout_below не будут работать должным образом.

Либо использовать высоту как match_parent или статический.

выбор 1:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="200dp"> 

выбор 2:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

выбор 3:

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:orientation="vertical"> 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="text" /> 
</LinearLayout> 

Теперь его ДО использовать которые так lution.

0

Если вы хотите, чтобы ваши Relative Layout к Wrap Content и вашей второй Image view Наносить layout_centerInParent, то вам нужно дать marginTop к Text View.

Прошу указать код.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 


    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:src="?attr/colorPrimary" /> 

    <ImageView 
     android:id="@+id/imageTest" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:src="@mipmap/ic_launcher" /> 

    <TextView 
     android:id="@+id/tvText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/imageTest" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="80dp" 
     android:text="text" 
     android:textSize="22sp" /> 


</RelativeLayout> 

Вот снимок экрана.

enter image description here

+0

Спасибо за помощь. – jfxu

+0

@xujifa Пожалуйста, поддержите и примите ответ, если он вам полезен. благодаря :). –

0

Спасибо за все ответил. У меня наконец есть решение. Это не очень хорошее решение, но оно отлично работает.

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

<ImageView 
    android:id="@+id/background" 
    android:src="@color/colorAccent" 
    android:layout_width="match_parent" 
    android:layout_height="200dp"/> 
<RelativeLayout 
    android:layout_height="warp_content" 
    android:layout_width="match_parent" 
    android:layout_alignTop="@+id/background" 
    android:layout_alignBottom="@+id/background"> 
    <ImageView 
     android:id="@+id/image" 
     android:src="@mipmap/ic_launcher" 
     android:layout_centerInParent="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <TextView 
     android:text="text" 
     android:layout_below="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</RelativeLayout> 
</RelativeLayout> 
0

Верхний ответ на самом деле необязателен. Вам не нужно указывать элементу верхний край, чтобы он находился ниже изображения. В моей ситуации у меня есть RelativeLayout с ImageView и ListView. Я хотел, чтобы ImageView был выше ListView, но ListView покрывал область под ImageView. И я достиг этого, установив layout_alignParentTop в ImageView и добавив layout_below в ListView.

<RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/activity_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     tools:context="io.menuloop.menuloopandroid.MainActivity"> 
    <ImageView android:layout_alignParentTop="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/yourStory" android:src="@drawable/ic_face_black_24dp" /> 
    <ListView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/yourStory" 
      android:layout_centerHorizontal="true" android:id="@+id/listView" /> 
</RelativeLayout> 
Смежные вопросы