如何使文本出现在水平滚动视图中的图像下方?
我正在尝试制作一个HorizontalScrollview
。滚动视图中的每个图像下面都应该有一个标题。我发现其他一些帖子说将图像/文本视图包装在垂直线性布局内。但这不起作用。
这是我的 XML:
<HorizontalScrollView
android:id="@+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text1"
android:scrollbars="none"
android:layout_alignParentTop="true"
android:fillViewport="true"
android:measureAllChildren="false">
<LinearLayout
android:id="@+id/l1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/l2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginEnd="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginTop="3dp"
android:padding="2dp"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
这就是我以编程方式膨胀布局和添加图像的方式:
List<ImageView> mImageViews = ((MainActivity) context).getImages("data.csv");
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.part4, this);
LinearLayout layout1 = this.findViewById(R.id.l1);
for (ImageView img : mImageViews) {
TextView tv = new TextView(getContext());
tv.setText("Image 1");
img.setAdjustViewBounds(true);
img.setScaleType(ImageView.ScaleType.FIT_XY);
img.setPadding(0,0,32,0);
layout1.addView((img));
layout1.addView(tv);
}
它生成一个水平可滚动布局,但图像和文本并排。并且文本不在每张图像下方。
I'm trying to make a HorizontalScrollview
. Every image within the scroll view should have a caption underneath. I found some other posts saying to wrap the Image/Text views inside a vertical linear layout. But it doesn't work.
This is my XML:
<HorizontalScrollView
android:id="@+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text1"
android:scrollbars="none"
android:layout_alignParentTop="true"
android:fillViewport="true"
android:measureAllChildren="false">
<LinearLayout
android:id="@+id/l1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/l2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginEnd="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginTop="3dp"
android:padding="2dp"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
And this is how I inflate the layout and adding images programmatically:
List<ImageView> mImageViews = ((MainActivity) context).getImages("data.csv");
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.part4, this);
LinearLayout layout1 = this.findViewById(R.id.l1);
for (ImageView img : mImageViews) {
TextView tv = new TextView(getContext());
tv.setText("Image 1");
img.setAdjustViewBounds(true);
img.setScaleType(ImageView.ScaleType.FIT_XY);
img.setPadding(0,0,32,0);
layout1.addView((img));
layout1.addView(tv);
}
It produces a horizontally scrollable layout, but image and text are side by side. And text not below each image.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该代码对我来说看起来不错。您可以尝试为
ImageView
添加特定的layout_width
。请尝试下面的代码,我在其中添加了来自drawable
文件夹的图像,并且文本位于结果布局中图像的下方。尝试一下,让我知道这是否适合您并满足您的要求。The code looks fine for me . You can try adding a specific
layout_width
for theImageView
. Please try the below code, where I added a image fromdrawable
folder and the text was below the image in the resulting layout. Try this and let me know whether this works for you and meet your requirements.