【问题标题】:Add/Remove border on-click Android Java [duplicate]添加/删除边框单击Android Java [重复]
【发布时间】:2016-07-04 03:17:58
【问题描述】:

我想知道使用 Java 在 Android Studio 中添加和删除图像边框的最简单方法。我只想让用户知道他单击了图像(当然是可单击的),而没有两个不同的 .png。

【问题讨论】:

  • 你用谷歌搜索过吗?你应该在那里找到例子。你有没有尝试过?

标签: java android image onclick border


【解决方案1】:

将图像放入布局中,然后用您希望的一些值填充布局。为该图像视图设置一个 OnClickListener,并在单击时更改图像所在布局的背景颜色。您可以非常轻松地将其作为打开和关闭切换。

编辑:-

ssh 的回答比我的要清楚得多。请参考他的。

【讨论】:

    【解决方案2】:

    您可以在图像前添加一个宽度为 1dp 的视图,并在需要显示边框时使其可见。

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:visibility="invisible"/>
        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src=""/>
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      ImageView 包裹在FrameLayout 上并添加

      android:foreground="?android:attr/selectableItemBackground" 属性。

                  <FrameLayout
                      android:layout_width="48dp"
                      android:layout_height="match_parent"
                      android:foreground="?android:attr/selectableItemBackground">
      
                      <ImageView
                          android:id="@+id/myButton"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          style="?android:borderlessButtonStyle"
                          android:scaleType="center"
                          android:src="@drawable/my_drawable" />
                  </FrameLayout>
      

      【讨论】:

      • @AdamSwid 请批准以后的搜索
      • 我如何批准? (对不起,这个网站的新手)
      • @AdamSwid 您只需按答案左侧的复选标记 :)
      • 太好了,再次感谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-21
      • 1970-01-01
      • 2015-08-24
      • 2019-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多