【问题标题】:How can I programatically add a rounded border to my ImageView?如何以编程方式向 ImageView 添加圆角边框?
【发布时间】:2016-09-23 14:49:42
【问题描述】:

到目前为止,我能够做到这一点的方法是创建一个 XML 可绘制对象:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#00ffffff" />
    <padding
        android:left="0dp"
        android:top="0dp"
        android:right="0dp"
        android:bottom="0dp" />
    <corners android:radius="12dp" />
    <stroke android:width="3dp" android:color="#ffffffff" />
</shape>

在我的布局 XML 中:

        <FrameLayout
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_gravity="top|center_horizontal">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/profilePicture"
                android:scaleType="fitXY"
                android:clickable="true"
                android:contentDescription="@string/content_desc_profile_picture" />

            <ImageView
                android:src="@drawable/frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:contentDescription="@string/content_desc_default_profile_picture" />

        </FrameLayout>

我的问题是框架没有覆盖它后面可绘制的 profilePicture 的角落,从而使实际图像溢出边框。我怎样才能覆盖角落?真希望现在有一个类似 CSS 的属性!

【问题讨论】:

    标签: java android xml imageview drawable


    【解决方案1】:

    shape.xml

    <?xml version="1.0" encoding="utf-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
            <solid android:color="#000000" />
            <corners android:radius="2dp" />
        </shape>
    

    您的布局 .xml

     <FrameLayout
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_gravity="top|center_horizontal" xmlns:android="http://schemas.android.com/apk/res/android">
    
    
    
        <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/shape"
        />
        <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/profilePicture"
                android:scaleType="fitXY"
                android:clickable="true"
                android:layout_margin="5dp"
                android:src="@drawable/your_image"
                android:background="@drawable/shape" />
    
    </FrameLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-13
      • 2012-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      相关资源
      最近更新 更多