【问题标题】:FrameLayout with rounded corners带圆角的 FrameLayout
【发布时间】:2016-07-12 12:34:00
【问题描述】:

我的根视图(线性:垂直)中有一个 FrameLayout,用于根据用户输入保存不同的片段。 FrameLayout 比背景小,所以它看起来是“浮动的”。

如何使 FrameLayout 的角变圆?

FrameLayout 中的碎片不是图片,所以我无法裁剪它们。

【问题讨论】:

  • 你不能把一个手工绘制的drawable作为你的FrameLayout的背景吗?

标签: android android-framelayout


【解决方案1】:

创建一个 xml 文件,例如your_rounded_shape.xml,在你的drawable文件夹中添加以下代码:

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

    <stroke android:width="2dp"
        android:color="#999999"/>

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

    <corners android:radius="8dp" />
</shape>

然后在您的FrameLayout 中使用your_rounded_shape.xml 作为背景。像这样的:

<FrameLayout
    android:width="match_parent"
    android:height="wrap_content"
    android:background="@drawable/your_rounded_shape"/>

【讨论】:

  • 这太完美了!谢谢,这正是我想要的。
【解决方案2】:

将 FrameLayout 替换为 CardView 布局。

<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:cardCornerRadius="8dp">

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

【讨论】:

    【解决方案3】:

    只是对上述答案的改进: 这个xml文件的名称,比如说rounded_frame_layout.xml

    <?xml version="1.0" encoding="UTF-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
        <solid android:color="#CCCCCC"/>    
    
        <stroke android:width="2dp"
            android:color="#999999"/>
    
        <padding android:left="2dp"
             android:top="2dp"
             android:right="2dp"
             android:bottom="2dp"/> 
    
        <corners android:radius="8dp"/> 
    </shape>
    
    
    Then use your drawable as follows:
    
    <FrameLayout
         android:width="match_parent"
         android:height="wrap_content"
         android:background="@drawable/rounded_frame_layout"/>
    

    【讨论】:

    • 这使得形状不可重复使用
    猜你喜欢
    • 2011-04-05
    • 2020-12-30
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    相关资源
    最近更新 更多