【问题标题】:Draw a border around an ImageView which has wrap_content height and width在具有 wrap_content 高度和宽度的 ImageView 周围绘制边框
【发布时间】:2018-03-22 14:25:34
【问题描述】:

我需要在 Android 上围绕 ImageView 设置一个矩形边框。 我不知道图像的确切比例,所以将高度和宽度设置为wrap_contentImageView 受限于顶视图和底视图。

如何在图像周围精确地绘制边框?没有完整的 ImageView?

目前它给出以下结果:

我的 XML。
布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/outer_background"
    android:orientation="vertical">

    <include layout="@layout/toolbar"/>

    <RelativeLayout
        android:id="@+id/login_layout_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="16dp"
        android:layout_weight="1">

        <ImageView
            android:id="@+id/login_course_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/login_layout_account_fields"
            android:layout_alignParentTop="true"
            android:background="@drawable/border_course_logo"
            tools:ignore="contentDescription"/>

        <ProgressBar
            android:id="@+id/login_progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:indeterminate="true"
            android:visibility="gone"/>

        <RelativeLayout
            android:id="@id/login_layout_account_fields"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_gravity="top"
            tools:ignore="textFields">

        </RelativeLayout>
    </RelativeLayout>
</LinearLayout>

border_course_logo.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/transparent"/>
    <stroke
        android:width="1dp"
        android:color="@color/divider"/>
    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp"/>
</shape>

【问题讨论】:

    标签: android android-layout android-imageview


    【解决方案1】:

    在你的地方,我会在下载图像后更改 ImageView 的宽度(如果你使用 Glide 或类似的东西)。

    【讨论】:

    • 仅使用 xml 有什么治疗方法吗?
    • 我认为不是
    【解决方案2】:

    尝试在布局 xml 文件中将 android:adjustViewBounds="true" 添加到您的 ImageView。

    【讨论】:

    • android:adjustViewBounds="true" 没有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 2011-01-17
    相关资源
    最近更新 更多