【问题标题】:Android NavigationView vith rounded corners带圆角的 Android NavigationView
【发布时间】:2020-01-15 16:37:56
【问题描述】:

我正在Android上设计一个自定义抽屉,它的顶部和底部必须有圆角,我首先自定义顶部,我发现形状的背景不透明。

我有:

我需要构建:

我还想要一些关于如何在底部圆角的帮助

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>

【问题讨论】:

    标签: java android material-design android-navigationview material-components-android


    【解决方案1】:

    如果您在 Material Components 库中使用 NavigationView,您可以将自定义 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就有了圆角。
    现在您必须注意标题布局以在顶部构建一个圆角。您必须将其用作标题视图的背景,例如:

    <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 需要 1.1.0 版本的材料组件(目前为'com.google.android.material:material:1.1.0-alpha10'

    【讨论】:

      【解决方案2】:

      在 Kotlin 语言中创建一个类来进行扩展:

      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()
      

      【讨论】:

        【解决方案3】:

        添加左下放射线

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

        【讨论】:

        • 这样不行,因为它是headerView的背景,而不是整个navigationView的背景。
        【解决方案4】:

        如果不想使用MaterialShapeDrawable,下面的方法。 这也是我认为最好的方式。

        首先,调整导航视图背景的不透明度。 其次,在 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>
        

        【讨论】:

          猜你喜欢
          • 2020-09-12
          • 1970-01-01
          • 2012-09-12
          • 2014-09-02
          • 2016-08-06
          • 2014-02-01
          • 1970-01-01
          • 2018-06-23
          • 2021-03-11
          相关资源
          最近更新 更多