如何使文本出现在水平滚动视图中的图像下方?

发布于 2025-01-15 13:25:07 字数 2439 浏览 3 评论 0原文

我正在尝试制作一个Horizo​​ntalScrollview。滚动视图中的每个图像下面都应该有一个标题。我发现其他一些帖子说将图像/文本视图包装在垂直线性布局内。但这不起作用。

这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

豆芽 2025-01-22 13:25:07

该代码对我来说看起来不错。您可以尝试为 ImageView 添加特定的 layout_width 。请尝试下面的代码,我在其中添加了来自 drawable 文件夹的图像,并且文本位于结果布局中图像的下方。尝试一下,让我知道这是否适合您并满足您的要求。

 <LinearLayout
     android:id="@+id/l2"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginEnd="16dp"
     android:orientation="vertical"
     app:layout_constraintLeft_toLeftOf="parent"
     app:layout_constraintTop_toTopOf="parent">

     <ImageView
        android:padding="16dp"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:src="@drawable/about" />

     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:gravity="center"
        android:padding="2dp"
        android:text="@string/app_name"
        android:textSize="20sp" />
 </LinearLayout>

The code looks fine for me . You can try adding a specific layout_width for the ImageView . Please try the below code, where I added a image from drawable 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.

 <LinearLayout
     android:id="@+id/l2"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginEnd="16dp"
     android:orientation="vertical"
     app:layout_constraintLeft_toLeftOf="parent"
     app:layout_constraintTop_toTopOf="parent">

     <ImageView
        android:padding="16dp"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:src="@drawable/about" />

     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:gravity="center"
        android:padding="2dp"
        android:text="@string/app_name"
        android:textSize="20sp" />
 </LinearLayout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文