java - 如何使用 ConstraintLayout 创建此布局

标签 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>

更新:我添加了指导方针,但如果我的framelayout高度超过50%,relativeLayout高度必须减小,但事实并非如此:framelayout的内容超出屏幕

最佳答案

试试这个代码。

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!
}

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

关于java - 如何使用 ConstraintLayout 创建此布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52886517/

相关文章:

android - Windows- 无法启动 Git : Probably the path to Git executable is not valid

具有底部导航的Android架构导航组件?

java - 带有 Glassfish 的 Geotools : Failed to connect to the EPSG database

java - Freemarker 迭代 hashmap 键

java - 从字符串转换为 UTC 格式

android - 如何把这个LinearLayout和RelativeLayout变成ConstraintLayout呢?

android - 如何在 ConstraintLayout 中水平放置两个 View ?

Java Swing UI 对齐问题

java - 具有异常 unicode 字符和单词边界的正则表达式模式

android - 如何在约束布局中创建水平 ScrollView