Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android上设置textview或imageview的连锁反应?_Android_Android Imageview_Textview_Material Design - Fatal编程技术网

如何在Android上设置textview或imageview的连锁反应?

如何在Android上设置textview或imageview的连锁反应?,android,android-imageview,textview,material-design,Android,Android Imageview,Textview,Material Design,我想在Android Studio中设置textview和imageview的连锁反应。我怎么做呢?你可以用 启动效果 final RippleBackground rippleBackground=(RippleBackground)findViewById(R.id.content); ImageView imageView=(ImageView)findViewById(R.id.centerImage); imageView.setOnClickListener(new View.OnC

我想在Android Studio中设置textview和imageview的连锁反应。我怎么做呢?

你可以用

启动效果

final RippleBackground rippleBackground=(RippleBackground)findViewById(R.id.content);
ImageView imageView=(ImageView)findViewById(R.id.centerImage);
imageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        rippleBackground.startRippleAnimation();
    }
});
停止动画:

rippleBackground.stopRippleAnimation();
为科特林

val rippleBackground = findViewById(R.id.content) as RippleBackground
val imageView: ImageView = findViewById(R.id.centerImage) as ImageView
imageView.setOnClickListener(object : OnClickListener() {
    fun onClick(view: View?) {
        rippleBackground.startRippleAnimation()
    }
})
使用图书馆。是其中之一。只需添加它的依赖项,并在每个需要连锁反应的元素之前添加以下xml代码:

<com.balysv.materialripple.MaterialRippleLayout
android:id="@+id/ripple"
android:layout_width="match_parent"
android:layout_height="wrap_content">

Ref:


如果要将涟漪限制到TextView/ImageView的大小,请使用:

<TextView
android:background="?attr/selectableItemBackground"
android:clickable="true"/>


(我认为它看起来更好)

除了@Bikesh M Annur的答案之外,一定要更新您的支持库。以前我使用的是23.1.1,但什么都没有发生。将其更新到23.3.0就成功了。


<TextView
    android:id="@+id/txt_banner"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:layout_below="@+id/title"
    android:background="@drawable/ripple_effect"
    android:gravity="center|left"
    android:paddingLeft="15dp"
    android:text="@string/banner"
    android:textSize="15sp" />
将此添加到drawable中

<?xml version="1.0" encoding="utf-8"?>

<!--this ripple animation only working for >= android version 21 -->

<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/click_efect" />

如果@Bikesh M Annur()发布的投票结果良好的解决方案对您无效,请尝试使用:

<TextView
...
android:background="?android:attr/selectableItemBackgroundBorderless"
android:clickable="true" />

<ImageView
...
android:background="?android:attr/selectableItemBackgroundBorderless"
android:clickable="true" />

另外,当使用android:clickable=“true”add
android:focusable=“true”
时,因为:


“声明为可单击但未声明为可聚焦的小部件无法通过键盘访问。”

上述答案的补充是添加可聚焦以避免UI编辑器的警告

android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"
试试这个。 这是为我工作

android:clickable="true"
    android:focusable="true"
    android:background="?android:attr/selectableItemBackground"

请参考下面的答案,了解连锁反应

文本视图或视图上的涟漪:

android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackgroundBorderless"
按钮或图像视图上的波纹:

android:foreground="?android:attr/selectableItemBackgroundBorderless"
添加的最佳方式:

    <ImageView
        android:id="@+id/ivBack"
        style="?attr/actionButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:src="@drawable/ic_back_arrow_black"
        android:tint="@color/white" />

添加
android:clickable=“true”
android:focusable=“true”

连锁反应

android:background="?attr/selectableItemBackgroundBorderless"
可选择的效果

android:background="?android:attr/selectableItemBackground"
按钮效果

android:adjustViewBounds="true" style="?android:attr/borderlessButtonStyle"
对于圆形波纹:
android:background=“?attr/selectableitemsbackgroundless”

对于矩形波纹:
android:background=“?attr/selectableItemBackground”

如果上述解决方案不适用于您的TextView,那么这肯定会起作用:

1。添加此样式

<style name="ClickableView">
    <item name="colorControlHighlight">@android:color/white</item>
    <item name="android:background">?selectableItemBackground</item>
</style>


请先详细说明你的问题。你到底需要什么,到目前为止你尝试了什么。简单地说我想要涟漪效果,这是一个非常广泛的问题。你也可以提供一些链接,以参考你想要什么。还可以查看文本视图和图像视图的“我想要效果”选择/取消选择效果。你尝试过搜索同样的?很好,我在棒棒糖上用的很好,但这种方式适用于哪个android版本?它对棒棒糖前的版本有效吗?@Richard一年前我对KitKat(或ICS)有一个问题,没有android的clickable=“true”ClickListeners就无法工作:对于该版本,在现代版本的android上,现在是
android:background=“?android:attr/selectableItemBackground”
当我需要自定义背景时,如何使用它?有/没有
无边框的区别
与使用
selectableItemBackground
相比,使用
SelectableItemBackgroundless
有什么区别吗?我不知道为什么它没有边框限制,而是扩展到整个layout@rakeshkashyap这个区别在于selectableItemBackground将波纹保持在其视图大小(宽度/高度)内。selectableItemBackgroundBorderless将在其他视图(如官方计算器应用程序ripple)上扩展它的涟漪,并且还需要设置此>>>android:focusable=“true”噢,请不要,这不是最好的方式,因为您对它的控制较少,而且它在android SDK中得到广泛支持。如果有人读到这篇文章,我建议你看看@Bikesh或Karthik的评论,如果你想控制颜色的话。我之前已经提到过这一点()因此,为了避免重复答案,请竖起我的大拇指,谢谢!)请注意,
selectableitemsbackgroundless
是API 21+。您可以在下面选择
selectableItemBackground
以避免兼容性问题!有Kotlin版本的吗?@Renginer有。抱歉耽搁了
<style name="ClickableView">
    <item name="colorControlHighlight">@android:color/white</item>
    <item name="android:background">?selectableItemBackground</item>
</style>
android:theme="@style/ClickableView"
android:background="?android:selectableItemBackground"
android:focusable="true"
android:clickable="true"