Pra*_*een 545
我将下面的xml设置为Image View的背景为Drawable.有用.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<stroke android:width="1dp" android:color="#000000" />
<padding android:left="1dp" android:top="1dp" android:right="1dp"
android:bottom="1dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
然后添加android:background="@drawable/yourXmlFileName"到您的ImageView
use*_*239 160
以下是我曾经有黑色边框的代码.请注意,我没有使用额外的xml文件的边框.
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/red_minus_icon"
android:background="#000000"
android:padding="1dp"/>
Run Code Online (Sandbox Code Playgroud)
mdu*_*pls 22
这是我认识的一篇旧帖子,但我认为这可能会帮助那些人.
如果要模拟不与形状的"实心"颜色重叠的半透明边框,请在xml中使用此边框.请注意,我根本不使用"stroke"标签,因为它似乎总是与实际绘制的形状重叠.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#55111111" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
<corners android:radius="5dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<solid android:color="#ff252525" />
</shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
Ash*_*ini 20
ImageView 在xml文件中
<ImageView
android:id="@+id/myImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:padding="1dp"
android:scaleType="centerCrop"
android:cropToPadding="true"
android:background="@drawable/border_image"
android:src="@drawable/ic_launcher" />
Run Code Online (Sandbox Code Playgroud)
保存下面的代码名为border_image.xml,它应该在drawable文件夹中
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#ffffff"
android:endColor="#ffffff"
android:angle="270" />
<corners android:radius="0dp" />
<stroke android:width="0.7dp" android:color="#b4b4b4" />
</shape>
Run Code Online (Sandbox Code Playgroud)
如果要将圆角设置为图像边框,则可以更改border.xml文件中的一行
<corners android:radius="4dp" />
Run Code Online (Sandbox Code Playgroud)
借助 Material Design 和新的ShapeableImageViewstrokeColor小部件,您可以使用和属性轻松创建具有边框的图像strokeWidth。这很简单,并且不涉及创建任何额外的 XML 文件。
<com.google.android.material.imageview.ShapeableImageView
android:layout_width="200dp"
android:layout_height="200dp"
app:strokeColor="@color/black"
app:strokeWidth="2dp"
app:srcCompat="@drawable/sample_image" />
Run Code Online (Sandbox Code Playgroud)
上面的代码创建了一个宽度为 2dp 的黑色边框。
In cases where you might want to add a stroke to rounded image, you can use the shapeAppearanceOverlay attribute. This allows you to draw the bitmap with the provided shape (round in this case). Check the below code for more details:
<com.google.android.material.imageview.ShapeableImageView
android:layout_width="200dp"
android:layout_height="200dp"
app:shapeAppearanceOverlay="@style/circleImageView"
app:srcCompat="@drawable/sample_image"
app:strokeColor="@color/black"
app:strokeWidth="2dp" />
Run Code Online (Sandbox Code Playgroud)
Add the below code to your styles.xml file:
<!-- Circle Shape -->
<style name="circleImageView" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">50%</item>
</style>
Run Code Online (Sandbox Code Playgroud)
Make sure your app extends the material design theme in order to use the ShapeableImageView.
在 drawable 文件夹中创建一个包含以下内容的 xml 文件(例如“frame_image_view.xml”):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="@dimen/borderThickness"
android:color="@color/borderColor" />
<padding
android:bottom="@dimen/borderThickness"
android:left="@dimen/borderThickness"
android:right="@dimen/borderThickness"
android:top="@dimen/borderThickness" />
<corners android:radius="1dp" /> <!-- remove line to get sharp corners -->
</shape>
Run Code Online (Sandbox Code Playgroud)
用你想要的任何东西替换@dimen/borderThickness和@color/borderColor或添加相应的尺寸/颜色。
将 Drawable 作为背景添加到您的 ImageView:
<ImageView
android:id="@+id/my_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/frame_image_view"
android:cropToPadding="true"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
Run Code Online (Sandbox Code Playgroud)
您必须使用android:cropToPadding="true",否则定义的填充无效。或者android:padding="@dimen/borderThickness"在您的 ImageView 中使用以实现相同的目的。如果边框框住父级而不是 ImageView,请尝试使用android:adjustViewBounds="true".
在代码中更改边框颜色的最简单方法是使用 tintBackgound 属性。
ImageView img = findViewById(R.id.my_image_view);
img.setBackgroundTintList(ColorStateList.valueOf(Color.RED); // changes border color to red
Run Code Online (Sandbox Code Playgroud)
或者
ImageView img = findViewById(R.id.my_image_view);
img.setBackgroundTintList(getColorStateList(R.color.newColor));
Run Code Online (Sandbox Code Playgroud)
不要忘记定义您的newColor.
| 归档时间: |
|
| 查看次数: |
270242 次 |
| 最近记录: |