【问题标题】:Display textview below imageview and not bottom to the parent在 imageview 下方显示 textview 而不是在父级下方显示
【发布时间】:2017-12-08 02:36:03
【问题描述】:

我正在开发一个 Android 应用程序,但我遇到了如何在视图中显示图像的问题。我的问题是我想在我的 imageView 下方显示一个图像和两个 textView,而不将它们的底部与父级对齐。请参阅下面的线框。

我的问题是,当 imageView 占据了 relativeLayout 的所有高度时,我的 textViews 隐藏在父视图下方。

我尝试了很多东西,但没有什么是令人满意的...... 我试图将我的 imageview 放在一个 linearLayout 中并对其施加重量,但如果我的 imageView 较小,我在 imageView 和 textView 之间会有一个空白。

这很复杂,因为我加载的每个图像都可以有不同的大小,因此我无法为其修复默认大小。 此外,我的布局在 viewPager 中,当我想在适配器上实例化它时获得视图的高度时,它返回 0。所以我无法计算视图中元素的大小。

你知道我该如何做我的布局吗?

也许在 coordinatorLayout 中有循环依赖(但我不知道如何实现)?

这里是我想用我的视图做什么的线框图:image

这里是我的布局代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/layout_photo_detail"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/relative_photo_detail_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/relative_photo_detail_bottom_infos">

        <ImageView
            android:id="@+id/photo_detail_full_size"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:src="@drawable/placeholder"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"/>

        <TextView
            android:id="@+id/photo_detail_owner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/photo_detail_full_size"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:textColor="@color/grey"/>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relative_photo_detail_bottom_infos"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/relative_photo_detail_image"
        android:padding="10dp">

        <ImageView
            android:id="@+id/photo_detail_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:src="@drawable/delete"/>
    </RelativeLayout>
</RelativeLayout>

【问题讨论】:

    标签: android android-layout android-imageview textview android-relativelayout


    【解决方案1】:

    尝试将您的代码放在滚动视图中

    【讨论】:

    • 您好,感谢您的回复。我已经在考虑滚动视图,但结果不是我想要的。 textviews 可以隐藏,用户不会想立即滚动并查看图像下方是否有信息。
    【解决方案2】:

    如下更新你的布局结构:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout_photo_detail"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <!-- TextView 2 -->
        <TextView
            android:id="@+id/photo_detail_owner_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginTop="8dp"
            android:padding="8dp"
            android:gravity="center"
            android:textSize="16sp"
            android:textColor="@android:color/holo_red_dark"
            android:text="This is TextView Two"/>
    
        <!-- TextView 1 -->
        <TextView
            android:id="@+id/photo_detail_owner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/photo_detail_owner_description"
            android:layout_marginTop="8dp"
            android:padding="8dp"
            android:gravity="center"
            android:textSize="18sp"
            android:textColor="@android:color/black"
            android:text="This is TextView One"/>
    
        <RelativeLayout
            android:id="@+id/relative_photo_detail_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/photo_detail_owner"
            android:background="@android:color/black">
    
            <!-- Image -->
            <ImageView
                android:id="@+id/photo_detail_full_size"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="fitCenter"
                android:src="@drawable/dummy"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true"/>
    
            <!-- Delete Icon -->
            <ImageView
                android:id="@+id/photo_detail_delete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_margin="16dp"
                android:src="@drawable/delete"/>
        </RelativeLayout>
    </RelativeLayout>
    

    输出:

    希望对你有帮助~

    【讨论】:

    • 它可能关注的对象。我可以知道投反对票的原因吗?
    • 您好,感谢您的回复。不幸的是,这不是我正在寻找的解决方案。正如我在问题中所说,我不想将我的两个文本视图底部与父视图对齐。所有的图像都有不同的大小,所以如果一个图像没有占据屏幕的所有高度,我希望我的文本视图就在它的下方,而不是固定在父视图的底部。
    • 那么你可以使用ScrollView作为根布局,使用ImageView高度作为wrap_content
    • 但是我不想使用滚动视图,它对这个视图不友好。如果图像很大并且隐藏了文本视图,用户将不会考虑滚动视图。
    • 如果您的图像尺寸大于屏幕尺寸,那么如何在屏幕上显示完整图像?
    【解决方案3】:

    像这样的

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_gravity="center_horizontal" />
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:id="@+id/textView2"
        android:textAlignment="center" />
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:id="@+id/textView3"
        android:textAlignment="center" />
    
    </LinearLayout>
    

    【讨论】:

    • 您好,感谢您的回复。不幸的是,这不是我正在寻找的解决方案。正如我在问题中所说,我不想将我的两个文本视图底部与父视图对齐。所有的图像都有不同的大小,所以如果一个图像没有占据屏幕的所有高度,我希望我的文本视图就在它下面而不是固定在父视图的底部。
    • 谢谢,我已经尝试过这个解决方案,但是如果图像很大并且占据了布局的所有高度,它就不起作用。文本视图隐藏在视图下...
    • 如果图像太大,请以编程方式设置ImageView 的maxheight。
    • 我已经考虑过了。但是当我在我的适配器上启动项目(因为它是一个视图寻呼机)并且我想知道我的文本视图的高度时,它返回 0...我怎样才能知道元素的高度并将 maxHeight 设置为我的 imageView ?
    猜你喜欢
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多