【发布时间】:2020-10-19 17:06:34
【问题描述】:
由于我是 android studio 的新手,请多多包涵。我想制作一个ScrollView,其中包含具有相应名称(TextView)的图像。我希望能够通过在ScrollView 中触摸它来选择图像,但我不知道如何。我已经实现了这样的 ScrollView,我还希望能够添加带有名称的图片。
Main_activity.xml
<HorizontalScrollView
android:id="@+id/scrollFilterView"
android:layout_width="fill_parent"
android:layout_height="130dp"
android:scrollbars="none"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.57"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<LinearLayout
android:id="@+id/gallery"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" />
</HorizontalScrollView>
scrollview.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/filterView"
android:layout_width="90dp"
android:layout_height="90dp"
app:srcCompat="@android:drawable/sym_def_app_icon"
android:contentDescription="filterView" />
<TextView
android:id="@+id/textFilter"
android:layout_width="90dp"
android:gravity="center"
android:layout_height="wrap_content"
android:text="textFilter" />
MainActivity.java
LayoutInflater inflater = LayoutInflater.from(this);
for (int i = 0; i < NUMBER_OF_FILTERS_GRID; i++ ) {
View scrollView = inflater.inflate(R.layout.scrollview, gallery, false);
TextView textview = scrollView.findViewById(R.id.textFilter);
textview.setText("Filter "+ i);
ImageView filterView = scrollView.findViewById(R.id.filterView);
filterView.setImageResource(R.mipmap.ic_launcher);
gallery.addView(scrollView);
}
}
如果这是否是正确的路径以及我应该使用哪些功能,请提供一些意见:)
【问题讨论】:
-
使用 RecycleView 代替 ScrollView