0

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

RelativeLayout 的最大高度必须是所有屏幕高度的 50%。并且如果body layout的高度超过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>

更新:我添加了指南,但如果我的 framelayout 高度超过 50%,relativeLayout 高度必须降低,但事实并非如此:framelayout 的内容离开屏幕

4

4 回答 4

1

使用 指导线

<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" />
于 2018-10-19T05:56:47.727 回答
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!
}

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

于 2018-10-19T07:24:41.320 回答
0

添加Guideline您想要的方向。然后,使用该行为每个视图添加约束。

Guideline您可以使用此属性设置固定大小-app:layout_constraintGuide_beginapp:layout_constraintGuide_end.

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

这是使用的完整 xml 代码Guideline

<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>
于 2018-10-19T06:20:38.290 回答
0

一些新的属性来自约束布局 1.1.0 及以后。

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

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

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

参考这里https://stackoverflow.com/a/45652413/6151137

于 2019-10-23T11:57:06.443 回答