在我們項目開發(fā)中微王,有許多時候需要給背景設(shè)置圓角施蜜,顏色等場景赦邻。常見的做法是在drawable中設(shè)置對應(yīng)的背景江兢,但是如果項目中頻繁用到,并且只是顏色變化读规,那樣會很麻煩抓督。
那么廢話不說,我們先引入SupertextView
在項目的build.gradle里添加
implementation 'com.github.lygttpod:SuperTextView:2.4.6'
引入后會有這么多控件可供選擇
用法很簡單
...
<com.allen.library.shape.ShapeConstraintLayout 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="wrap_content"
app:shapeCornersRadius="@dimen/dp12"
app:shapeSolidColor="@color/white"
android:padding="20dp">
<TextView
android:id="@+id/tv_title"
style="@style/DialogTitleTextStyle"
android:layout_width="match_parent"
android:gravity="center"
android:lineHeight="28dp"
app:layout_constraintTop_toTopOf="parent"
tools:text="標題" />
<TextView
android:id="@+id/tv_content"
style="@style/LightBlackTextStyle"
android:layout_width="match_parent"
android:layout_marginTop="12dp"
android:gravity="center"
android:lineHeight="22dp"
android:textColor="@color/black"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_title"
tools:text="對話框內(nèi)容束亏,超長可換行" />
<com.allen.library.shape.ShapeButton
android:id="@+id/btn_left"
android:layout_height="@dimen/huge_36"
android:layout_width="0dp"
android:gravity="center"
android:layout_marginTop="24dp"
android:layout_marginEnd="@dimen/dp12"
app:shapeSolidColor="@color/white"
app:shapeCornersRadius="@dimen/dp12"
app:shapeStrokeColor="@color/accentBlue"
app:shapeStrokeWidth="1dp"
android:text="取消"
app:layout_constraintEnd_toStartOf="@+id/btn_right"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_content"
app:layout_constraintWidth_percent="0.45" />
<com.allen.library.shape.ShapeButton
android:gravity="center"
android:id="@+id/btn_right"
android:layout_height="@dimen/huge_36"
android:layout_width="0dp"
android:layout_marginTop="24dp"
android:text="確認"
android:textColor="@color/white"
app:shapeSolidColor="@color/accentBlue"
app:shapeCornersRadius="@dimen/dp12"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/btn_left"
app:layout_constraintTop_toBottomOf="@+id/tv_content"
app:layout_constraintWidth_percent="0.45" />
</com.allen.library.shape.ShapeConstraintLayout>
...
重點就是通過屬性來配置背景
這點和drawable文件的設(shè)置是一一對應(yīng)的铃在。
所以沒有什么學(xué)習(xí)成本
我們看下效果
不需要額外引用了,效果很直觀