通过布局就可以解决,下面贴出全部布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<com.lz.zergling.widget.banner.CustomBanner
android:id="@+id/banner_header"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="h,375:160"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
注:
1、com.lz.zergling.widget.banner.CustomBanner是我自己引用的banner,可以随意引用自己用习惯的banner。
我用的是:implementation ‘com.youth.banner:banner:1.4.10’
2、app:layout_constraintDimensionRatio="h,375:160"的意思就是以宽度为基础去设置高度,宽高比是:375:160
3、app:layout_constraintDimensionRatio的值里面的h和w是什么意思。一般来说,加上h的意思就是,h之后的比例是以w为基础去设置h,即h = w * ratio。反之,写上w的意思是,w = h / ratio (因为 ratio = w / h 代表宽高比)。
4、 app:layout_constraintLeft_toLeftOf="parent"的意思是view会贴着父view的左边