繁体   English   中英

使用嵌套的TextView限制Android水平ScrollView的宽度

[英]Restrict the Width of an Android Horizontal ScrollView with nested TextView

我已经为列表视图的自定义适配器编写了以下代码。 这是我的row_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">


<HorizontalScrollView
    android:layout_width="300dp"
    android:layout_height="24dp"
    android:id="@+id/horizontalScrollView">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="111111111111111111111111111111111111111111111111111111"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:scrollHorizontally="true"
        android:maxLines = "1"
        android:textSize="18sp" />

</HorizontalScrollView>



<TextView
    android:id="@+id/price"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/horizontalScrollView"
    android:layout_marginTop="5dp"
    android:text="$39.99"
    android:textColor="@android:color/black" />



<ImageView
    android:id="@+id/store"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_favorite_border_black_24dp"
    android:layout_centerVertical="true"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="16dp" />

</RelativeLayout>

我将文本视图置于水平滚动视图中,因为许多文本项都非常长。 上面的代码确实使它从左向右滚动并将其限制为一行,但是我希望scrollView在imageView之前结束。 下面是所需功能的图片:

视觉辅助

但是,当我运行该应用程序时,文本直接在imageView上方流血。

关于如何纠正这一点的任何想法?

更新1:这是我现在得到的:

视觉辅助

这是因为RelativeLayout将视图放在另一个视图的顶部,当您滚动Horizo​​ntalScrollView时,实际上是在重绘它,并且将其绘制在ImageView的顶部。

尝试用LinearLayout替换RelativeLayout,看看它是否固定。

编辑:我没有看到下面的图像。 嗯,这是因为您使用的是相对布局,而Horizo​​ntalScrollView的宽度是match_parent,并且视图彼此顶部相互重叠。 尝试将LinearLayout与width = 0px和weight = 1一起使用

编辑2:好的,所以您想要价格-Horizo​​ntalScreenView-ImageView? 这是整个代码,只需复制并粘贴即可。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">

<TextView
    android:id="@+id/price"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/horizontalScrollView"
    android:layout_marginTop="5dp"
    android:text="$39.99"
    android:textColor="@android:color/black" />

<HorizontalScrollView
    android:id="@+id/horizontalScrollView"
    android:layout_width="0px"
    android:layout_height="24dp"
    android:layout_weight="1">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:maxLines="1"
        android:scrollHorizontally="true"

        android:text="111111111111111111111111111111111111111111111111111111"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline"
        android:textSize="18sp" />

</HorizontalScrollView>


<ImageView
    android:id="@+id/store"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:src="@drawable/ic_favorite_border_black_24dp" />

</LinearLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM