【问题标题】:Add small button on the upper right of imageview在imageview右上角添加小按钮
【发布时间】:2018-03-14 06:52:46
【问题描述】:

目前我有这个布局:

https://imgur.com/a/Y2a67

这是此布局的代码:

  <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <HorizontalScrollView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">

                        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent">
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="horizontal">

                            <ImageView
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_margin="5dp"
                                android:src="@drawable/img_docu_placeholder"/>


                            <ImageView
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_margin="5dp"
                                android:src="@drawable/img_docu_placeholder"/>
                            <ImageView
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_margin="5dp"
                                android:src="@drawable/img_docu_placeholder"/>
                            <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                                android:layout_margin="5dp"
                            android:src="@drawable/img_docu_placeholder"/>
                            <ImageView
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_margin="5dp"
                                android:src="@drawable/img_docu_placeholder"/>

                        </LinearLayout>
                        </FrameLayout>

                    </HorizontalScrollView>

我想要实现的是:

https://imgur.com/a/MYxkh

我需要在图像视图的左上角添加额外的“X”按钮

【问题讨论】:

  • 如果我的回答适合你,请采纳
  • 当我在图像视图中添加图片时,x 按钮超出范围
  • @PhátPhát 可以使用坐标布局吗?

标签: android xml android-layout imageview


【解决方案1】:

您可以使用FrameLayout。您的图标必须具有android:layout_gravity 属性。

<FrameLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/myImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:src="@mipmap/ic_launcher"/>

    <ImageView
        android:id="@+id/imgX"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_gravity="top|left" //This line
        android:src="@mipmap/ic_launcher_round" />

</FrameLayout>

希望对你有帮助。

【讨论】:

  • 这个能适应不同的屏幕尺寸吗?
  • 您可以使用 sdp 库进行响应式设计。
  • 是否可以使用坐标布局来做到这一点?
  • 抱歉,我不确定。
【解决方案2】:

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="108dp"
android:background="#ffffff">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="8dp"
    android:background="#e6e6e6" />

<ImageView
    android:layout_width="16dp"
    android:layout_height="16dp"
    android:src="@drawable/ic_cancel_black" />

输出:

支持多种分辨率:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-17
    • 2018-05-30
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    相关资源
    最近更新 更多