【发布时间】:2021-02-21 04:18:32
【问题描述】:
我正在尝试将setOnClickListener 添加到以编程方式创建的布局中,但我无法使其正常工作。我在其他问题中看到他们在 ot 上使用侦听器创建了一个函数但没有运行它,但这不是我的情况。
for (anime in search.anime) {
runOnUiThread {
var layout = LinearLayout(this)
AnimeResultLayout.addView(layout)
layout.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
layout.setBackgroundColor(Color.parseColor("#eeeeee"))
layout.setOnClickListener {
Toast.makeText(this@SearchActivity, "clicked", Toast.LENGTH_SHORT).show()
}
var param = layout.layoutParams as LinearLayout.LayoutParams
param.setMargins(0, getDps(5), 0, getDps(5))
layout.layoutParams = param
layout.orientation = LinearLayout.HORIZONTAL
var cover = ImageView(this)
layout.addView(cover)
cover.layoutParams = LinearLayout.LayoutParams(getDps(78), getDps(111))
Picasso.with(this).load(anime.cover).into(cover)
cover.scaleType = ImageView.ScaleType.CENTER_CROP
var textlayout = LinearLayout(this)
layout.addView(textlayout)
textlayout.orientation = LinearLayout.VERTICAL
var titletext = TextView(this)
textlayout.addView(titletext)
titletext.text = anime.title
textlayout.setPadding(getDps(10), getDps(10), getDps(10), getDps(10))
titletext.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
titletext.setTextColor(resources.getColor(R.color.black))
titletext.textSize = 16F
var textlayoutbottom = ConstraintLayout(this)
textlayout.addView(textlayoutbottom)
var typelabel = TextView(this)
textlayoutbottom.addView(typelabel)
textlayoutbottom.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f)
typelabel.text = getAnimeType(anime.type)
typelabel.layoutParams = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT)
var params = typelabel.layoutParams as ConstraintLayout.LayoutParams
params.leftToLeft = textlayoutbottom.id
params.bottomToBottom = textlayoutbottom.id
typelabel.requestLayout()
typelabel.setTextColor(resources.getColor(R.color.ykblue))
}
}
我的 xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:card_view="http://schemas.android.com/tools" 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"
tools:context=".HomeActivity"
android:background="@color/white">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/ykblue"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:focusable="true"
android:focusableInTouchMode="true"
android:id="@+id/SearchText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="@android:color/transparent"
android:drawableEnd="@drawable/ic_search"
android:hint="Search"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLines="1"
android:padding="10dp"
android:textColor="@color/white"
android:textColorHighlight="@color/white"
android:textColorHint="@color/searchhintcolor"
android:textColorLink="@color/white"
app:hintAnimationEnabled="false"
android:layout_marginTop="10dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/ResultsLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animes"
android:textStyle="bold"
android:textSize="18sp"
android:background="@drawable/home_text_drawable"
android:paddingBottom="5dp"
android:layout_margin="15dp"/>
<LinearLayout
android:id="@+id/AnimeResultLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
</LinearLayout>
<TextView
android:id="@+id/ReportDuplicatedResults"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Report duplicated results"
android:textAlignment="center"
android:textSize="16sp"
android:padding="5dp"
android:background="@drawable/blue_background_btn_small"
android:layout_margin="10dp"
android:textColor="@color/white"
android:textStyle="bold"/>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
我试图在我的活动中显示一些结果,并根据它点击的内容将其带到另一个活动中,但我什至无法让它烤一些东西。我有什么遗漏吗?
【问题讨论】:
-
您是否在 LinearLayout 中添加可点击的子视图?他们可能正在消耗点击次数
-
我已经尝试在图像视图中使用
setOnClickListener,但没有任何反应 -
同
textlayout -
@ChristilynArjona 我已经添加了我的 xml。你能看到一些奇怪的东西吗?
标签: android kotlin layout listener