标签: android-constraintlayout

ConstraintLayout不尊重最大高度

我正在尝试使用ConstraintLayout创建布局组合.为了简化我的案例,我的布局应该有三个部分:

  1. 第一个布局(红色),应根据剩余空间增长并具有最大高度.
  2. 第二个布局(绿色),固定大小为150dp,应始终低于第一个布局.
  3. 第一个布局(粉红色),也有150dp的固定大小,应该与视图底部对齐.

我正在努力的部分是为第一个布局设置最大高度(红色).好像ConstraintLayout忽略了我的"最大高度语句":

app:layout_constraintHeight_max="300dp"
Run Code Online (Sandbox Code Playgroud)

这是我当前的结果(红色部分忽略了高度限制..):

在此输入图像描述

这是完整的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:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context="com.mixtiles.android.reviewOrder.ReviewOrderActivity"
tools:layout_editor_absoluteY="25dp">


<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white">
        <android.support.v7.widget.Toolbar
            android:id="@+id/review_order_toolbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
        </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

<FrameLayout
    android:id="@+id/red"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@color/red"
    app:layout_constraintBottom_toTopOf="@+id/green"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_max="300dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/appBarLayout"
    app:layout_constraintVertical_chainStyle="spread_inside">

</FrameLayout>


<FrameLayout
    android:id="@+id/green"
    android:layout_width="0dp"
    android:layout_height="150dp"
    android:background="@color/greenish"
    app:layout_constraintBottom_toTopOf="@+id/pink"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/red">

</FrameLayout>

<FrameLayout
    android:id="@+id/pink"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:background="@color/pink"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/green">

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

android android-constraintlayout constraint-layout-chains

16
推荐指数
2
解决办法
7442
查看次数

如何垂直对齐TextView中的文本?

我正在努力实现这个结果:

预期结果

我现在所拥有的是ConstraintLayout包括三个Views:TextView,ImageView,ImageView

设计要求:

  1. 文字居中TextView;
  2. TextView 固定大小为80dp
  3. 文字大小应为AutoSizeable;
  4. ImageViews应根据文本位置水平改变位置TextView.请参见案例#1 /#2的图像以供参考.

是否可以用XML实现第4项?那么以编程方式呢?

[附加信息]

自动调整大小无法正常工作wrap_content,请参阅自动调整文档:

注意:如果在XML文件中设置自动调整大小,则不建议对TextViewlayout_widthlayout_height属性使用值"wrap_content" .它可能会产生意外的结果.

android textview autoresize imageview android-constraintlayout

16
推荐指数
1
解决办法
386
查看次数

Android Studio 2.2预览1 EditText错误

我正在尝试使用Android Studio 2.2预览1,但如果我创建一个新项目并添加一个简单的EditText并尝试运行,我收到以下错误.

05-20 15:53:00.748 11798-11798/br.com.tutorialandroid.sqlite E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.com.tutorialandroid.sqlite, PID: 11798
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.tutorialandroid.sqlite/br.com.tutorialandroid.sqlite.MainActivity}: android.view.InflateException: Binary XML file line #14: Error inflating class EditText
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class EditText
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:933)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:933)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at …
Run Code Online (Sandbox Code Playgroud)

android android-studio android-constraintlayout

15
推荐指数
1
解决办法
4108
查看次数

窗口软输入模式ConstraintLayout

之前软输入模式没有问题,但是在包含之后ConstraintLayout,当键盘出现时,片段的内容不会向上移动.

表现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ru.pinspb.pinsupport">

    <uses-feature
        android:name="android.software.leanback"
        android:required="false" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="ru.pinspb.pinsupport.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="ru.pinspb.pinsupport.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />

    <application
        android:name=".PinApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme.NoActionBar">
        <activity
            android:name=".auth.ui.HomeActivity"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".front.ui.FrontActivity"
            android:launchMode="singleTop" />
        <activity
            android:name=".chats.ui.InitChatActivity"
            android:launchMode="singleTop"
            android:windowSoftInputMode="stateHidden" />
    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

分段

public …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-constraintlayout

15
推荐指数
3
解决办法
1万
查看次数

删除水平进度条中的填充

在我们的应用程序中,我们需要一个不确定的进度条,如下所示:

进度条应该如何

我们可以通过在ProgressBar上设置负边距来实现这一点,如下所示:

<ProgressBar
    android:id="@+id/progressbar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:indeterminate="true"
    android:marginTop="-7dp"
    android:visibility="@{loading ? View.VISIBLE : View.GONE}" />
Run Code Online (Sandbox Code Playgroud)

