TextView没有定位在`CoordinatorLayout`中的ImagevVew下

问题描述 投票:1回答:3

我正在开发android新闻应用程序。在我的列表中,我实现了图像和TextView然而我不能把TextView放在ImageView下面。我用谷歌搜索它没有找到有用的信息。

在我的CoordinatorLayout示例下面:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content"/>

    <ImageView
        android:id="@+id/articleImage"
        android:layout_width="400dp"
        android:layout_height="185dp" />

     <TextView
         android:id="@id/articleAuthor"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="@string/article_author"/>
</android.support.design.widget.CoordinatorLayout>
java android android-coordinatorlayout
3个回答
1
投票

请看一下这里的代码 - > https://www.androidauthority.com/using-coordinatorlayout-android-apps-703720/

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.sample.foo.usingcoordinatorlayout.FabAndSnackbarActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp"
            app:title="@string/collapsing_toolbar">

            <ImageView
                android:id="@+id/toolbarImage"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                android:src="@drawable/bg"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin" />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="28sp"
            android:lineSpacingExtra="8dp"
            android:text="@string/long_latin"
            android:padding="@dimen/activity_horizontal_margin" />
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:src="@drawable/mascot_icon"
        app:layout_anchor="@id/appBar"
        app:layout_anchorGravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout>

0
投票

布局可能是这样的

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".view.club.detail.ActivityTopicDetail"
    >

<com.google.android.material.appbar.AppBarLayout
        android:id="@+id/cus_appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:theme="@style/AppTheme.AppBarOverlay"
        >

    <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            >

        <ImageView
            android:id="@+id/articleImage"
            android:layout_width="400dp"
            android:layout_height="185dp" />

    </com.google.android.material.appbar.CollapsingToolbarLayout>

</com.google.android.material.appbar.AppBarLayout>

<TextView
     android:id="@id/articleAuthor"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="@string/article_author"
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

0
投票

您的XML有两个问题。

  1. 有一个额外的LinearLayout标签永远不会关闭,所以这甚至不应该建立。你不应该需要它,应该能够删除它。
  2. 如果你想要在TextView下方的ImageView,你需要在TextView上设置约束,所以它就是这样做的。

所以至少,你需要这样的东西:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" // <- App namespace
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center">

    <ImageView
        android:id="@+id/articleImage"
        android:layout_width="400dp"
        android:layout_height="185dp" />

     <TextView
         android:id="@id/articleAuthor"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="@string/article_author"
         app:layout_constraintTop_toBottomOf="@+id/articleImage" /> // <- Constraint

</android.support.design.widget.CoordinatorLayout>

我强烈建议您查看ConstraintLayout文档:

https://developer.android.com/training/constraint-layout

https://developer.android.com/reference/android/support/constraint/ConstraintLayout

希望有所帮助! https://codelabs.developers.google.com/codelabs/constraint-layout/index.html#0

© www.soinside.com 2019 - 2024. All rights reserved.