android - 在我的手机上运行应用程序时不显示垂直线 View

标签 android xml android-layout android-studio

我想在 ListView 中的每个项目的开头制作一条垂直线,它在 android studio 预览屏幕上看起来很棒,如 here 所示。

但是当我在手机上运行它时它没有出现,知道为什么吗? screenshoot from my phone

这是 item_view.xml 文件的 XML 脚本:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:maxHeight="80dp"
    android:orientation="vertical"
    android:padding="8dp">
<View
    android:id="@+id/line2"
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark"
    app:layout_constraintLeft_toLeftOf="parent" />

<TextView
    android:id="@+id/tv_item_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:maxLines="1"
    android:singleLine="true"
    android:text="Tesla Stock's Terrible Week: A Buying Opportunity?"
    android:textColor="@color/colorAccent"
    android:textSize="18dp"
    android:textStyle="bold"
    app:layout_constraintLeft_toRightOf="@+id/line2"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/tv_item_by"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:text="By"
    app:layout_constraintStart_toStartOf="@+id/tv_item_title"
    app:layout_constraintTop_toTopOf="@+id/tv_item_name"
    tools:layout_conversion_absoluteHeight="19dp"
    tools:layout_conversion_absoluteWidth="336dp" />

<TextView
    android:id="@+id/tv_item_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:ellipsize="end"
    android:text="Fool.com"
    app:layout_constraintStart_toEndOf="@+id/tv_item_by"
    app:layout_constraintTop_toBottomOf="@+id/tv_item_title" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Read More"
    android:textColor="@color/colorPrimaryDark"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="@+id/tv_item_name"
    app:layout_constraintEnd_toStartOf="@+id/iv_arrow"
    app:layout_constraintTop_toTopOf="@+id/tv_item_name" />

<ImageView
    android:id="@+id/iv_arrow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_arrow"
    app:layout_constraintBottom_toBottomOf="@+id/textView2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="@+id/textView2" />

</android.support.constraint.ConstraintLayout>

我没有更改 java 代码的任何属性。 谢谢。

最佳答案

出现问题是因为您使用的 ConstraintLayout 有自己的规则。

而不是使用:

<View
android:id="@+id/line2"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
app:layout_constraintLeft_toLeftOf="parent" />

使用:

<View
android:id="@+id/line2"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="@color/colorPrimaryDark"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />

基本上,如果您在 ConstraintLayout's Dimensions Constraints 上查看此文档,您会看到它提到不使用 MATCH_PARENT 而是使用 MATCH_CONSTRAINT(又名 0dp)。

此外,您需要添加顶部和底部约束,以便 ConstraintLayout 知道如何调整其高度。

关于android - 在我的手机上运行应用程序时不显示垂直线 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51236839/

相关文章:

java - 自 API 28 起无法写入文件

javascript - 使用sendIntent.putExtras(bundle)的空页面,页面未填充Android studio

xml - CDATA 的正则表达式是什么

java - 在 xml 字符串的第二行添加 xml-stylesheet

java - 如何导入预构建的 *.so 库并从新项目中调用它?

Android Studio Gradle 构建非常慢

jquery - 如何在 Chrome 24 和 jQuery 1.8.2 中解析命名空间 XML?

android - View 中 setWillNotDraw(false) 的行为

Android ScrollView fillViewport 不工作

android - 如何在 Android Studio 预览中预览 ScrollView?