【问题标题】:How can I give a border to image view in Android?如何在 Android 中为图像视图添加边框?
【发布时间】:2012-12-20 12:06:38
【问题描述】:

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

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

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

【问题讨论】:

标签: android background imageview


【解决方案1】:

我认为您应该创建一个自定义视图。做一个以背景为边框的 LinearLayout,并使 ImageView 在 LinearLayout 内居中。使用 9-patch 获得正确的拉伸并创建内容区域以便显示边框(在 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)。您可以与内容区域进行相同的拉伸。

【讨论】:

  • k.我会试试这个并回复你。
  • K 我同意这一点,我可以添加边框。但我认为它不会满足第二个问题。因为我需要一个主背景图片。是否可以为背景添加一个更宽松的视图。
  • 要么您的主背景是边框图像的一部分,要么只是将线性布局设置为相对布局,并且必须在其中添加图像。
  • 主图像不是边框的一部分
  • 其实我的要求就是这样。我有圆形硬币图像(png透明图像,即无背景)。我需要一个边界。而且那枚硬币应该有大师背景。因为它是圆形的。
猜你喜欢
  • 1970-01-01
  • 2015-01-29
  • 2013-02-02
  • 2019-04-23
  • 1970-01-01
  • 2013-02-27
  • 2011-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多