Относительный фон макета не заполняет все пространство

#android #background #padding #android-relativelayout

#Android #фон #заполнение #android-relativelayout

Вопрос:

у меня есть относительный макет с фоновым изображением, но я хочу, чтобы изображение заполняло все пространство макета, я думаю, это проблема с заполнением, я установил значение -1dip, это не работает. Также перед установкой моего заполнения на -1dip, когда я проверяю заполнение с помощью hierachy viewer. вот мой xml

 <RelativeLayout
        android:id="@ id/relativeLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@ id/gridView"
        android:background="@drawable/tool_bar"
        android:gravity="center"
       >

        <ImageView
            android:id="@ id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/locate" />

        <ImageView
            android:id="@ id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@ id/imageView1"
            android:src="@drawable/orbit" />

        <ImageView
            android:id="@ id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@ id/imageView2"
            android:src="@drawable/search" />

        <ImageView
            android:id="@ id/imageView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@ id/imageView3"
            android:src="@drawable/snap" />

        <ImageView
            android:id="@ id/imageView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@ id/imageView4"
            android:src="@drawable/profile" />
    </RelativeLayout>
thanks
  

Ответ №1:

Я столкнулся с той же проблемой. Я думаю, что по умолчанию RelativeLayout не будет расширять фон до его размеров.

В итоге я использовал представление внутри следующим образом:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300px"
android:layout_height="204px">
    <View 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"       
    android:background="@android:color/white"/> //Or whatever background you pick
</RelativeLayout>
  

Не то решение, на которое я надеялся, но в любом случае решение.

Ответ №2:

если вы хотите заполнить все пространство по ширине и высоте, используйте FILL_PARENT или, если вы хотите заполнить один из них, например, заполнить width, а не height, тогда установите height как wrap_content или, если вы хотите заполнить height, а не width, тогда установите wrap_content в width

как и здесь, он заполнит оба

 <RelativeLayout
        android:id="@ id/relativeLayout2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@ id/gridView"
        android:background="@drawable/tool_bar"
        android:gravity="center"
       >
</RelativeLayout>
  

Комментарии:

1. я хочу, чтобы фоновое изображение заполняло относительное пространство макета. не относительный макет, чтобы заполнить весь экран

Ответ №3:

  <RelativeLayout
    android:id="@ id/relativeLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/tool_bar"
   >  
  

Должно сработать, я полагаю

Ответ №4:

Я думаю, вы хотите, чтобы изображение было заполнено относительным макетом. Вы попробуйте. Это может вам помочь. fill_parent

 <ImageView
            android:id="@ id/any_image"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/profile_you" />