简体   繁体   English

Android约束布局对齐方式

[英]Android Constraint Layout alignemnt

Im facing an issue with ConstraintLayout. 我遇到了ConstraintLayout问题。

I have a view which has 3 textviews T1, T2 and T3. 我有一个具有3个文本视图T1,T2和T3的视图。

Precondition: T1,T2 is having 4dp margin top. 前提条件:T1,T2具有4dp的余量顶部。

Problem: T3 needs to be top should be aligned to topmost view. 问题:T3需要位于顶部,应与顶部视图对齐。 Condition is T1 will be visible or gone based on certain condition. 条件是T1将根据特定条件可见或消失。
If T1 is visible the margintop will be 8dp and when T1 is not visible T2 will have 12dp margintop. 如果T1可见,则边距顶部将为8dp;当T1不可见时,T2将具有12dp边距。

<?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/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="T1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        android:text="T2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />


    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="T3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/textView" />


</android.support.constraint.ConstraintLayout>

Is this possible by XML or I need to configure programmatically? XML是否可能实现此功能,或者我需要以编程方式进行配置?

I don't know exactly your use case,but you can achieve this setting view to invisible instead of gone. 我不确切知道您的用例,但是您可以将此设置视图实现为不可见而不是消失。

View.GONE This view is invisible, and it doesn't take any space for layout purposes. View.GONE该视图是不可见的,并且不占用任何空间用于布局。

View.INVISIBLE This view is invisible, but it still takes up space for layout purposes. View.INVISIBLE此视图是不可见的,但仍会占用空间以进行布局。

You can't achieve this only with XML, what you did is create a screen using constraint layout. 您不能仅使用XML来实现这一点,您要做的就是使用约束布局创建屏幕。
If you want to add some logic to change the position of some view under certain conditions you will have to write it programmatically. 如果要添加某些逻辑以在某些条件下更改某些视图的位置,则必须以编程方式编写它。
Here is a good thread about the subject. 这是有关该主题的一个好话题。

You can use layout_goneMarginTop to have a different margin when your view is GONE . 当视图为GONE时,可以使用layout_goneMarginTop来具有不同的边距。

You can also use SequenceLayout for better flexibility on managing your layout. 您也可以使用SequenceLayout在管理布局时获得更大的灵活性。 Use visibilityElement on each Span you want to be gone when your related view is gone. 当相关视图消失后,在要消失的每个Span上使用visibilityElement

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM