java - wrap_content 或 match_parent 未给出正确的结果

标签 java android android-layout

friend 们,我试图将 view1 的高度设置为填充文本 textView1 的高度,但它不起作用。 view1 因此无限长,超过了文本的高度,占据了整个屏幕,但我需要文本的末尾。

<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/cv">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         android:weightSum="3"
        android:background="@android:color/white">
         <View
            android:id="@+id/view1"
            android:background="@drawable/rectangle_with_color"
            android:layout_height="wrap_content"
            android:layout_width="16dp" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="3dp"
            android:text="Не следует, однако забывать, что начало повседневной работы по формированию позиции представляет собой интересный эксперимент проверки новых предложений. С другой стороны реализация намеченных плановых заданий представляет собой интересный эксперимент проверки форм развития."
            android:gravity="top"
            />

    </RelativeLayout>
</androidx.cardview.widget.CardView>

矩形_with_color.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid
        android:color="#E41300" />
</shape>

enter image description here

最佳答案

修复了您的错误。请使用下面的代码:

<View
            android:id="@+id/view1"
            android:background="@drawable/blank_checkbox"
            android:layout_height="wrap_content"
            android:layout_width="16dp"
            android:layout_alignBottom="@+id/textView1"/> //make this change

You need to set alignBottom property to mark its end.

根据文档:

alignBottom: Makes the bottom edge of this view match the bottom edge of the given anchor view ID. Accommodates bottom margin.

关于java - wrap_content 或 match_parent 未给出正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61275244/

相关文章:

android - 混合 PhoneGap 和 native 应用程序 View

java - 显示 Java 文件而不是 XML 文件中的文本 (Android Studio)

java - 将文档写入内部存储

java - 我需要同步 writeObject() 吗?

java - Gson 字符串到对象

android - 从非接触式阅读器获取信息(例如金额、货币......)

Android - 多屏支持问题

android - RecyclerView : Inconsistency detected. 无效的项目位置

java - 确定使用的库以减少 JAR 文件的大小

java - 将 espresso View 匹配器与 hamcrest 匹配器一起使用