但是因为ConstraintLayout不支持负边距,所以它看起来像这样:

带填充的进度条

好吧,负利润是一个黑客.让我们换一个不同的黑客,好吗?让我们来介绍我们的自定义视图CustomProgressBar,它扩展ProgressBar并覆盖其onDraw方法,如下所示:

@Override
protected void onDraw(Canvas canvas) {
    int marginTop = dpToPx(7);
    canvas.translate(0, -marginTop);
    super.onDraw(canvas);
}
Run Code Online (Sandbox Code Playgroud)

但所有这些都闻起来像坏代码.必须有一个更好的解决方案!你会推荐什么?

java layout android android-studio android-constraintlayout

15
推荐指数
2
解决办法
5537
查看次数

为什么Android Studio正在删除和/或更改layout_margin*约束?

我正在使用android.support.constraint.ConstraintLayout布局简单卡片的内容视图.

我每次都会在以下情况下随机更改XML:

  • 我切换到Android Studio的布局编辑器Design选项卡
  • 或者当我Preview打开一个窗格时(在这种情况下,在我的更改期间更改会发生更改).

随机变化包括:

  • 删除我手动定义的布局边距(例如layout_marginEnd)
  • 更改我手动输入的边距值的值
  • 添加tools:layout_editor_absoluteX

问题:

  • 其他人都有这个Android Studio的功能吗?
  • 如何关闭这个"功能",所以我的布局不会被破坏?
  • 在哪里向Google或JetBrains报告此错误?

请看一下这个文件diff,它是在切换到Design选项卡时自动生成的: 在布局XML文件中自动更改diff

PS.:我的Android Studio版本是2.2.3; 我在macOS Sierra上运行它.

更新#1

正如所建议的,在制作之后layout_height="match_parent",我不经常修改,但是当我在Layout Design'er中编辑布局时,我的XML仍然搞砸了: 自动更改#2 这次我想layout_marginTop通过设计UI 进行设置(所选行是我想要添加的),但我还得到了其他不需要的更改:

  • layout_marginStart 删除;
  • app:layout_constraintBottom_toBottomOf引用的ID有一个前缀"+"符号,这意味着,这些ID是新声明的,所以当我使用Goto declaration功能时,我将不得不从列表中选择它 - 而不是所需的行为.
  • tools:layout_editor_absoluteX得到补充.我可以忽略它,但仍然 - 不是我想要发生的事情.

android android-studio android-constraintlayout android-layout-editor

15
推荐指数
1
解决办法
2877
查看次数

marginTop不适用于ConstraintLayout和wrap_content

在我的Fragment中我有一个ConstraintLayout,layout_height="wrap_content"我希望在视图底部的两个底部之间有一个边距.

当我将这个边距添加layout_marginBottom到上方按钮(button_welcome_signup)时,似乎工作正常.但是,如果我尝试将其添加到底部按钮(button_welcome_signin),因为layout_marginTop它不起作用.

有谁知道这里的问题/如果我做错了什么?

(请注意,我使用wrap_content是有原因的,而且我非常想在底部按钮上使用边距,因此我可以将其添加到其样式中,以便在我的项目中实现更好的UI一致性).

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:MyAppApp="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:background="@color/white"
    android:minHeight="@dimen/min_height_welcome_frame"
    android:padding="@dimen/margin_all_frame_inner">

    <ImageView
        android:id="@+id/imageview_welcome_logo"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@drawable/logo_header"
        MyAppApp:layout_constraintTop_toTopOf="parent"
        MyAppApp:layout_constraintLeft_toLeftOf="parent"
        MyAppApp:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:id="@+id/textiew_welcome_title"
        style="@style/MyAppTextViewTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margin_all_component_l"
        android:text="@string/welcome_title"
        MyAppApp:layout_constraintTop_toBottomOf="@id/imageview_welcome_logo" />

    <TextView
        android:id="@+id/textview_welcome_text"
        style="@style/MyAppTextViewText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/welcome_message"
        MyAppApp:layout_constraintTop_toBottomOf="@id/textiew_welcome_title" />

    <Button
        android:id="@+id/button_welcome_signin"
        style="@style/MyAppSubButton"
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:layout_marginTop="@dimen/margin_all_component_s" 
        android:text="@string/welcome_sign_in"
        MyAppApp:layout_constraintBottom_toBottomOf="parent" />

    <Button
        android:id="@+id/button_welcome_signup"
        style="@style/MyAppButton"
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:layout_marginTop="@dimen/margin_all_component_l"
        android:text="@string/welcome_sign_up"
        MyAppApp:layout_constraintBottom_toTopOf="@id/button_welcome_signin"
        MyAppApp:layout_constraintTop_toBottomOf="@id/textview_welcome_text"
        MyAppApp:layout_constraintVertical_bias="1" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

