Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ConstraintLayout中的TextView会导致父对象收缩_Android_Android Constraintlayout - Fatal编程技术网

Android ConstraintLayout中的TextView会导致父对象收缩

Android ConstraintLayout中的TextView会导致父对象收缩,android,android-constraintlayout,Android,Android Constraintlayout,在ConstraintLayout中正确定位TextView有一个问题,ConstraintLayout表示RecyclerView中的项布局 简单项布局由图像组成,并与父项的星形、小的子文件文本和标题文本(titleStr)对齐,这会导致对齐问题标题栏应位于图像和父端之间 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns

在ConstraintLayout中正确定位TextView有一个问题,ConstraintLayout表示RecyclerView中的项布局

简单项布局由图像组成,并与父项的星形、小的子文件文本和标题文本(
titleStr
)对齐,这会导致对齐问题<代码>标题栏应位于图像和父端之间

<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:layout_width="match_parent"
android:layout_height="140dp"
android:background="@color/white"
android:elevation="4dp"
android:foreground="?android:attr/selectableItemBackground"
android:paddingStart="8dp"
android:paddingTop="16dp"
android:paddingEnd="8dp"
android:paddingBottom="16dp">

<ImageView
    android:id="@+id/imageItem"
    android:layout_width="120dp"
    android:layout_height="match_parent"
    android:src="@drawable/banner_placeholder"
    app:layout_constraintStart_toStartOf="parent"
    tools:background="@color/colorPrimary"/>

<com.starcut.taskupeto.common.TextViewCustom
    android:id="@+id/subTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:textSize="12sp"
    app:fontName="AvenirLTStd-Roman.ttf"
    app:layout_constraintStart_toEndOf="@id/imageItem"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="4h ago" />

<android.support.v7.widget.AppCompatTextView
    android:id="@+id/titleStr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:textColor="@color/black"
    android:textSize="14sp"
    app:layout_constrainedWidth="true"
    app:layout_constraintStart_toEndOf="@+id/imageItem"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/subTitle"
    tools:text="Article title. This title can be big. How big you ask? Who knows?" />

另一种尝试是将
layout\u width
设置为
wrap\u content
,并使用
layout\u constrainedWidth=“true”
。在这种情况下,文本部分超过其父宽度。如果标题是短文本,则父级会再次将其包装
ConstraintLayout
使用的版本:
1.1.3

试试这个

您必须为TextView设置
0dp
,并设置结束约束,如
app:layout\u constraintEnd\u toEndOf=“parent”

试试这个

您必须为TextView设置
0dp
,并设置结束约束,如
app:layout\u constraintEnd\u toEndOf=“parent”

请检查下面的代码

 <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="140dp"
    android:background="@android:color/white"
    android:elevation="4dp"
    android:foreground="?android:attr/selectableItemBackground"
    android:paddingStart="8dp"
    android:paddingTop="16dp"
    android:paddingEnd="8dp"
    android:paddingBottom="16dp">

    <ImageView
        android:id="@+id/imageItem"
        android:layout_width="120dp"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintStart_toStartOf="parent"
        tools:background="@color/colorPrimary" />

    <androidx.appcompat.widget.AppCompatAutoCompleteTextView
        android:id="@+id/titleStr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:textColor="@color/colorAccent"
        android:textSize="14sp"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/imageItem"
        app:layout_constraintTop_toBottomOf="@+id/subTitle"
        tools:text="Article title. This title can be big. How big you ask? Who knows?" />

    <com.starcut.taskupeto.common.TextViewCustom
        android:id="@+id/subTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:textSize="12sp"
        app:fontName="AvenirLTStd-Roman.ttf"
        app:layout_constraintStart_toEndOf="@id/imageItem"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="4h ago" />

        </androidx.constraintlayout.widget.ConstraintLayout>

请检查下面的代码

 <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="140dp"
    android:background="@android:color/white"
    android:elevation="4dp"
    android:foreground="?android:attr/selectableItemBackground"
    android:paddingStart="8dp"
    android:paddingTop="16dp"
    android:paddingEnd="8dp"
    android:paddingBottom="16dp">

    <ImageView
        android:id="@+id/imageItem"
        android:layout_width="120dp"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintStart_toStartOf="parent"
        tools:background="@color/colorPrimary" />

    <androidx.appcompat.widget.AppCompatAutoCompleteTextView
        android:id="@+id/titleStr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:textColor="@color/colorAccent"
        android:textSize="14sp"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/imageItem"
        app:layout_constraintTop_toBottomOf="@+id/subTitle"
        tools:text="Article title. This title can be big. How big you ask? Who knows?" />

    <com.starcut.taskupeto.common.TextViewCustom
        android:id="@+id/subTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:textSize="12sp"
        app:fontName="AvenirLTStd-Roman.ttf"
        app:layout_constraintStart_toEndOf="@id/imageItem"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="4h ago" />

        </androidx.constraintlayout.widget.ConstraintLayout>

根据您在
0dp
(匹配约束)情况下的布局行为,我怀疑问题在于如何在适配器的
onCreateViewHolder
中膨胀
视图。如果您的电话如下所示:

val itemView = LayoutInflater.from(context).inflate(R.layout.item_layout, null, false)
这意味着您没有为视图层次定义父级。您应该使用
onCreateViewHolder
中的父
ViewGroup
参数:

val itemView = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false)

根据您在
0dp
(匹配约束)情况下的布局行为,我怀疑问题在于如何在适配器的
onCreateViewHolder
中膨胀
视图。如果您的电话如下所示:

val itemView = LayoutInflater.from(context).inflate(R.layout.item_layout, null, false)
这意味着您没有为视图层次定义父级。您应该使用
onCreateViewHolder
中的父
ViewGroup
参数:

val itemView = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false)


请共享屏幕截图项目视图单击链接,它们是共享的请检查下面的答案并接受是否对您有效。它不起作用,我已经尝试过,如我的后租赁共享屏幕截图项目视图中所述单击链接,它们是共享的请检查下面的答案并接受是否对您有效。它不起作用,我已经试过了,如我的post中所述,也不起作用,导致parent包装它的内容。你试过了吗?是的,我试过了,忘了在示例xml中添加约束,将更正post Now仅使用英语文本检查。因为另一种语言而剪切文本。也不起作用,会导致父对象包装其内容。您尝试过吗?是的,我尝试过,忘记在示例xml中添加该约束,将更正post NOW仅使用英语文本检查此问题。由于另一种语言而导致的文本剪切。我已经尝试过了,检查示例图像,我已经在帖子中上传了。文本超过父级,短文本导致父级收缩。我已经尝试过了,检查示例图像,我已在帖子中上传。文本超过父级,短文本导致父级再次收缩这是真的,未定义父级。现在可以了,谢谢。没错,父母没有定义。现在很好用了,谢谢。