我正在开发android新闻应用。并且在我的列表中,我已经实现了图像和TextView
,但是我不能将TextView
放在ImageView
下面。我用Google搜索它找不到有用的信息。
在我的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>
答案 0 :(得分: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>
答案 1 :(得分: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>
答案 2 :(得分:0)
您的XML有两个问题。
LinearLayout
标记从未关闭过,因此甚至不应该建立。您不需要并且应该可以删除它。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