Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml Android约束布局-使视图占据屏幕的上半部分_Xml_Android Constraintlayout - Fatal编程技术网

Xml Android约束布局-使视图占据屏幕的上半部分

Xml Android约束布局-使视图占据屏幕的上半部分,xml,android-constraintlayout,Xml,Android Constraintlayout,我试图让textview占据屏幕的上半部分,以便熟悉新的约束布局系统 我尝试使用以下方法: <?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-au

我试图让
textview
占据屏幕的上半部分,以便熟悉新的约束布局系统

我尝试使用以下方法:

<?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"
    app:layout_constraintVertical_weight="2"
    tools:context=".MainActivity">

    <TextView
        android:text="Test"
        android:background="@color/colorPrimaryDark"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintLeft_toRightOf="parent"
        app:layout_constraintRight_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_weight="1" />

</android.support.constraint.ConstraintLayout>


我做错了什么?

如果使用Constraint Layout 1.1.0(撰写本文时最新的稳定版本)…最好的方法就是使用百分比

<?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"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/black"
        android:text="Test"
        android:textColor="@android:color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintHeight_percent="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

更新

  • 为什么要将layout\u width设置为0dp,并使用开始/结束而不是layout:width=“match\u parent”
  • 好问题。ConstraintLayout不支持
    match\u parent
    (一年多后,我认为这一决定还可以,因为它迫使用户在更正时问这个问题,但我认为这应该是一个警告,布局应该做到预期的效果-将每一面固定到父面,并在幕后使用0dp-)。在实践中,它并不总是这样做,这比使用0dp的技术正确性更令人困惑(在幕后,0dp对应于MATCH_约束,而不是MATCH_PARENT)。我建议您查看一下以了解它

    对于“因果我赶时间”读者,我使用了0dp,因为'match_parent_uu'对ConstraintLayout的作用与您认为的不同(它可能看起来不错,但不起作用)。0dp意味着匹配约束,您的大小最终将由它决定

    为什么选择开始/结束?因为如果目标是API 16+,则不应使用左/右;如果目标是API 16<,则应同时使用这两种语言,因为在从左到右的情况下,这很好,但如果不使用开始/结束,阿拉伯语、希伯来语和许多从右到左的语言将无法正确工作。(左是右,右是左!).在这一点上,请相信我,无论什么事情,都要始终使用开始/结束

  • 如何检查我使用的约束库的版本
  • 您的库是通过Gradle导入的,因此打开您的
    build.Gradle
    并找到
    dependencies
    部分。它将包含一行类似于以下内容:

    implementation“com.android.support.constraint:constraint布局:1.1.0”


    (例如)。毫无疑问。

    如果您使用Constraint Layout 1.1.0(在撰写本文时最新的稳定版本)…最好的方法就是使用百分比

    <?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"
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@android:color/black"
            android:text="Test"
            android:textColor="@android:color/white"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintHeight_percent="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </android.support.constraint.ConstraintLayout>
    
    
    

    更新

  • 为什么要将layout\u width设置为0dp,并使用开始/结束而不是layout:width=“match\u parent”
  • 好问题。ConstraintLayout不支持
    match\u parent
    (一年多后,我认为这一决定还可以,因为它迫使用户在更正时问这个问题,但我认为这应该是一个警告,布局应该做到预期的效果-将每一面固定到父面,并在幕后使用0dp-)。在实践中,它并不总是这样做,这比使用0dp的技术正确性更令人困惑(在幕后,0dp对应于MATCH_约束,而不是MATCH_PARENT)。我建议您查看一下以了解它

    对于“因果我赶时间”读者,我使用了0dp,因为'match_parent_uu'对ConstraintLayout的作用与您认为的不同(它可能看起来不错,但不起作用)。0dp意味着匹配约束,您的大小最终将由它决定

    为什么选择开始/结束?因为如果目标是API 16+,则不应使用左/右;如果目标是API 16<,则应同时使用这两种语言,因为在从左到右的情况下,这很好,但如果不使用开始/结束,阿拉伯语、希伯来语和许多从右到左的语言将无法正确工作。(左是右,右是左!).在这一点上,请相信我,无论什么事情,都要始终使用开始/结束

  • 如何检查我使用的约束库的版本
  • 您的库是通过Gradle导入的,因此打开您的
    build.Gradle
    并找到
    dependencies
    部分。它将包含一行类似于以下内容:

    implementation“com.android.support.constraint:constraint布局:1.1.0”


    (例如)。毫无疑问。

    您使用的是CL 1.1.0吗?您使用的是CL 1.1.0吗?出于好奇,您为什么将layout\u width设置为0dp,并使用constraintStart/constraintEnd而不是layout:width=“match\u parent”?此外,我如何检查我使用的约束库的版本?出于好奇,为什么您将layout_width设置为0dp,并使用constraintStart/constraintEnd而不是layout:width=“match_parent”?此外,我如何检查我使用的约束库的版本?