标签: android-constraintlayout

如何在PercentRelativeLayout中使用layout_aspectRatio?

我尝试在视图上实现16:9的宽高比PercentRelativeLayout.

所以我把这行放在build.gradle文件中: compile 'com.android.support:design:23.0.1'

我用这个布局:

<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="match_parent"
        app:layout_aspectRatio="178%"
        android:scaleType="centerCrop"/>

</android.support.percent.PercentRelativeLayout>
Run Code Online (Sandbox Code Playgroud)

问题:

  • Android Studio警告我:'layout_height' should be defined为了ImageView.
  • 当我运行我的项目时,我得到了:Error:(15) No resource identifier found for attribute 'layout_aspectRatio'.

那有什么不对?

android android-layout android-constraintlayout android-percentrelativelayout

13
推荐指数
3
解决办法
8653
查看次数

android studio SDK工具中"ConstraintLayout for android"和"Solver for ConstraintLayout"之间的区别

在用于ConstraintLayout.One的android studio SDK工具中有两个选项ConstraintLayout for android,另一个是Solver for ConstraintLayout.

这是截图: 在此输入图像描述

这两个选项有什么区别?

android android-sdk-tools android-constraintlayout

13
推荐指数
1
解决办法
2194
查看次数

android ConstraintLayout不允许负边距

我相对于布局指南放置了很多项目,并希望几乎相对于此布局指南定位一个新项目.

我尝试使用负布局边距但没有成功.

android android-constraintlayout

13
推荐指数
3
解决办法
4928
查看次数

以编程方式更改ConstraintLayout子项的边距和大小

我正在编写一个自定义视图,以便能够使用自定义XML属性根据这些属性设置视图的边距和大小,一切正常,直到我到达ConstraintLayout的子项获得其边距和大小由自定义值确定的部分.边距没有任何区别,视图保留在其父ConstraintLayout的左上角.可能是什么问题呢?(图像应距离屏幕边界500PXs)

