问题
I'm trying to do this, with code and not with background image
回答1:
Define xml file like this (this is how i rounded my botton) put the xml file in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<corners
android:radius="25dip" />
<stroke
android:width="1dp"
android:color="#000000" />
</shape>
and put the xml file in the background attribute
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dateButton"
android:background="@drawable/shape"
></Button>
So maybe the same it works with toolbar ?
回答2:
You can use the MaterialToolbar
in the Material Components Library:
In the layout use something like:
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
style="@style/Widget.MaterialComponents.Toolbar.Primary"
.../>
Then apply a MaterialShapeDrawable
float radius = getResources().getDimension(R.dimen....);
MaterialShapeDrawable toolbarBackground = (MaterialShapeDrawable) toolbar.getBackground();
toolbarBackground.setShapeAppearanceModel(
toolbarBackground.getShapeAppearanceModel()
.toBuilder()
.setBottomRightCorner(CornerFamily.ROUNDED,radius)
.setBottomLeftCorner(CornerFamily.ROUNDED,radius)
.build()
);
来源:https://stackoverflow.com/questions/59221483/how-can-i-do-a-rounded-toolbar-actionbar-in-android-studio-with-code-like-this-i