how can i do a rounded toolbar/actionbar in android studio with code like this in the image? [closed]

大兔子大兔子 提交于 2020-05-30 07:00:01

问题


I'm trying to do this, with code and not with background image

here you can find the example image i'm referring in the question


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!