TextView有两行时,包含的ConstraintLayout不能固定位置

时间:2019-06-12 08:45:24

标签: android xml android-constraintlayout

我有COAL,其中包含library(ggplot2) library(dplyr) MU17 %>% mutate(Fuel = factor(Fuel), Month = factor(Month,levels = month.abb)) %>% group_by(Month, Month2, Fuel) %>% summarise(n = n()) %>% group_by(Month) %>% mutate(p = n / sum(n), p2 = paste(formatC(p*100, digits = 2, format = "fg"),"%",sep = ""), pos = cumsum(p) - (0.5 * p)) %>% ggplot(aes(x = Month, y = p, fill = factor(Fuel, levels = rev(levels(Fuel))))) + geom_col(width = 0.5, position = "fill") + scale_y_continuous(limits = c(0, 1), breaks = c(-.5,-.25,0,.25,.5,.75,1), expand = c(0, 0), labels = scales::percent) + scale_fill_manual(breaks = c("COAL", "GAS","HYDRO","BIOMASS"), values = c("black","yellow","blue","green")) + geom_text(aes(label = p2, y = pos), position = "identity", vjust = 0.5, colour = ifelse(data$Fuel == "COAL" | data$Fuel == "HYDRO", "#FFFFFF", "#000000")) + labs(x = "2017" , y = "Marginal Fuel Between HE8 & HE20") + labs(fill = "Fuel Type") ,还包含其他ConstraintLayout。之所以包含其他约束,是因为在该项目中,我使用的是旧版本的支持库(26.1.0),其中不包含组,并且目前无法对其进行升级。

TextView

我直接从代码中设置了ConstraintLayout,并且在不同的设备上,它可能只有一两行。当它有两行时,包含的部分将移动该行的高度,但应调整大小,因为它具有<?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:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/topTitleTextView" style="@style/TextBigMinus" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/margin_normal" android:layout_marginRight="@dimen/margin_normal" android:layout_marginBottom="@dimen/margin_normal" android:gravity="center_horizontal" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" /> <include android:id="@+id/included_part" layout="@layout/included_part" android:layout_height="0dp" android:layout_width="0dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/topTitleTextView" app:layout_constraintBottom_toBottomOf="parent"/> </android.support.constraint.ConstraintLayout>

是什么原因导致的?如何解决?

4 个答案:

答案 0 :(得分:0)

尝试删除app:layout_constraintBottom_toBottomOf="parent" 并设置android:layout_height="wrap_content"

答案 1 :(得分:0)

这应该可以按照您的要求正常工作

<android.support.constraint.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/topTitleTextView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:gravity="center_horizontal"
            app:layout_constraintBottom_toTopOf="@+id/included_part"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />

        <include
            android:id="@+id/included_part"
            layout="@layout/included_part"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/topTitleTextView"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>

答案 2 :(得分:0)

由于您希望包含的部分随着文本行高度的变化而移动,因此也要调整其大小以适合可用的屏幕,因此app:layout_constraintBottom_toBottomOf="parent"不适用于包含视图的布局({{1} },TextView等),以dp或sp为单位嵌套包含固定高度值的包含布局。确保包含的视图在ImageView内将每个height属性都设置为0dp,然后在固定约束字段的地方使用以下代码:

ConstraintLayout

答案 3 :(得分:0)

文本视图底部是对父级的约束,而包含的部件底部也是对父级的约束。这将导致视图的某些部分相互重叠。尝试删除文本视图属性上的app:layout_constraintBottom_toBottomOf =“ parent”。