gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:51:47 27 4
gpt4 key购买 nike

我在制作此屏幕 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com