Java 如何使用ConstraintLayout创建此布局

Java 如何使用ConstraintLayout创建此布局,java,android,android-constraintlayout,Java,Android,Android Constraintlayout,我对生成此屏幕xml有问题: RelativeLayout的最大高度必须为所有屏幕高度的50%。 若车身布局高度超过50%,则相对布局高度必须降低。我不明白如何将相对布局最大高度设置为50%。谢谢 屏幕的Xml代码: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/ap

我对生成此屏幕xml有问题:

RelativeLayout的最大高度必须为所有屏幕高度的50%。 若车身布局高度超过50%,则相对布局高度必须降低。我不明白如何将相对布局最大高度设置为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%,则相对框架高度必须降低,但事实并非如此:框架布局的内容将脱离屏幕

使用


使用


添加带有所需方向的
指南。然后,使用该线为每个视图添加约束

您可以使用此属性设置固定大小的
指南
-
app:layout\u constraintGuide\u begin
app:layout\u constraintGuide\u end

另外,如果您正在考虑使用
ConstraintLayout
,那么
match\u constraint(=0dp)
match\u parent
更好。在这种情况下,必须将约束与父约束一起设置

这是使用
指南
的完整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>

添加带有所需方向的
指南。然后,使用该线为每个视图添加约束

您可以使用此属性设置固定大小的
指南
-
app:layout\u constraintGuide\u begin
app:layout\u constraintGuide\u end

另外,如果您正在考虑使用
ConstraintLayout
,那么
match\u constraint(=0dp)
match\u parent
更好。在这种情况下,必须将约束与父约束一起设置

这是使用
指南
的完整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>

试试这段代码

activity_main.xml

MainActivity.java
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics dm=新的DisplayMetrics();
WindowManager wm=(WindowManager)getSystemService(窗口服务);
wm.getDefaultDisplay().getMetrics(dm);
int main height=dm.height像素;
Log.i(标签“mainHeight:+mainHeight”);
FrameLayout flLayout=findviewbyd(R.id.flu容器);
flLayout.setMinimumHeight(mainHeight/2);//试试这段代码

activity_main.xml

MainActivity.java
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics dm=新的DisplayMetrics();
WindowManager wm=(WindowManager)getSystemService(窗口服务);
wm.getDefaultDisplay().getMetrics(dm);
int main height=dm.height像素;
Log.i(标签“mainHeight:+mainHeight”);
FrameLayout flLayout=findviewbyd(R.id.flu容器);

flLayout.setMinimumHeight(mainHeight/2);//一些新属性在Constraint layout 1.1.0之后的版本中出现

要将布局高度设置为屏幕的50%,可以执行以下操作:

android:layout_height="0dp"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent=".5"
此处,0.5被视为屏幕高度的50%


请参阅此处

从Constraint layout 1.1.0开始,一些新属性就属于Constraint layout 1.1.0

要将布局高度设置为屏幕的50%,可以执行以下操作:

android:layout_height="0dp"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent=".5"
此处,0.5被视为屏幕高度的50%



请参阅此处

使用
应用程序:布局约束权重
您可以使用
应用程序:布局约束权重=“1”
约束布局中的两个布局。@JeelVankhede,但如果我的身体布局小于50%,徽标布局的高度将扩展好,然后使用@Atif提供的指导解决方案,它将起作用。
app:layout\u constraintheighth\u center=“50”
您可以使用
app:layout\u constraintVertical\u weight=“1”
约束布局中的两个布局。@JeelVankhede,但如果我的身体布局小于50%,徽标布局的高度将扩展好,然后使用@Atif提供的指导解决方案,它会工作的。我尝试了这个组件,但无法理解如何接受我的问题,因为指导原则就像填充一样工作(如果我理解的话)只需在xml中添加此准则,在设计中您将看到50%的布局分隔符只需将视图添加到centerMargini的底部。我尝试了此组件,但无法理解如何接受此解决我的问题,因为此准则的工作方式类似于填充(如果我理解的话)只需在xml中添加此准则,在设计中您将看到50%的布局分隔符只需将视图添加到CenterMargints的底部谢谢,我写了相同的代码,但如果我的framelayout高度超过50%,相对高度必须降低,但不是:framelayout的内容脱离屏幕:/you的意思是在
RelativeLayout
中,框架布局的高度可以灵活调整,对吗?是的,但前提是框架布局高度超过屏幕的一半高度,否则,框架布局和相对高度必须相同(50-50)然后,你不需要指导。只需将constraint
relativeLayout
的底部设置为
fl\u容器的顶部即可。并且你必须以编程方式将
fl\u容器的
minHeight
设置为设备屏幕的一半。谢谢你的回答,我写了相同的代码,但如果我的框架布局高度超过50%,relativeLayout就可以了您的高度必须降低,但不是:framelayout的内容脱离屏幕:/n您的意思是,
RelativeLayout
将随
framelayout
的高度而灵活,对吗?是的,但仅当framelayout高度超过屏幕高度的一半时,否则,framelayout和relative的高度必须相同(50-50)然后,您不需要指南。只需将constraint
relativeLayout
的底部设置为
fl\u conta的顶部即可
android:layout_height="0dp"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent=".5"