Сделай так чтобы картинка занимала 100% от ширины и 60% от выоты и была на фоне текста и…
- User
Сделай так чтобы картинка занимала 100% от ширины и 60% от выоты и была на фоне текста и помни что этот код для AndroidStudio : <androidx.cardview.widget.CardView xmlns:android=“http://schemas.android.com/apk/res/android” android:layout_width=“match_parent” android:layout_height=“wrap_content” xmlns:app=“http://schemas.android.com/apk/res-auto” android:layout_margin=“10dp” android:elevation=“6dp” app:cardCornerRadius=“8dp”>
<RelativeLayout android:layout_width=“match_parent” android:layout_height=“wrap_content” android:padding=“10dp”
<TextView android:id=“@+id/discount” android:layout_width=“match_parent” android:layout_height=“wrap_content” android:text=“Товар подешевел на 30%” android:textSize=“18sp” android:textStyle=“bold” />
<ImageView android:id=“@+id/product_image” android:layout_width=“match_parent” android:layout_height=“0dp” android:layout_below=“@id/discount” android:src=“@drawable/food_1” />
<TextView android:id=“@+id/product_name” android:layout_width=“match_parent” android:layout_height=“0dp” android:layout_below=“@id/product_image” android:layout_weight=“2” android:gravity=“center_vertical” android:text=“Product Name” android:textSize=“22sp” android:textStyle=“bold” />
<TextView android:id=“@+id/product_description” android:layout_width=“match_parent” android:layout_height=“0dp” android:layout_below=“@id/product_name” android:layout_weight=“1” android:text=“Product Description” android:textSize=“16sp” />
<TextView android:id=“@+id/old_price” android:layout_width=“wrap_content” android:layout_height=“wrap_content” android:layout_below=“@id/product_description” android:layout_alignParentStart=“true” android:layout_marginTop=“10dp” android:text=“Old Price” android:textColor=“@android:color/darker_gray” android:textSize=“16sp” />
<TextView android:id=“@+id/new_price” android:layout_width=“wrap_content” android:layout_height=“wrap_content” android:layout_alignBaseline=“@id/old_price” android:layout_marginStart=“20dp” android:layout_toEndOf=“@id/old_price” android:text=“New Price” android:textColor=“#EC0909” android:textSize=“16sp” />
<Button android:id=“@+id/goRestaurantButton” android:layout_width=“wrap_content” android:layout_height=“wrap_content” android:layout_below=“@id/new_price” android:layout_alignParentEnd=“true” android:layout_marginTop=“5dp” android:layout_marginEnd=“9dp” android:text=“Перейти в ресторан” />
</androidx.cardview.widget.CardView>
- Assistant
Чтобы картинка занимала 100% от ширины и 60% от высоты, замените следующую строку:
на эту:
Чтобы фон был под картинкой, удалите или закомментируйте эту строку:
android:padding="10dp"
- User
продолжай
- Assistant
Когда вы удалите или закомментируете строку
android:padding="10dp", ваша разметка будет выглядеть так:<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_margin="10dp" android:elevation="6dp" app:cardCornerRadius="8dp">
</androidx.cardview.widget.CardView>
Теперь картинка займет 60% высоты и будет на фоне текста, а не под текстом.
- User
картинку все еще не видно на фоне
- Assistant
Для того, чтобы сделать картинку на фоне текста, необходимо внести изменения в разметку. Вам необходимо добавить background с нужным цветом элементу RelativeLayout. Например, чтобы сделать его белым, используйте следующий код:
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white">При этом вам также необходимо будет создать файл цветов, где вы определите цвет, который будет использоваться в качестве фона:
<resources> <color name="white">#FFFFFF</color> </resources>Пример полной разметки с этими изменениями может выглядеть так:
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_margin="10dp" android:elevation="6dp" app:cardCornerRadius="8dp"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:padding="10dp"> <TextView android:id="@+id/discount" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Товар подешевел на 30%" android:textSize="18sp" android:textStyle="bold" /> <ImageView android:id="@+id/product_image" android:layout_width="match_parent" android:layout_height="0dp" android:layout_below="@id/discount" android:layout_weight="0.6" android:src="@drawable/food_1" /> <TextView android:id="@+id/product_name" android:layout_width="match_parent" android:layout_height="0dp" android:layout_below="@id/product_image" android:layout_weight="2" android:gravity="center_vertical" android:text="Product Name" android:textSize="22sp" android:textStyle="bold" /> <TextView android:id="@+id/product_description" android:layout_width="match_parent" android:layout_height="0dp" android:layout_below="@id/product_name" android:layout_weight="1" android:text="Product Description" android:textSize="16sp" /> <TextView android:id="@+id/old_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/product_description" android:layout_alignParentStart="true" android:layout_marginTop="10dp" android:text="Old Price" android:textColor="@android:color/darker_gray" android:textSize="16sp" /> <TextView android:id="@+id/new_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@id/old_price" android:layout_marginStart="20dp" android:layout_toEndOf="@id/old_price" android:text="New Price" android:textColor="#EC0909" android:textSize="16sp" /> <Button android:id="@+id/goRestaurantButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/new_price" android:layout_alignParentEnd="true" android:layout_marginTop="5dp" android:layout_marginEnd="9dp" android:text="Перейти в ресторан" /> </RelativeLayout> </androidx.cardview.widget.CardView>Обратите внимание, что в файле цветов содержится только один цвет - белый. Если вам нужно использовать другой цвет для фона, вы можете его определить в соответствующее имя.