最佳答案
不要只使用带有 gradient
的 shape
,而是使用带有指定的 2 个 gradients
的 layer-list
每个高度
API 21
android:bottom
, android:top
属性表示层列表的填充,更改这些以更改要显示的渐变百分比
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:bottom="690dp"
android:gravity="top">
<shape android:shape="rectangle" >
<gradient
android:angle="270"
android:type="linear"
android:endColor="@android:color/transparent"
android:startColor="#000000" />
</shape>
</item>
<item
android:top="610dp"
android:gravity="bottom">
<shape android:shape="rectangle" >
<gradient
android:angle="270"
android:type="linear"
android:endColor="#000000"
android:startColor="@android:color/transparent" />
</shape>
</item>
如果您的 minSDK
是 23+,那么为每个图层类型指定 height
会更容易
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:height="40dp"
android:gravity="top">
<shape android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="@android:color/transparent"
android:startColor="#000000"
android:type="linear" />
</shape>
</item>
<item
android:height="70dp"
android:gravity="bottom">
<shape android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="#000000"
android:startColor="@android:color/transparent"
android:type="linear" />
</shape>
</item>
然后将其应用为 ViewPager
的 background
试试这个,希望这能解决你的问题
关于java - 如何在顶部、底部和透明中心对 View 进行渐变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62891214/