Android 带有圆角的 NavigationView
Android NavigationView vith rounded corners
我正在Android上设计一个自定义抽屉,它的顶部和底部必须有圆角,我是首先自定义顶部,我发现 形状的背景不透明 .
我有:
(来源:toile-libre.org)
我需要构建:
(来源:toile-libre.org)
我还需要一些关于如何在底部将其四舍五入的帮助
nav_header_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="wrap_content"
android:contentDescription="@string/nav_header_desc"
android:paddingTop="@dimen/nav_header_vertical_spacing"
app:srcCompat="@mipmap/ic_launcher_round" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="@string/nav_header_title"
android:textColor="@color/colorWhite"
android:textAppearance="@style/TextAppearance.AppCompat.Headline" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorWhite"
android:text="@string/nav_header_subtitle" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
side_nav_bar.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:width="3dp"
android:color="@color/colorPrimary"
/>
<corners android:radius="1dp"
android:bottomRightRadius="0dp" android:bottomLeftRadius="0dp"
android:topLeftRadius="30dp" android:topRightRadius="0dp"/>
</shape>
添加左下角半径
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:width="3dp"
android:color="@color/colorPrimary"
/>
<corners android:radius="1dp"
android:bottomRightRadius="0dp" android:bottomLeftRadius="30dp"
android:topLeftRadius="30dp" android:topRightRadius="0dp"/>
</shape>
如果您使用 NavigationView
in the Material Components library, you can apply a custom ShapeAppearanceModel
到 NavigationView
的角落。
类似于:
float radius = getResources().getDimension(R.dimen.roundcorner);
NavigationView navigationView = findViewById(R.id.nav_view);
MaterialShapeDrawable navViewBackground = (MaterialShapeDrawable) navigationView.getBackground();
navViewBackground.setShapeAppearanceModel(
navViewBackground.getShapeAppearanceModel()
.toBuilder()
.setTopRightCorner(CornerFamily.ROUNDED,radius)
.setBottomRightCorner(CornerFamily.ROUNDED,radius)
.build());
这样 NavigationView
就有了圆角。
现在你要注意 header 布局在顶部建立一个圆角。您必须将 header 视图用作背景,例如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/side_nav_bar"
...>
side_nav_bar 是
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
....
<corners android:topRightRadius="32dp" />
</shape>
不要在底部使用圆角,因为它只是在 NavigationView 的 header 上使用的视图。
ShapeAppearanceModel
需要 material 个组件的版本 1.1.0(目前 'com.google.android.material:material:1.1.0-alpha10'
)
在 Kotlin 语言中创建一个 class 来进行扩展:
fun MaterialNavigationView.changeCornerRadius() {
val navViewBackground : MaterialShapeDrawable = background as MaterialShapeDrawable
val radius = resources.getDimension(R.dimen.menu_radius)
navViewBackground.shapeAppearanceModel = navViewBackground.shapeAppearanceModel
.toBuilder()
.setTopLeftCorner(CornerFamily.ROUNDED, radius)
.setBottomLeftCorner(CornerFamily.ROUNDED, radius)
.build()
}
用法:
val materialNavigationView: MaterialNavigationView = findViewById(R.id.material_navigation_view)
materialNavigationView.changeCornerRadius()
如果你不想使用MaterialShapeDrawable,下面的方法。
这也是我认为最好的方式。
首先,调整导航视图背景的不透明度。
二、Round the layout inside Navigation View
❖ 但是,注意上下视图的背景。
layout_gnb.xml
<com.google.android.material.navigation.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/NAVIGATION_VIEW"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000">
<LinearLayout
android:id="@+id/LINEAR_LAYOUT_03"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/rounded_right_corner_light_white_rectangle">
...
</LinearLayout>
</com.google.android.material.navigation.NavigationView>
rounded_right_corner_light_white_rectangle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:topRightRadius="30dp"
android:bottomRightRadius="30dp" />
<solid android:color="@color/colorLightWhite" />
</shape>
我正在Android上设计一个自定义抽屉,它的顶部和底部必须有圆角,我是首先自定义顶部,我发现 形状的背景不透明 .
我有:
(来源:toile-libre.org)
我需要构建:
(来源:toile-libre.org)
我还需要一些关于如何在底部将其四舍五入的帮助
nav_header_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="wrap_content"
android:contentDescription="@string/nav_header_desc"
android:paddingTop="@dimen/nav_header_vertical_spacing"
app:srcCompat="@mipmap/ic_launcher_round" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="@string/nav_header_title"
android:textColor="@color/colorWhite"
android:textAppearance="@style/TextAppearance.AppCompat.Headline" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorWhite"
android:text="@string/nav_header_subtitle" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
side_nav_bar.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:width="3dp"
android:color="@color/colorPrimary"
/>
<corners android:radius="1dp"
android:bottomRightRadius="0dp" android:bottomLeftRadius="0dp"
android:topLeftRadius="30dp" android:topRightRadius="0dp"/>
</shape>
添加左下角半径
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:width="3dp"
android:color="@color/colorPrimary"
/>
<corners android:radius="1dp"
android:bottomRightRadius="0dp" android:bottomLeftRadius="30dp"
android:topLeftRadius="30dp" android:topRightRadius="0dp"/>
</shape>
如果您使用 NavigationView
in the Material Components library, you can apply a custom ShapeAppearanceModel
到 NavigationView
的角落。
类似于:
float radius = getResources().getDimension(R.dimen.roundcorner);
NavigationView navigationView = findViewById(R.id.nav_view);
MaterialShapeDrawable navViewBackground = (MaterialShapeDrawable) navigationView.getBackground();
navViewBackground.setShapeAppearanceModel(
navViewBackground.getShapeAppearanceModel()
.toBuilder()
.setTopRightCorner(CornerFamily.ROUNDED,radius)
.setBottomRightCorner(CornerFamily.ROUNDED,radius)
.build());
这样 NavigationView
就有了圆角。
现在你要注意 header 布局在顶部建立一个圆角。您必须将 header 视图用作背景,例如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/side_nav_bar"
...>
side_nav_bar 是
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
....
<corners android:topRightRadius="32dp" />
</shape>
不要在底部使用圆角,因为它只是在 NavigationView 的 header 上使用的视图。
ShapeAppearanceModel
需要 material 个组件的版本 1.1.0(目前 'com.google.android.material:material:1.1.0-alpha10'
)
在 Kotlin 语言中创建一个 class 来进行扩展:
fun MaterialNavigationView.changeCornerRadius() {
val navViewBackground : MaterialShapeDrawable = background as MaterialShapeDrawable
val radius = resources.getDimension(R.dimen.menu_radius)
navViewBackground.shapeAppearanceModel = navViewBackground.shapeAppearanceModel
.toBuilder()
.setTopLeftCorner(CornerFamily.ROUNDED, radius)
.setBottomLeftCorner(CornerFamily.ROUNDED, radius)
.build()
}
用法:
val materialNavigationView: MaterialNavigationView = findViewById(R.id.material_navigation_view)
materialNavigationView.changeCornerRadius()
如果你不想使用MaterialShapeDrawable,下面的方法。 这也是我认为最好的方式。
首先,调整导航视图背景的不透明度。 二、Round the layout inside Navigation View
❖ 但是,注意上下视图的背景。
layout_gnb.xml
<com.google.android.material.navigation.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/NAVIGATION_VIEW"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000">
<LinearLayout
android:id="@+id/LINEAR_LAYOUT_03"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/rounded_right_corner_light_white_rectangle">
...
</LinearLayout>
</com.google.android.material.navigation.NavigationView>
rounded_right_corner_light_white_rectangle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:topRightRadius="30dp"
android:bottomRightRadius="30dp" />
<solid android:color="@color/colorLightWhite" />
</shape>