styles.xml:

<style name="MyAppButton" parent="Widget.AppCompat.Button">
    <item …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-xml android-constraintlayout android-wrap-content

15
推荐指数
1
解决办法
1万
查看次数

ConstraintLayout onMeasure非常慢

所以我尝试重构我的一些回收的ViewHoldersConstraintLayouts.我做完之后,在看到之后感到震惊.对单个视图进行充气所需的时间比平常多20倍LinearLayout.它实际上在执行时会跳过这么多帧.

编辑:约束布局的版本不相关.试过不同的组合有几乎相同的结果.

任何人都能解释为什么会这样吗?也许它不是为这种"沉重"的观点而设计的?

以下是XML用于以下内容的根ViewHolder:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:elevation="@dimen/param_2"
    android:orientation="vertical"
    android:stateListAnimator="@animator/material_selector">

    <LinearLayout
        android:id="@+id/order_view_tabs_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/param_2"
        android:padding="@dimen/param_4"
        android:background="@color/white"
        android:divider="@drawable/empty_horizontal_divider"
        android:elevation="@dimen/param_2"
        android:orientation="horizontal"
        android:showDividers="middle"
        android:visibility="gone"/>

    <include layout="@layout/order_list_item_constraint"/>

    </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这是 order_list_item_constraint.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="wrap_content"
    android:padding="@dimen/param_4"
    android:clipToPadding="false">

    <TextView
        android:id="@+id/delivery_status"
        style="@style/DefaultText.Normal"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginEnd="4dp"
        android:padding="4dp"
        android:background="@color/white"
        android:elevation="2dp"
        android:gravity="center_vertical"
        android:text="@string/main_swipe_list_item_info_title_delivered_time"
        app:layout_constraintEnd_toStartOf="@id/mid_guideline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteY="4dp"/>

    <TextView
        android:id="@+id/order_list_item_order_title"
        style="@style/FullListItemInfoText"
        android:layout_width="0dp"
        android:layout_marginTop="4dp"
        android:text="@string/main_swipe_list_item_info_title_order"
        android:textColor="@color/red_900"
        app:layout_constraintEnd_toEndOf="@id/mid_guideline"
        app:layout_constraintStart_toStartOf="@id/start_guideline"
        app:layout_constraintTop_toBottomOf="@+id/delivery_status"/>

    <TextView
        android:id="@+id/order_list_item_order_id"
        style="@style/FullListItemInfoDetailsText"
        android:layout_width="0dp"
        app:layout_constraintEnd_toEndOf="@id/mid_guideline"
        app:layout_constraintStart_toStartOf="@+id/start_guideline"
        app:layout_constraintTop_toBottomOf="@id/order_list_item_order_title"
        /> …
Run Code Online (Sandbox Code Playgroud)

xml android android-layout android-measure android-constraintlayout

15
推荐指数
1
解决办法
4380
查看次数

View.GONE不适用于"Constraint.Group"特定的子项

我正在尝试"Constraint.Group"并且我有孩子的观点:A,B,C.

在代码中,"Constraint.Group".visibility = View.Gone确实有效,但如果我选择这样做A.visibility = View.Gone不会对子视图产生影响.这是正常的行为吗?

android android-constraintlayout

15
推荐指数
2
解决办法
2167
查看次数

在ConstraintLayout中与障碍物链接

我想使用没有嵌套的ConstraintLayout实现下面的布局。

在此处输入图片说明

布局需要一个障碍,因为不能保证哪个textview会更高。这也需要一条链,因为一切都需要居中。问题是我找不到使壁垒与壁垒一起工作的方法。

到目前为止,这是我可以正确布局但不会居中的内容:

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/topLeftText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        tools:text="Test test test test test test test test test test"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/topRightText"/>

    <TextView
        android:id="@+id/topRightText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp"
        tools:text="Test test test test test test test test test test test test test test test test test test test test"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@+id/topLeftText"
        app:layout_constraintEnd_toEndOf="parent"/>

    <android.support.constraint.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:constraint_referenced_ids="topLeftText,topRightText"
        app:barrierDirection="bottom"/>

    <TextView
        android:id="@+id/bottomText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        tools:text="Test test test test test test test test test test …
Run Code Online (Sandbox Code Playgroud)

android android-constraintlayout

15
推荐指数
1
解决办法
272
查看次数