【问题标题】:Adding rounded corners to constraint layout leaves white background向约束布局添加圆角会留下白色背景
【发布时间】:2020-11-10 10:23:20
【问题描述】:

我正在为我的设计使用约束布局。我是约束布局的新手。我的根布局是约束布局,我的子布局也是约束布局。我已经为带有圆角的根布局添加了背景,但它在角落处留下了白色背景。下面是我的布局。此布局附加到片段。

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/background_black_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/res_bottomflat_topcorner_audiocontrols"
    app:layout_constraintBottom_toTopOf="@+id/background_grey_layout">


    <android.support.constraint.ConstraintLayout
        android:id="@+id/background_grey_layout"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_marginEnd="10dp"
        android:background="@drawable/res_grey_roundfilled_corner"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/background_black_layout"
        app:layout_constraintBottom_toBottomOf="parent"
        >

        <android.support.v7.widget.CardView
            android:id="@+id/album_art_card_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:elevation="2dp"
            app:cardCornerRadius="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/background_grey_layout">

            <ImageView
                android:id="@+id/album_art"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:contentDescription="@string/currently_playing_song"
                android:scaleType="fitXY"
                tools:src="@drawable/ic_launcher" />

        </android.support.v7.widget.CardView>


        <LinearLayout
            android:id="@+id/content"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toEndOf="@+id/album_art_card_view"
            app:layout_constraintTop_toTopOf="@+id/background_grey_layout"
            app:layout_constraintEnd_toStartOf="@+id/favorite_layout"
            >

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/currently_playing_song"
                android:ellipsize="end"
                android:fontFamily="@font/quicksand_medium"
                android:maxLines="1"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/main_text"
                android:textSize="12sp"
                tools:text="The Best Music EVAH1111 THE BEST" />

            <TextView
                android:id="@+id/artist"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/currently_playing_artist"
                android:ellipsize="end"
                android:fontFamily="@font/quicksand_regular"
                android:maxLines="1"
                android:text="A.R.Rehman"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="@color/sub_text"
                android:textSize="10sp" />

        </LinearLayout>

        <RelativeLayout
            android:id="@+id/favorite_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/play_pause"
            android:layout_marginEnd="20dp"
            app:layout_constraintTop_toTopOf="@+id/background_grey_layout"
            android:layout_marginStart="10dp">

            <CheckBox
                android:id="@+id/favorite"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:button="@null"
                android:clickable="false"
                android:drawableStart="@drawable/favourite_check_status"
                />
        </RelativeLayout>


        <ImageView
            android:id="@+id/play_pause"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/bg_player_play_button_home"
            android:contentDescription="@string/play_pause"
            android:src="@drawable/ic_pause"
            android:layout_marginEnd="20dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/background_grey_layout"
            app:layout_constraintBottom_toBottomOf="parent"
            />
    </android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

下面是我的可绘制文件。 res_bottomflat_topcorner_audiocontrols

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <!-- fill/color -->
    <solid
        android:color="#000000"/>
    <corners
        android:topLeftRadius="30dp"
        android:topRightRadius="30dp"
        />

</shape>

下面给出的图像是我得到的输出。

我需要的如下所示。

我不知道我在哪里失踪。我进行了很多搜索,但没有找到与此相关的任何答案。需要帮忙。提前致谢。

【问题讨论】:

    标签: android android-drawable android-constraintlayout rounded-corners


    【解决方案1】:

    您错过的事情是您必须将所需的背景颜色放入根视图 发生这种情况的另一个原因可能是您的回收器视图(假设您必须使用回收器)位于该音乐播放视图的上方,它应该位于底部导航的上方

    【讨论】:

    • 我已经在我的根布局中添加了背景。背景是圆角的可绘制文件,回收站视图位于底部导航上方
    • 你应该已经为相应的activity.xml的根视图添加了一个背景颜色你为底部导航视图和上面的recyclerview添加了一个背景,白色来自活动默认背景。
    【解决方案2】:

    科特林

      MyDialog.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    

    安卓

    MyDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    

    【讨论】:

      【解决方案3】:

      在生气了2天后,我终于找到了它。 它无法通过任何可绘制文件或通过编辑画布来解决,而只能通过将布局作为基本布局来解决。

      值下有几种方法和内容,您必须选择正确的并将其添加到您的样式中。

      首先将这些添加到您的styles.xml

      <style name="AppBottomSheetDialogTheme" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
          <item name="bottomSheetStyle">@style/CustomBottomSheetStyle</item>
      </style>
      
      <style name="CustomBottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">
          <item name="android:background">@drawable/drawable_round_bottom_sheet</item>
      </style>
      

      现在最重要的部分是为此调用 AppBottomDialogue 我使用了这个

       override fun getTheme(): Int {
          return R.style.AppBottomSheetDialogTheme
      }
      

      现在你可以选择用你的方法做什么了

      如果你想知道这里是

      首先我创建了这两个可绘制对象,因为我必须在白天和夜间模式之间切换

      day.xml

          <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle">
          <solid android:color="@android:color/white"/>
          <corners android:topLeftRadius="16dp"
              android:topRightRadius="16dp"
              android:bottomLeftRadius="16dp"
              android:bottomRightRadius="16dp"/>
      
      </shape>
      

      然后是 night.xml

          <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle">
          <solid android:color="@color/night"/>
          <corners android:topLeftRadius="16dp"
              android:topRightRadius="16dp"
              android:bottomLeftRadius="16dp"
              android:bottomRightRadius="16dp"/>
      
      </shape>
      

      然后我将它们放在方法中 parent1 是 imageview 的 id,如果你想知道的话

      if (isNightMode) {
              parent1.setImageResource(R.drawable.round_night)
      
          } else {
              parent1.setImageResource(R.drawable.round_day)
          }
      

      【讨论】:

        猜你喜欢
        • 2019-01-22
        • 2011-12-02
        • 1970-01-01
        • 2020-08-30
        • 2016-11-29
        • 2021-02-24
        • 1970-01-01
        • 2013-03-09
        • 2019-02-21
        相关资源
        最近更新 更多