android - 如何在 Android 中为 ImageView 添加边框?

标签 android background imageview

在我的新 android 应用程序中,我需要使用 .9.png 之类的东西为 imageview 提供边框。

边框大小应该根据我给 imageview 的图像而改变,如果可能的话,我需要给 imageview 一个背景图像,因为我将要将透明的 png 图像应用于 imageview

我应该为此创建一个新的自定义 View 吗?

最佳答案

我认为您应该创建一个自定义 View 。做一个以背景为边框的 LinearLayout,并让 ImageView 在 LinearLayout 内居中。使用 9-patch 获得正确的拉伸(stretch)并创建内容区域以便显示边框(在 android SDK/Tools 中使用 draw9patch)。

例子:

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/border">

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            />
    </LinearLayout>

然后 9-patch 你的边框(在本例中为 border.9.png)。您可以像内容区域一样进行拉伸(stretch)。

关于android - 如何在 Android 中为 ImageView 添加边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13971874/

相关文章:

html - 对齐页面中的两个可滚动元素

c++ - 如果系统处于事件状态,如何检查 C++?

android - 如何删除显示在 scrollView 顶部的空白

android - 在 LinearLayout 中对齐 TextView 和 ImageView

java - 使用 AndEngine 时的 onResumeGame() NullPointerException

java - 如何知道 Retrofit 生成的 Url 是哪个?

android - ImageSpan.ALIGN_BASELINE 当 TextView 有 lineSpacing 时

html - 移动设备上的背景大小

android - 如何在 Android 的 ImageView 上制作卷边?

android - 我的 list 上没有选项