2016-12-31 170 views
4

我试图将具有4个相同大小的按钮的水平LinearLayout转换为ConstraintLayout。 问题是,当我在LinearLayout中设置一个或多个按钮到android:visibility="gone"时,其余按钮的大小将调整为占用整个空间(全部大小相同),并在ConstraintLayout中删除按钮,但仍占用空间。将LinearLayout转换为ConstraintLayout问题

编辑:根据应用程序状态,不同的按钮将是可见的。

我需要更改什么,以便ConstraintLayout的行为与LinearLayout相似?

编辑:我发现ConstraintLayout(约束引用)的错误,所以我更新它和图像(问题仍然存在)。

LinearLayout XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 

    <Button 
     android:id="@+id/b1" 
     android:text="B1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="50" 
     /> 

    <Button 
     android:id="@+id/b2" 
     android:text="B2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="50" 
     android:visibility="gone" 
     /> 

    <Button 
     android:id="@+id/b3" 
     android:text="B3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="50" 
     /> 

    <Button 
     android:id="@+id/b4" 
     android:text="B4" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="50" 
     /> 
</LinearLayout> 

编辑:ConstraintLayout XML:

<?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" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Button 
     android:id="@+id/b1" 
     android:text="B1" 
     android:layout_width="0px" 
     android:layout_height="wrap_content" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toLeftOf="@+id/b2" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintHorizontal_weight="1" 
     /> 

    <Button 
     android:id="@+id/b2" 
     android:text="B2" 
     android:layout_width="0px" 
     android:layout_height="wrap_content" 
     android:visibility="gone" 
     app:layout_constraintLeft_toRightOf="@+id/b1" 
     app:layout_constraintRight_toLeftOf="@+id/b3" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintHorizontal_weight="1" 
     /> 

    <Button 
     android:id="@+id/b3" 
     android:text="B3" 
     android:layout_width="0px" 
     android:layout_height="wrap_content" 
     app:layout_constraintLeft_toRightOf="@+id/b2" 
     app:layout_constraintRight_toLeftOf="@+id/b4" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintHorizontal_weight="1" 
     android:layout_marginTop="0dp"/> 

    <Button 
     android:id="@+id/b4" 
     android:text="B4" 
     android:layout_width="0px" 
     android:layout_height="wrap_content" 
     app:layout_constraintLeft_toRightOf="@+id/b3" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintHorizontal_weight="1" 
     /> 
</android.support.constraint.ConstraintLayout> 

LinearLayout vs ConstraintLayout ConstraintLayout blueprint

+0

为什么要替换线性布局,如果它工作正常?它慢吗?它吃了很多内存吗? – pskink

+0

我有一个复杂的布局结构,所以我将它转换为约束布局。这是我没有设法转换的一部分。 – Hanan

+0

我知道我可以在约束布局中嵌入线性布局,但是想避免它(根据我的理解约束布局的目的是去除深布局层次结构)。 – Hanan

回答

1

你也许可以改变你的布局是这样的:

<?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" 
    android:orientation="horizontal"> 

    <Button 
     android:id="@+id/b1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="B1" 
     app:layout_constraintBaseline_toBaselineOf="@+id/b3" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toLeftOf="@+id/b3" 
     tools:layout_constraintBaseline_creator="1" 
     tools:layout_constraintLeft_creator="1" 
     tools:layout_constraintRight_creator="1" /> 

    <Button 
     android:id="@+id/b2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="B2" 
     android:visibility="gone" 
     app:layout_constraintBottom_toTopOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     tools:layout_constraintBottom_creator="1" 
     tools:layout_constraintLeft_creator="1" 
     tools:layout_constraintRight_creator="1" 
     tools:layout_constraintTop_creator="1" /> 

    <Button 
     android:id="@+id/b3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginEnd="128dp" 
     android:layout_marginLeft="128dp" 
     android:layout_marginRight="128dp" 
     android:layout_marginStart="128dp" 
     android:text="B3" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     tools:layout_constraintLeft_creator="1" 
     tools:layout_constraintRight_creator="1" 
     tools:layout_constraintTop_creator="1" /> 

    <Button 
     android:id="@+id/b4" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="B4" 
     app:layout_constraintBaseline_toBaselineOf="@+id/b3" 
     app:layout_constraintLeft_toRightOf="@+id/b3" 
     app:layout_constraintRight_toRightOf="parent" 
     tools:layout_constraintBaseline_creator="1" 
     tools:layout_constraintLeft_creator="1" 
     tools:layout_constraintRight_creator="1" /> 

</android.support.constraint.ConstraintLayout> 

如果您在将现有布局切换到ConstraintLayout时遇到困难,可以继续尝试使用Android Studio的内部设计工具来帮助您。您可以切换到设计选项卡并打开组件树窗口,右键单击要转换的元素并选择转换为ConstraintLayout

+0

当B2消失时,这看起来不错,但是如果我隐藏了一个不同的按钮,它就不能正常工作。我认为它不是很清楚(我也会更新这个问题),但是通过编程我可以根据应用状态改变按钮的可见性。 – Hanan

+1

试图使用AndroidStudio转换工具没有解决问题。 – Hanan

1

通过转到布局编辑器中的设计选项卡,可以将任何布局转换为ConstraintLayout

+1

我知道,但问题是,当您将按钮的可见性更改为“GONE”时,它无法正确运行。 – Hanan