通過佈局就可以解決,下面貼出全部佈局:
<?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的左邊