Textview 未显示在 LinearLayout 中
Textview not showing inside LinearLayout
我有以下 XML 文件 -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/white"
android:orientation="vertical"
android:padding="20dp">
<ImageView
android:id="@+id/products_row_item_product_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/products_row_item_product_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fragment_marketplace_products_row_item_product_title"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="@+id/products_row_item_description_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/products_row_item_product_description" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/very_light_grey"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/products_row_item_price_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
tools:text="9.90" />
<ImageView
android:id="@+id/products_row_item_heart_image_view"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:src="@drawable/like_heart" />
<TextView
android:id="@+id/products_row_item_likes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end"
android:text="@string/fragment_marketplace_products_row_item_likes"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
正在创建以下布局 -
但问题是我得到了以下结果 -
我有 2 个问题 -
1) 为什么我在TextView中看不到价格?
2) 我想设置在每个行项目之间可见的背景颜色。考虑到我当前的布局,我该怎么做?试图给 Recyclerview 一个背景颜色,但没有任何反应,就像我在每个单元格之间有 0 个空格,这似乎不正确。
更改文本视图的宽度以包裹内容并将 tool:text 替换为 android:text
<TextView
android:id="@+id/products_row_item_price_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:text="9.90" />
第二个答案:
在父线性布局结束前使用视图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/white"
android:orientation="horizontal"
android:padding="20dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation = "vertical">
<ImageView
android:id="@+id/products_row_item_product_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/products_row_item_product_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fragment_marketplace_products_row_item_product_title"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="@+id/products_row_item_description_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/products_row_item_product_description" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/very_light_grey"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/products_row_item_price_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
tools:text="9.90" />
<ImageView
android:id="@+id/products_row_item_heart_image_view"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:src="@drawable/like_heart" />
<TextView
android:id="@+id/products_row_item_likes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end"
android:text="@string/fragment_marketplace_products_row_item_likes"
android:textSize="20sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/very_light_grey"/>
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match"
android:background="@color/very_light_grey"/>
</LinearLayout>
在您的布局中,您使用 tools:text="9.90"
作为价格 TextView
。看来您可能没有手动设置文本。 tools:text
只会出现在 Android Studio 内的预览中。
也许还有其他类似@kam1234 在他的回答中建议的内容
要显示项目之间的线条,请使用
recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
我有以下 XML 文件 -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/white"
android:orientation="vertical"
android:padding="20dp">
<ImageView
android:id="@+id/products_row_item_product_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/products_row_item_product_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fragment_marketplace_products_row_item_product_title"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="@+id/products_row_item_description_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/products_row_item_product_description" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/very_light_grey"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/products_row_item_price_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
tools:text="9.90" />
<ImageView
android:id="@+id/products_row_item_heart_image_view"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:src="@drawable/like_heart" />
<TextView
android:id="@+id/products_row_item_likes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end"
android:text="@string/fragment_marketplace_products_row_item_likes"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
正在创建以下布局 -
但问题是我得到了以下结果 -
我有 2 个问题 -
1) 为什么我在TextView中看不到价格?
2) 我想设置在每个行项目之间可见的背景颜色。考虑到我当前的布局,我该怎么做?试图给 Recyclerview 一个背景颜色,但没有任何反应,就像我在每个单元格之间有 0 个空格,这似乎不正确。
更改文本视图的宽度以包裹内容并将 tool:text 替换为 android:text
<TextView
android:id="@+id/products_row_item_price_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:text="9.90" />
第二个答案:
在父线性布局结束前使用视图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/white"
android:orientation="horizontal"
android:padding="20dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation = "vertical">
<ImageView
android:id="@+id/products_row_item_product_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/products_row_item_product_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fragment_marketplace_products_row_item_product_title"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="@+id/products_row_item_description_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/products_row_item_product_description" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/very_light_grey"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/products_row_item_price_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
tools:text="9.90" />
<ImageView
android:id="@+id/products_row_item_heart_image_view"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:src="@drawable/like_heart" />
<TextView
android:id="@+id/products_row_item_likes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end"
android:text="@string/fragment_marketplace_products_row_item_likes"
android:textSize="20sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/very_light_grey"/>
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match"
android:background="@color/very_light_grey"/>
</LinearLayout>
在您的布局中,您使用 tools:text="9.90"
作为价格 TextView
。看来您可能没有手动设置文本。 tools:text
只会出现在 Android Studio 内的预览中。
也许还有其他类似@kam1234 在他的回答中建议的内容
要显示项目之间的线条,请使用
recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));