【问题标题】:Custom shaped Linearlayout with curved side in android在android中具有弯曲侧的自定义形状的Linearlayout
【发布时间】:2016-04-27 11:55:04
【问题描述】:

我正在尝试制作一个自定义形状的线性布局,如下所示

我试图只使一侧弯曲。尝试使用圆角半径,但它的外观与上面不同。

已经试过这个背景形状如下:-

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

    <padding
        android:bottom="7dp"
        android:left="7dp"
        android:right="7dp"
        android:top="7dp" />

    <corners
        android:bottomLeftRadius="50dp"
        android:bottomRightRadius="50dp"
        android:topLeftRadius="0dp"
        android:topRightRadius="0dp" />
</shape>

它只对角进行圆化,并且在增加值形状时不会保留它变得太圆。我想要曲线而不是圆角

【问题讨论】:

  • 您可以使用这种弯曲的阴影创建图像,并可以添加为Layout的背景。
  • 我不确定它是否在所有分辨率上看起来都一样,所以我避免了。会吗?
  • 您可以将dimen用于所有分辨率并使其轻松工作。
  • @SumeetDarade 你能解决这个问题吗?你是怎么做到的?

标签: android android-xml android-drawable android-shape


【解决方案1】:

已经很晚了,但这是为未来的寻求者准备的。 我认为vector drawables 是最完美的解决方案。使用下面的矢量as background 来获得自定义形状的底部弯曲布局。

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="100.0"
android:viewportWidth="200.0">
<path
    android:fillColor="#YOUR_FAVORITE_COLOR"
    android:pathData="M200,0H0v4.5h0v75.8h0c17.8,10.2 56,17.2 100.5,17.2c44.5,0 81.6,-7 99.5,-17.2h0V4.5h0V0z" />

【讨论】:

  • 将此与 ImageView scaleType=fitXY 一起使用
【解决方案2】:

在可绘制文件夹中创建一个形状文件,例如:my_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
   <corners
      android:bottomLeftRadius="100dp"
      android:bottomRightRadius="100dp"
      android:topLeftRadius="0dp"
      android:topRightRadius="0dp" />
   <padding
      android:bottom="0dp"
      android:left="0dp"
      android:right="0dp"
      android:top="0dp" />
   <stroke
      android:width="0.5dp"
      android:color="@color/theme_red" />
  <solid android:color="@color/white" />
</shape> 

然后将此形状添加到您的布局中作为背景。例如:

<LinearLayout
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@drawable/my_shape"
     android:orientation="horizontal">             
</LinearLayout>

【讨论】:

  • 正如我所说,我尝试添加半径。它看起来不像图像中的形状。我想让线条弯曲
  • 我认为你应该使用背景图片。首先,您根据要求设计图像。然后将其用作布局中的背景
  • 是的,您能建议任何一种在所有设备上看起来都相同的分辨率吗?
  • 您应该从 mdpi 屏幕开始您的图像设计,然后设计屏幕的其余部分,如 hdpi、xhdpi、xxhdpi 等,并且必须遵循屏幕设计比例。关注Link1Link2
【解决方案3】:

我所做的是 创建一个可绘制的allowaccess_button_normal.xml

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

    <item>
        <shape android:shape="oval">
            <solid android:color="@color/colorPrimary"/>
        </shape>
    </item>

</selector>

试试下面的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#fefefe"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:background="@color/colorPrimary"
        android:layout_height="110dp">
    </RelativeLayout>

    <ImageView
        android:layout_marginTop="85dp"
        android:src="@drawable/allowaccess_button_normal"
        android:layout_width="match_parent"
        android:layout_height="50dp" />


</RelativeLayout>

【讨论】:

    【解决方案4】:

    shape_curve.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item>
            <shape android:shape="rectangle" />
        </item>
    
        <item
            android:bottom="0dp"
            android:left="-100dp"
            android:right="-100dp"
            android:top="-80dp">
            <shape android:shape="oval">
                <solid android:color="@color/colorAccent" />
            </shape>
        </item>
    

    使用

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@drawable/shape_curve"
        android:orientation="horizontal" />
    

    【讨论】:

      【解决方案5】:
       <layer-lisxmlns:android="http://schemas.android.com/apk/res/android">
      
         <item  android:top="175dp">
      
          <shape android:shape="oval">
      
           <gradient
              android:angle="135"
              android:startColor="#f56f2c"
              android:endColor="#fa9f46"/>
      
         </shape>
        </item>
      
      
        <item android:bottom="40dp">
      
         <shape android:shape="rectangle">
      
          <gradient
              android:angle="135"
              android:startColor="#f56f2c"
              android:endColor="#fa9f46"/>
      
         </shape>
       </item>
      </layer-list>
      

      【讨论】:

        【解决方案6】:

        您可以在值 -dimension.xml 文件中设置尺寸,然后在您的线性布局的 xml 中设置它,它将给出如下所示

        <?xml version="1.0" encoding="utf-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid
           android:color="#FFFF00" />
        <padding android:left="7dp"
            android:top="7dp"
            android:right="7dp"
            android:bottom="7dp" />
        

        只需在您的可绘制集合中创建一个 xml 文件即可满足您需要的尺寸,然后通过您的线性布局调用它

        【讨论】:

        • 很抱歉没有得到它,但你能举个例子吗?不知道你说的是什么尺寸
        猜你喜欢
        • 2021-11-12
        • 2021-03-28
        • 1970-01-01
        • 2022-01-11
        • 1970-01-01
        • 1970-01-01
        • 2022-07-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多