以编程方式设置的 Android 约束布局不正确
Posted
技术标签:
【中文标题】以编程方式设置的 Android 约束布局不正确【英文标题】:Android constraint layout set programmatically is incorrect 【发布时间】:2021-11-22 01:13:41 【问题描述】:所以我一直在尝试以编程方式设置约束布局。 原来的版面设计是这个
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_
android:layout_
android:layout_weight="0.3">
<androidx.cardview.widget.CardView
android:layout_
android:layout_
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.48"
app:cardCornerRadius="20dp"
app:cardElevation="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/card1_layout"
android:layout_
android:layout_
android:background="@drawable/up_arrow">
<TextView
android:id="@+id/card1_company"
android:layout_
android:layout_
android:layout_margin="10dp"
android:text="CompanyName"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5" />
<TextView
android:id="@+id/card1_point"
android:layout_
android:layout_
android:layout_margin="10dp"
android:text="CompanyPoint"
android:textColor="@color/black"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/card1_company"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5" />
<TextView
android:id="@+id/card1_des"
android:layout_
android:layout_
android:layout_margin="10dp"
android:text="SOME COMPANY DESCRIPTION"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_default="wrap"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_
android:layout_
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.48"
app:cardCornerRadius="20dp"
app:cardElevation="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/card2_layout"
android:layout_
android:layout_
android:background="@drawable/up_arrow">
<TextView
android:id="@+id/card2_company"
android:layout_
android:layout_
android:layout_margin="10dp"
android:text="CompanyName"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5" />
<TextView
android:id="@+id/card2_point"
android:layout_
android:layout_
android:layout_margin="10dp"
android:text="CompanyPoint"
android:textColor="@color/black"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/card2_company"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5" />
<TextView
android:id="@+id/card2_des"
android:layout_
android:layout_
android:layout_margin="10dp"
android:text="SOME COMPANY DESCRIPTION"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_default="wrap"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
<View
android:layout_
android:layout_
android:layout_weight="0.01"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_
android:layout_
android:layout_weight="0.3">
<androidx.cardview.widget.CardView
android:layout_
android:layout_
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.48"
app:cardCornerRadius="20dp"
app:cardElevation="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/card3_layout"
android:layout_
android:layout_
android:background="@drawable/down_arrow">
<TextView
android:id="@+id/card3_company"
android:layout_
android:layout_
android:layout_margin="10dp"
android:text="CompanyName"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5" />
<TextView
android:id="@+id/card3_point"
android:layout_
android:layout_
android:layout_margin="10dp"
android:text="CompanyPoint"
android:textColor="@color/black"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/card3_company"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5" />
<TextView
android:id="@+id/card3_des"
android:layout_
android:layout_
android:layout_margin="10dp"
android:text="SOME COMPANY DESCRIPTION"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHeight_default="wrap"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_
android:layout_
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.48"
app:cardCornerRadius="20dp"
app:cardElevation="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/card4_layout"
android:layout_
android:layout_
android:background="@drawable/down_arrow">
<TextView
android:id="@+id/card4_company"
android:layout_
android:layout_
android:layout_margin="10dp"
android:text="CompanyName"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5" />
<TextView
android:id="@+id/card4_point"
android:layout_
android:layout_
android:layout_margin="10dp"
android:text="CompanyPoint"
android:textColor="@color/black"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/card4_company"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5" />
<TextView
android:id="@+id/card4_des"
android:layout_
android:layout_
android:layout_margin="10dp"
android:text="SOME COMPANY DESCRIPTION"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHeight_default="wrap"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
<View
android:layout_
android:layout_
android:layout_weight="0.01"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_
android:layout_
android:layout_weight="0.3">
<androidx.cardview.widget.CardView
android:layout_
android:layout_
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.48"
app:cardCornerRadius="20dp"
app:cardElevation="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/card5_layout"
android:layout_
android:layout_
android:background="@drawable/up_arrow">
<TextView
android:id="@+id/card5_company"
android:layout_
android:layout_
android:layout_margin="10dp"
android:text="CompanyName"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5" />
<TextView
android:id="@+id/card5_point"
android:layout_
android:layout_
android:layout_margin="10dp"
android:text="CompanyPoint"
android:textColor="@color/black"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/card5_company"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5" />
<TextView
android:id="@+id/card5_des"
android:layout_
android:layout_
android:layout_margin="10dp"
android:text="SOME COMPANY DESCRIPTION"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_default="wrap"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_
android:layout_
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.48"
app:cardCornerRadius="20dp"
app:cardElevation="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/card6_layout"
android:layout_
android:layout_
android:background="@drawable/down_arrow">
<TextView
android:id="@+id/card6_company"
android:layout_
android:layout_
android:layout_margin="10dp"
android:text="CompanyName"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5" />
<TextView
android:id="@+id/card6_point"
android:layout_
android:layout_
android:layout_margin="10dp"
android:text="CompanyPoint"
android:textColor="@color/black"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/card6_company"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5" />
<TextView
android:id="@+id/card6_des"
android:layout_
android:layout_
android:layout_margin="10dp"
android:text="SOME COMPANY DESCRIPTION"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHeight_default="wrap"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
因此,根据公司点,设置布局。如果积分为负数,则将公司名称和积分文本设置在父项的末尾,将描述设置为开始,如图所示,如果积分为正数,则反之。
但是当我以编程方式设置它时,对于一些进行更改的布局,文本设置得很奇怪,例如查看以编程方式设置的下一张图片
如果布局发生变化,那么某些文本的位置会更靠近中心 例如,请注意第二张图片、垂直第二布局和第三布局中的标识差异。 第三个布局不必更改,因此其位置与未以编程方式设置的第一张图片相同。然而,第二个布局是确定的,因为它是通过编程更改的
以编程方式设置布局的代码
if(arrayList.get(0).getPoints() < 0)
constraintLayout1.setBackgroundResource(R.drawable.down_arrow);
company1Name.setGravity(Gravity.END);
company1point.setGravity(Gravity.END);
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) company1Name.getLayoutParams();
params.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
params = (ConstraintLayout.LayoutParams) company1point.getLayoutParams();
params.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
params = (ConstraintLayout.LayoutParams) company1desc.getLayoutParams();
params.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
else
constraintLayout1.setBackgroundResource(R.drawable.up_arrow);
company1desc.setGravity(Gravity.END);
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) company1Name.getLayoutParams();
params.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
params = (ConstraintLayout.LayoutParams) company1point.getLayoutParams();
params.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
params = (ConstraintLayout.LayoutParams) company1desc.getLayoutParams();
params.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
如何解决这个问题?
【问题讨论】:
看起来像是重力问题。尝试调用 company1Name.setGravity(Gravity.START); company1point.setGravity(Gravity.START);在其他部分。 我什至尝试在上面的代码中改变重力,即使它显示出奇怪的布局。 您只是将其更改为 END。您是否尝试在 else 部分中将其设置为 START? 所以检查第一个布局,它有负点,所以必须在最后设置 textviews。但即使在最后它被识别为远离父级的末尾,请检查第一张图片和第二张图片中负面布局的差异 你应该使用constraintSet
来设置约束
【参考方案1】:
看起来您正在为视图设置附加约束,但没有删除原始约束。换句话说,您正在为视图设置开始和结束约束,这将使视图在约束之间居中。我不认为重力有任何影响。
如果您需要将视图向右移动,请移除左侧约束并设置右侧。如果要将视图向左移动,请设置左侧约束并删除右侧。您还可以考虑为视图设置两个约束并使用水平偏差将其向左或向右移动。这可能是更简单的方法。
您可以通过将参数设置为 View.NO_ID 来删除带有参数的约束,但我对此不是 100% 确定的。我认为使用ConstraintSet 会更好。另外,请参阅here 以获取使用示例。
【讨论】:
这行得通。非常感谢:)以上是关于以编程方式设置的 Android 约束布局不正确的主要内容,如果未能解决你的问题,请参考以下文章
约束布局中以编程方式添加的按钮将忽略约束 - Android