【问题标题】:Rounded linear layout leaving extra space on corners圆角线性布局在角落留下额外空间
【发布时间】:2017-01-04 09:49:46
【问题描述】:

我正在创建一个带有一些按钮和文本视图的圆形线性布局,我使用了一个自定义选择器作为布局的背景。我的问题是布局在角落有额外的空间如何删除它?任何帮助将不胜感激 这是我的代码

<?xml version="1.0" encoding="utf-8"?>
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" android:padding="10dp"
    >
    <solid android:color="#FFFFFF"/>
    <corners
        android:bottomRightRadius="15dp"
        android:bottomLeftRadius="15dp"
        android:topLeftRadius="15dp"
        android:topRightRadius="15dp"
        />
    <stroke android:width="2dip"
            android:color="@color/#275D69"/>
</shape>

布局:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/selector"
    />

当前输出:

不想要我放置红色标记的那个空间。

【问题讨论】:

  • 该线性布局是否包含在另一个布局中?发布整个 XML 布局文件
  • 不是父布局
  • 放圆角半径。
  • 你会如何填写?如果角落是圆的,你必须使用后面的另一个视图或 xml 中的另一个项目。你不能圆屏幕:P
  • 圆角布局不适合带角的屏幕。您将不得不将手机四舍五入以删除该空间

标签: android xml android-linearlayout


【解决方案1】:

使用下面的代码,您的背景是圆角的,角落的空间最小

 <?xml version="1.0" encoding="utf-8"?>
   <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:padding="10dp"
      android:shape="rectangle">
    <solid android:color="#FFFFFF" />
    <corners android:radius="5dp" />
    <stroke
      android:width="2dip"
      android:color="#275D69" />
   </shape>

这可能包括角落的空间,但它太低了。

只需减少radius 的值。

【讨论】:

    【解决方案2】:

    尝试使用这种方法,不是最明确的解决方案,但对我来说有效:

    创建一个具有透明背景的 Layout 容器,并将其用作圆形布局的容器

    <LinearLayout
    .....
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@android:color/transparent"
    ......>
    
      <LinearLayout
    
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/selector"
        android:orientation="vertical">
    
         <!-- content of the rounded ll -->
       </LinearLayout>
    </LinearLayout>
    

    空格应该消失

    【讨论】:

      【解决方案3】:

      这是您需要遵循的解决方案:

      <?xml version="1.0" encoding="utf-8"?>
         <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle" android:padding="10dp">
      
            <solid android:color="#FFFFFF"/>
                <corners
                  android:bottomRightRadius="15dp"
                  android:bottomLeftRadius="15dp"
                  android:topLeftRadius="15dp"
                  android:topRightRadius="15dp" />
      
            <stroke
                  android:width="2dip"
                  android:color="#275D69"/>
      
       </shape>
      

      现在在主 xml 中

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="vertical">
      
         <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:margin="10dp"
              android:background="@drawable/selector"
              android:orientation="vertical">
         </LinearLayout>
      
      </LinearLayout>
      

      问题解决了!

      【讨论】:

        【解决方案4】:

        您可以使用 cardView 制作圆角

         <android.support.v7.widget.CardView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:layout_marginTop="6dp"
        app:cardCornerRadius="5dp"
        app:cardBackgroundColor="#D3D3D3"
        app:cardPreventCornerOverlap="false"
        card_view:cardPreventCornerOverlap="false">
        
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="3dp" />
        
        </android.support.v7.widget.CardView>
        

        【讨论】:

          【解决方案5】:
          <?xml version="1.0" encoding="utf-8"?>
          <shape xmlns:android="http://schemas.android.com/apk/res/android"
             android:shape="rectangle" android:padding="10dp">
             <solid android:color="#FFFFFF"/>
             <corners
                android:bottomRightRadius="15dp"
                android:bottomLeftRadius="15dp"
                android:topLeftRadius="15dp"
                android:topRightRadius="15dp" />
             <stroke android:width="0dip"
              android:color="@color/#275D69"/>
          </shape>
          <corners android:radius="6dip" />
          

          随便放corner radius

          【讨论】:

            【解决方案6】:

            在生气了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
            }
            

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

            【讨论】: