[英]Set background with gravity without resizing the container View in Android
如何在不調整大小的情況下向視圖添加背景,而僅顯示適合上一空間的圖像部分?
例:
我有以下初始布局:
<?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="#8900"
android:orientation="vertical"
tools:context="com.tappx.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#0f0"
android:gravity="center"
android:text="HELLO" />
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#f0f"
android:gravity="center"
android:text="HELLO" />
</LinearLayout>
</LinearLayout>
我想將以下圖像作為漸變應用於白色背景:
為此(請注意,漸變填充水平並且具有底部重力):
我的問題是將漸變設置為android:background
會將容器視圖大小擴展為此(丟失紅色底部)
還嘗試制作沒有結果的xml位圖:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:gravity="bottom"
android:src="@drawable/gradient" />
</item>
</layer-list>
我該如何實現?
您使用的圖像很長,並且您的容器設置為包裝內容,因此正在擴展以容納該圖像。
由於您已經在內部元素上使用了固定的高度,因此您只需為容器設置固定的高度(200dp),它就會起作用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.