if (hasCustomWidth || hasCustomHeight || hasCustomTopMargin || hasCustomRightMargin || hasCustomBottomMargin || hasCustomLeftMargin) {
    if(((ViewGroup)getParent()) instanceof  LinearLayout) {
        LinearLayout.LayoutParams newLayoutParams = new LinearLayout.LayoutParams((int) Math.round(customWidth), (int) Math.round(customHeight));
        ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) newLayoutParams;
        marginLayoutParams.setMargins((int) Math.round(customLeftMargin), (int) Math.round(customTopMargin), (int) Math.round(customRightMargin), (int) Math.round(customBottomMargin));
        setLayoutParams(newLayoutParams);
    } else if(((ViewGroup)getParent()) instanceof ConstraintLayout) {
        ConstraintLayout parentConstraintLayout = (ConstraintLayout)CustomAppCompatImageView.this.getParent();

        ConstraintLayout.LayoutParams newLayoutParams = new ConstraintLayout.LayoutParams((int) Math.round(customWidth), (int) Math.round(customHeight));

        ConstraintSet constraintSet = new ConstraintSet();

        constraintSet.clone(parentConstraintLayout);

        constraintSet.connect(CustomAppCompatImageView.this.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 500);
        constraintSet.setMargin(CustomAppCompatImageView.this.getId(), ConstraintSet.LEFT, 500);

        setLayoutParams(newLayoutParams);
        constraintSet.applyTo(parentConstraintLayout);

        parentConstraintLayout.invalidate();
    } else {
        throw new …
Run Code Online (Sandbox Code Playgroud)

android android-custom-attributes android-constraintlayout

13
推荐指数
1
解决办法
4298
查看次数

如何将AdView放在ConstraintLayout的底部和下方?

我正在尝试从RelativeLayout迁移到ConstraintLayout,但我在将AdMob AdView添加到底部以及其上方的其余内容时遇到问题.例如,使用RelativeLayout就是这么简单.您只需要放置android:layout_alignParentBottom="true"AdView和android:layout_above="@+id/adView"包含内容的视图.

我试图了解如何使用ConstraintLayout而不是RelativeLayout将此示例代码和这两行代码迁移到等效代码.

拜托,有人可以帮我解决这个问题吗?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="@android:color/transparent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/adView"/>

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="5dp"
        ads:adSize="BANNER"
        ads:adUnitId="@string/ad_id_banner">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

app:layout_constraintBottom_toTopOf="@+id/adView"在adView上面和adView app:layout_constraintBottom_toBottomOf="parent"上测试了我想要的东西,但它不起作用,因为这些东西不在它后面的adView之上.此外,adView不会居中.这非常令人沮丧.

android android-layout android-relativelayout android-constraintlayout

13
推荐指数
1
解决办法
4836
查看次数

在Constraint布局中编辑文本滚动条问题

我只是在编辑文本的高度达到布局高度的末尾时,我试图在编辑文本中显示滚动条.如果我输入/添加空格,但是当我的文本到达编辑文本宽度的末尾时,它会导致问题.一旦我的文本到达我的编辑文本宽度的末尾,它就开始向我显示滚动条,而当我到达屏幕的(高度)结束时它应该显示.

请告知我如何解决此问题.请查看附图和代码以供参考.在此输入图像描述

<?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">

    <EditText
        android:id="@+id/editFeedback"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:bufferType="spannable"
        android:gravity="top"
        android:hint="Some text"
        android:inputType="textCapSentences|textMultiLine"
        android:minHeight="120dp"
        android:overScrollMode="ifContentScrolls"
        android:scrollbarStyle="insideInset"
        android:scrollbars="vertical"
        android:selectAllOnFocus="true"
        android:textIsSelectable="true"
        android:textSize="16sp"
        app:layout_constraintBottom_toTopOf="@+id/buttonSend"
        app:layout_constraintEnd_toEndOf="@id/guidelineEnd"
        app:layout_constraintHeight_default="wrap"
        app:layout_constraintStart_toStartOf="@id/guidelineStart"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintVertical_chainStyle="packed"
        tools:text="" />

    <Button
        android:id="@+id/buttonSend"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:text="Send"
        android:textAllCaps="false"
        app:layout_constraintBottom_toBottomOf="@+id/barrierEnd"
        app:layout_constraintEnd_toEndOf="@id/guidelineEnd"
        app:layout_constraintTop_toBottomOf="@+id/editFeedback" />

    <TextView
        android:id="@+id/Footer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:layout_marginBottom="100dp"
        android:text="This is a footer"
        android:textSize="15sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <android.support.constraint.Guideline
        android:id="@+id/guidelineStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="8dp" />

    <android.support.constraint.Guideline
        android:id="@+id/guidelineEnd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_end="8dp" />

    <android.support.constraint.Barrier
        android:id="@+id/barrierEnd"
        android:layout_width="wrap_content" …
Run Code Online (Sandbox Code Playgroud)

android android-edittext android-constraintlayout

13
推荐指数
1
解决办法
600
查看次数

IllegalStateException:函数 = &lt;匿名&gt;,计数 = 3,索引 = 3

直到昨天一切都很好。今天当我打开系统时,我突然收到错误:

java.lang.IllegalStateException: function = <anonymous>, count = 3, index = 3
Run Code Online (Sandbox Code Playgroud)

与“内部编译器错误”一起运行我的项目。

我尝试更新我的 gradle 版本和 gradle 插件版本,以及我使用过的依赖项,但是没有用。

我该如何解决这个问题?

这些是我使用过的依赖项:

    def lifecycle_version = "2.3.1"

    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation "androidx.drawerlayout:drawerlayout:1.1.1"
    implementation "androidx.navigation:navigation-fragment-ktx:2.3.5"
    implementation "androidx.navigation:navigation-ui-ktx:2.3.5"
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'com.shawnlin:number-picker:2.4.11'
    implementation 'com.karumi:dexter:6.2.1'
    implementation 'com.github.IslamKhSh:CardSlider:1.0.1'
    implementation 'com.intuit.sdp:sdp-android:1.0.6'
    implementation 'com.intuit.ssp:ssp-android:1.0.6'
    implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
    implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
    implementation 'com.google.android.gms:play-services-auth:19.0.0'
    implementation 'com.google.android.gms:play-services-auth-api-phone:17.5.0'
    implementation 'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:3.0.0-RC2'
    // Lottie dependency
    implementation "com.airbnb.android:lottie:3.7.0"

    //Autostart settings open
    implementation 'com.thelittlefireman:AppKillerManager:2.1.1'

    // Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.3.0' …
Run Code Online (Sandbox Code Playgroud)

android compiler-errors kotlin android-constraintlayout android-jetpack-compose

13
推荐指数
1
解决办法
1240
查看次数

ConstraintLayout View.GONE用法

我最近开始使用ConstraintLayout.正如我发现的那样,大多数功能都非常简单,并且在文档中对样本,文本和视频教程等进行了详细解释.

我想到的是如何尽可能优雅地解决这个"谜题"?

看起来很好的蓝图 看起来很糟糕的蓝图

如您所见,在布局的右侧部分,我有多个左对齐的视图.在最后一行中,有3个视图水平对齐(它们也在彼此之间对齐).问题是:如果我将第一个View的可见性从该行设置为GONE,则另外两个(在同一行中)按预期向左移动,但下面的那个(垂直对齐中的最后一行)超过该行(因为它constraintTop属性设置为View设置为GONE的底部.

我能想到的唯一解决方案是使用ViewGroup对这3个视图进行分组,并将约束添加到最后一行View.

我错过了ConstraintLayout上的一些属性,这对我的情况有帮助吗?如果在GONE View中设置其中一个,可能会出现某种后退(或多个)约束?

对不起,如果我的解释看起来很深奥,也许图片会帮助你更多:)

LE: 增加了布局:https://gist.github.com/DooruAdryan/7e7920a783f07b865489b1af0d933570

android android-constraintlayout

12
推荐指数
1
解决办法
8304
查看次数

ConstraintLayout中的RecyclerView与其他元素重叠

我正在使用以下组件进行简单的列表活动:EditText,RecyclerView,ProgressBar和Textview.一切都运行得很好,除了与EditText重叠并且"超出"屏幕底部的RecyclerView,所以最后一项被部分剪掉(参见截图).我做错了什么?

主要活动:

<?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"
    tools:context="jacopo.com.flickrgallery.GalleryActivity">

    <EditText
        android:id="@+id/search_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:hint="search by tag..."
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ProgressBar
        android:id="@+id/gallery_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search_text" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/gallery_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search_text" />

    <TextView
        android:id="@+id/gallery_error"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search_text"
        tools:text="error" />

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

recyclerview重叠editext recyclerView走出屏幕

android android-layout android-recyclerview android-constraintlayout

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

androidx.constraintlayout.widget.constraintlayout没有可拖动的链

我在布局中使用的是androidx.constraintlayout.widget.ConstraintLayout,它没有显示链,也无法拖动任何小部件。我只是输入要使用的约束。

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="112dp"/>
Run Code Online (Sandbox Code Playgroud)

这是androidx约束布局的示例屏幕截图: androidx.constraintlayout.widget.ConstraintLayout

但是当我尝试切换回android.support.constraint.ConstraintLayout时,我可以看到它

<android.support.constraint.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="112dp"/>
Run Code Online (Sandbox Code Playgroud)

这是支持constraintLayout的示例屏幕截图: android.support.constraint.ConstraintLayout

这是错误还是什么?

编辑:

我当前的解决方案是从:

implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
Run Code Online (Sandbox Code Playgroud)

implementation 'com.android.support.constraint:constraint-layout:1.1.2'
Run Code Online (Sandbox Code Playgroud)

但这不能解决我的问题,因为我不想使用支持。

android android-constraintlayout constraint-layout-chains androidx

12
推荐指数
2
解决办法
2万
查看次数