如何使用ConstraintLayout

时间:2018-10-19 05:47:46

标签: java android android-constraintlayout

我在制作此屏幕xml时遇到问题:problem

RelativeLayout的最大高度必须为所有屏幕高度的50%。 并且,如果车身布局的高度大于50%,则必须降低相对布局的高度。我不明白如何将相对布局max_height设置为50%。谢谢。 屏幕的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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="@id/guide_line">

        <LogoView
            android:id="@+id/logo_view"
            android:layout_width="match_parent"
            android:layout_height="144dp"
            android:layout_alignParentBottom="true"
            app:lv_textColor="@android:color/white" />

    </RelativeLayout>

    <android.support.constraint.Guideline
        android:id="@+id/guide_line"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />


    <FrameLayout
        android:id="@+id/fl_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guide_line" />

        </android.support.constraint.ConstraintLayout>

更新:我添加了准则,但是如果我的框架布局高度超过50%,则必须降低relativeLayout高度,但并非如此:框架布局的内容不在屏幕上

4 个答案:

答案 0 :(得分:1)

使用Guide Line

<android.support.constraint.Guideline
            android:id="@+id/centerMargin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent=".50"
            app:layout_constraintStart_toStartOf="parent" />

答案 1 :(得分:1)

尝试此代码。

activity_main.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/fl_container"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LogoView
            android:id="@+id/logo_view"
            android:layout_width="match_parent"
            android:layout_height="144dp"
            android:layout_alignParentBottom="true"
            app:lv_textColor="@android:color/white" />

    </RelativeLayout>

    <FrameLayout
        android:id="@+id/fl_container"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DisplayMetrics dm = new DisplayMetrics();
    WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(dm);
    int mainHeight = dm.heightPixels;
    Log.i(TAG, "mainHeight: " + mainHeight);

    FrameLayout flLayout = findViewById(R.id.fl_container);
    flLayout.setMinimumHeight(mainHeight / 2); // <-- Set pixel value, not dp!
}

我认为它并不是高度的一半,但是这种方式将是您想要的解决方案。

答案 2 :(得分:0)

以所需的方向添加Guideline。然后,在该视图的每一行添加约束。

您可以使用此属性-Guidelineapp:layout_constraintGuide_begin设置固定大小app:layout_constraintGuide_end

此外,如果您正在考虑使用ConstraintLayout,那么match_constraint(= 0dp)match_parent更好。在这种情况下,必须将与父项的约束设置在一起。

这是使用Guideline的完整xml代码。

<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">

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline_horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LogoView
            android:id="@+id/logo_view"
            android:layout_width="match_parent"
            android:layout_height="144dp"
            android:layout_alignParentBottom="true"
            android:src="@mipmap/ic_launcher" />

    </RelativeLayout>

    <FrameLayout
        android:id="@+id/fl_container"
        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/guideline_horizontal" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />

</android.support.constraint.ConstraintLayout>

答案 3 :(得分:0)

从约束布局1.1.0开始,有一些新属性。

要将布局高度设置为屏幕的50%,您可以这样做:

android:layout_height="0dp"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent=".5"

这里.5被认为是屏幕高度的50%。

请在此处https://stackoverflow.com/a/45652413/6151137