【问题标题】:TextView not positioning under ImagevVew in `CoordinatorLayout`TextView 未在“CoordinatorLayout”中的 ImagevVew 下定位
【发布时间】:2019-09-17 11:46:29
【问题描述】:

我正在开发安卓新闻应用。在我的列表中,我已经实现了图像和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


    【解决方案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>
    

    【讨论】:

      【解决方案2】:

      您的 XML 有两个问题。

      1. 有一个额外的LinearLayout 标记永远不会关闭,所以这甚至不应该建立。你不应该需要这个并且应该可以删除它。
      2. 如果您希望TextViewImageView 下方,您需要在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

      【讨论】:

      • "layout_constraintTop_toBottomOf" 当你想在 coordinatorlayout 中的 appbar 下放置视图时,它就像一个魅力
      【解决方案3】:

      布局可能是这样的

      <?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>
      

      【讨论】:

        猜你喜欢
        • 2019-03-11
        • 1970-01-01
        • 2017-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-02
        • 2023-03-08
        相关资源
        最近更新 更多