猿问

Android-按钮边框

如何在按钮上添加边框?是否可以不使用图像来做到这一点?



烙印99
浏览 647回答 3
3回答

HUH函数

&nbsp;Android Official Solution自从引入Android Design支持v28以来,使用即可轻松创建带边框的按钮MaterialButton。此类为构造函数中的按钮提供更新的Material样式。使用app:strokeColor和app:strokeWidth您可以创建自定义边框,如下所示:1.使用时androidx:build.gradledependencies {&nbsp; &nbsp; implementation 'androidx.appcompat:appcompat:1.1.0'&nbsp; &nbsp; implementation 'com.google.android.material:material:1.0.0'}•带边框的按钮:<com.google.android.material.button.MaterialButton&nbsp; &nbsp; style="@style/Widget.AppCompat.Button.Colored"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:text="MATERIAL BUTTON"&nbsp; &nbsp; android:textSize="15sp"&nbsp; &nbsp; app:strokeColor="@color/green"&nbsp; &nbsp; app:strokeWidth="2dp" />•未填充边框按钮:<com.google.android.material.button.MaterialButton&nbsp; &nbsp; style="@style/Widget.AppCompat.Button.Borderless"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:text="UNFILLED MATERIAL BUTTON"&nbsp; &nbsp; android:textColor="@color/green"&nbsp; &nbsp; android:textSize="15sp"&nbsp; &nbsp; app:backgroundTint="@color/transparent"&nbsp; &nbsp; app:cornerRadius="8dp"&nbsp; &nbsp; app:rippleColor="#33AAAAAA"&nbsp; &nbsp; app:strokeColor="@color/green"&nbsp; &nbsp; app:strokeWidth="2dp" />2.使用时appcompat:build.gradledependencies {&nbsp; &nbsp; implementation 'com.android.support:design:28.0.0'}style.xml确保您的应用程序主题继承自Theme.MaterialComponents而不是Theme.AppCompat。<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">&nbsp; &nbsp; <!-- Customize your theme here. --></style>•带边框的按钮:<android.support.design.button.MaterialButton&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:text="MATERIAL BUTTON"&nbsp; &nbsp; android:textSize="15sp"&nbsp; &nbsp; app:strokeColor="@color/green"&nbsp; &nbsp; app:strokeWidth="2dp" />•未填充边框按钮:<android.support.design.button.MaterialButton&nbsp; &nbsp; style="@style/Widget.AppCompat.Button.Borderless"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:text="UNFILLED MATERIAL BUTTON"&nbsp; &nbsp; android:textColor="@color/green"&nbsp; &nbsp; android:textSize="15sp"&nbsp; &nbsp; app:backgroundTint="@color/transparent"&nbsp; &nbsp; app:cornerRadius="8dp"&nbsp; &nbsp; app:rippleColor="#33AAAAAA"&nbsp; &nbsp; app:strokeColor="@color/green"&nbsp; &nbsp; app:strokeWidth="2dp" />
随时随地看视频慕课网APP

相关分类

Android
我要回答