我的TextView
布局中有一个CardView
,但是它既没有显示在布局预览中,也没有显示在实时设备上。类似的问题也有“删除工具:android:text =“ ..”“之类的答案,等等,但这不是这里的问题。有什么想法吗?
这就是我要使用cardView的目的:
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:orientation="horizontal"
android:weightSum="100"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/explore_desc">
<androidx.cardview.widget.CardView
android:id="@+id/localEvents"
android:layout_width="150dp"
android:layout_height="75dp"
android:layout_marginEnd="10dp"
android:layout_weight="50"
app:cardCornerRadius="12dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:fontFamily="@font/montserrat_bold"
android:text="@string/local_events"
android:textColor="@android:color/white"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/beach_bg_placeholder" />
</androidx.cardview.widget.CardView>
</LinearLayout>
答案 0 :(得分:2)
我尝试了您的代码,以下代码效果很好。由于ImageView
和TextView
堆积在LinearLayout
内部,因此文本没有显示在ImageView
的顶部。在ImageView将其放置在ImageView上方之后定义TextView,如下所示:
<?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="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:orientation="horizontal"
android:weightSum="100"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<androidx.cardview.widget.CardView
android:id="@+id/localEvents"
android:layout_width="150dp"
android:layout_height="75dp"
android:layout_marginEnd="10dp"
android:layout_weight="50"
app:cardCornerRadius="12dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/test" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Local Events"
android:textColor="@android:color/white"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:elevation="@dimen/_10sdp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
屏幕截图:
答案 1 :(得分:0)
您的书写应如下所示
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="TextV'ew"
android:textColor="@color/colorPrimary"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="16dp"
android:background="@drawable/america"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.cardview.widget.CardView>
答案 2 :(得分:0)
问题在于CardView内的ImageView隐藏了TextView。因此只需将可绘制图像用作cardview内RelativeLayout的背景即可,而cardview现在是TextView的父级。
所以尝试一下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<androidx.cardview.widget.CardView
android:id="@+id/cardView1"
android:layout_width="150dp"
android:layout_height="75dp"
app:cardCornerRadius="12dp"
android:layout_margin="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/beach_bg_placeholder">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Local Events"
android:layout_centerInParent="true"
android:textColor="@android:color/white"
android:textSize="16sp"
android:textStyle="bold"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/cardView2"
android:layout_width="150dp"
android:layout_height="75dp"
app:cardCornerRadius="12dp"
android:layout_margin="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/beach_bg_placeholder">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Local Events"
android:layout_centerInParent="true"
android:textColor="@android:color/white"
android:textSize="16sp"
android:textStyle="bold"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<androidx.cardview.widget.CardView
android:id="@+id/cardView3"
android:layout_width="150dp"
android:layout_height="75dp"
app:cardCornerRadius="12dp"
android:layout_margin="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/beach_bg_placeholder">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Local Events"
android:layout_centerInParent="true"
android:textColor="@android:color/white"
android:textSize="16sp"
android:textStyle="bold"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/cardView4"
android:layout_width="150dp"
android:layout_height="75dp"
app:cardCornerRadius="12dp"
android:layout_margin="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/beach_bg_placeholder">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Local Events"
android:layout_centerInParent="true"
android:textColor="@android:color/white"
android:textSize="16sp"
android:textStyle="bold"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>