标签: android-constraintlayout

NestedScrollView 未在 AlertDialog 内扩展高度

我当前正在警报对话框内使用滚动视图,并且需要滚动视图的高度增加,直到对话框达到其最大默认高度。对我来说解释起来有点困难,所以我附上了一个插图来帮助。希望如此。

在此输入图像描述

我遇到的问题是滚动视图的高度不会增长,即使我删除它app:layout_constraintBottom_toTopOf="@id/linearLayout4",它也会增长,但底部部分将被按钮布局部分隐藏。

这是我当前的代码:

<androidx.constraintlayout.widget.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/filter_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/dialog_box"
android:minHeight="300dp"
android:elevation="12dp">

<TextView
    android:id="@+id/filter_header"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text="@string/filter"
    style="@style/element_header"
    android:textColor="@color/textColorDark"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/filter_reset_btn"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:background="?attr/selectableItemBackgroundBorderless"
    android:text="@string/reset"
    style="@style/text_info_nBold"
    android:textSize="14sp"
    android:textColor="@color/textColorDark"
    app:layout_constraintBottom_toBottomOf="@+id/filter_header"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="@+id/filter_header" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintBottom_toTopOf="@id/linearLayout4"
    app:layout_constraintTop_toBottomOf="@id/filter_header"
    app:layout_constraintVertical_bias="0.0">

    <androidx.core.widget.NestedScrollView
        android:id="@+id/filter_scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:layout_marginTop="16dp"
        android:scrollbarFadeDuration="1000">

        <!-- VIEWS INSIDE HERE -->

    </androidx.core.widget.NestedScrollView>

</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/dialog_bottombar_layout"
    android:orientation="horizontal"
    android:padding="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <Button
        android:id="@+id/dialog_secondary_button"
        style="@style/low_emphasis_btn"
        android:layout_width="0dp"
        android:layout_marginEnd="8dp"
        android:layout_weight="1"
        android:backgroundTint="@color/textColorDark"
        android:text="@string/dialog_cancel"
        android:textColor="@color/textColorDark" …
Run Code Online (Sandbox Code Playgroud)

xml android scrollview android-studio android-constraintlayout

2
推荐指数
1
解决办法
2100
查看次数

如何在父级为滚动视图的约束布局中将元素逐个放置在另一个元素下面

我所拥有的:

我有 4 个小部件,放置在约束布局内,其父级是滚动视图


我正在尝试做的事情:

我正在尝试将小部件一个一个地显示在另一个下面


怎么了:

小部件一个一个地放置在另一个之上


我的布局

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fillViewport="true">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <EditText
            android:id="@+id/edtNameId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/str_name"
            android:inputType="text"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

        <EditText
            android:id="@+id/edtPwdId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/str_password"
            app:layout_constraintBottom_toBottomOf="@+id/edtNameId"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

        <TextView
            android:id="@+id/txtChangePwdId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/str_change_password"
            app:layout_constraintBottom_toBottomOf="@+id/edtPwdId"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

        <Button
            android:id="@+id/btnSubmitId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/str_submit"
            app:layout_constraintBottom_toBottomOf="@+id/txtChangePwdId"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>


    </androidx.constraintlayout.widget.ConstraintLayout>

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

输出:

在此输入图像描述

android android-constraintlayout

2
推荐指数
1
解决办法
2158
查看次数

Android 约束布局屏障和消失的边距

我有一个约束布局,如下所示:

A-B
--------- (barrier that refer to A and B bottom)
C (constraint_top_bottomOf barrier)
Run Code Online (Sandbox Code Playgroud)

这是代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".TypographyActivity"
    android:background="?attr/lightPrimary">

    <TextView
        android:id="@+id/textviewA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/textviewB"
        app:layout_constraintTop_toTopOf="parent"
        android:text="Text A" />

    <TextView
        android:id="@+id/textviewB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        app:layout_constraintStart_toEndOf="@id/textviewA"
        app:layout_constraintTop_toTopOf="parent"
        android:text="Text B" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="textviewA, textviewB" />

    <TextView
        android:id="@+id/textviewC"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        app:layout_goneMarginTop="30dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/barrier"
        android:text="Text C" />

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

即使 A 和 B 可见,textviewC 中的 goneMarginTop 也始终会被触发。

当 A 和 B 消失时,有没有办法将 …

android margin barrier android-constraintlayout

2
推荐指数
1
解决办法
5864
查看次数

0dp layout_width 无法按 recyclerview 项中的约束布局子项的预期工作

缩小工作布局代码,

<?xml version="1.0" encoding="utf-8"?>
<layout>
    <data>
    </data>

    <androidx.constraintlayout.widget.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/recyclerview_item_layout_root_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/recyclerview_item_layout_textview_fact"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)

上面的代码按预期呈现回收器视图。
但根据文档

您不能对 ConstraintLayout 中的任何视图使用 match_parent。而是使用“匹配约束”(0dp)。

所以,我替换了这一行,

android:layout_width="match_parent"
Run Code Online (Sandbox Code Playgroud)

有了这个

android:layout_width="0dp"
Run Code Online (Sandbox Code Playgroud)

但是,它不起作用。是recyclerview不可见的。

完整的存储库是开源的, https://github.com/Abhimanyu14/cat-fact

Recyclerview 适配器代码(如果需要),

class HomeFragmentRecyclerViewAdapter :
    PagingDataAdapter<CatFact, HomeFragmentRecyclerViewAdapter.MainActivityRecyclerViewHolder>(
        CatFactDiffCallback
    ) {
    
    class MainActivityRecyclerViewHolder(private var binding: RecyclerviewItemLayoutBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(catFact: CatFact?) {
            catFact?.let {
                binding.recyclerviewItemLayoutTextviewFact.text = String.format(
                    binding.root.context.resources.getString(R.string.recyclerview_item_layout_fact),
                    catFact.id,
                    catFact.fact
                )
            }
        }
    }
    
    object CatFactDiffCallback : DiffUtil.ItemCallback<CatFact>() { …
Run Code Online (Sandbox Code Playgroud)

android android-recyclerview android-constraintlayout

2
推荐指数
1
解决办法
3166
查看次数

ConstraintLayout 中带有drawableEnd 的 TextView 换行问题

我有一个ConstraintLayout包含 aTextView在文本末尾有一个小图像的文件。我遇到的这个问题是长文本没有换行。如果我修复了约束,TextView以便长文本将图像末尾包裹到屏幕的末尾:

截屏:

在此输入图像描述

如果我添加app:layout_constraintEnd_toEndOf="parent"TextView长文本换行但最后的图像不换行:

在此输入图像描述

代码:

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout
    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"
    android:padding="10dp"
    android:layout_marginBottom="20dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/subscription_row_item_thumb"
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:scaleType="centerInside"
        android:background="@drawable/all_circle_white_bg"
        android:padding="1dp"
        android:foreground="?android:attr/selectableItemBackground"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <ImageView
        android:id="@+id/subscription_row_item_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:foreground="?android:attr/selectableItemBackground"
        android:layout_marginTop="15dp"
        app:layout_constraintTop_toBottomOf="@id/subscription_row_item_thumb"
        app:layout_constraintStart_toStartOf="@id/subscription_row_item_thumb"
        app:layout_constraintEnd_toEndOf="@id/subscription_row_item_thumb"/>

     <TextView
        android:id="@+id/subscription_row_item_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:breakStrategy="simple"
        android:layout_marginStart="15dp"
        android:textSize="16sp"
        app:layout_constrainedWidth="true"
        app:drawableEndCompat="@drawable/ic_action_open_episode_list"
        android:drawablePadding="10dp"
        app:layout_constraintTop_toTopOf="@id/subscription_row_item_thumb"
        app:layout_constraintBottom_toBottomOf="@id/subscription_row_item_thumb"
        app:layout_constraintStart_toEndOf="@id/subscription_row_item_thumb"
        app:layout_constraintEnd_toEndOf="parent"/>

    <TextView
        android:id="@+id/subscription_row_item_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:breakStrategy="simple"
        android:layout_marginTop="5dp"
        android:textSize="14sp"
        android:textColor="#A1A0A0"
        app:layout_constraintTop_toBottomOf="@id/subscription_row_item_title"
        app:layout_constraintStart_toStartOf="@id/subscription_row_item_title"/>

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

android textview android-layout android-constraintlayout

2
推荐指数
1
解决办法
977
查看次数

Jetpack Compose:约束布局垂直链约束问题

我正在尝试使用 Jetpack Compose ConstraintLayout 构建类似于以下内容的内容。

在此输入图像描述

我想了解如何创建相对于图像的垂直链。当我尝试使用该createVerticalChain()函数时,它会覆盖我给定的约束并将其链接到父级。

这是我当前的代码,如果有帮助的话

@Composable
fun GreetingWithConstraintLayout(name: String) {
    ConstraintLayout(modifier = Modifier.fillMaxSize()) {
        val(ivIcon, tvHello, tvBye) = createRefs()
        createVerticalChain(tvBye, tvHello, chainStyle = ChainStyle.Packed)
        Image(
            painter = painterResource(id = R.drawable.ic_launcher_background),
            contentDescription = null,
            modifier = Modifier
                .size(80.dp)
                .clip(CircleShape)
                .border(1.5.dp, MaterialTheme.colors.primary, CircleShape)
                .constrainAs(ivIcon) {
                    start.linkTo(parent.start)
                    top.linkTo(parent.top)
                }
        )
        Text(
            text = "Hello $name!",
            modifier = Modifier.constrainAs(tvHello) {
                start.linkTo(ivIcon.end, 8.dp)
                top.linkTo(ivIcon.top)
                bottom.linkTo(tvBye.top)
            }
        )
        Text(
            text = "Bye $name!",
            modifier = Modifier.constrainAs(tvBye) {
                start.linkTo(tvHello.start)
                top.linkTo(tvHello.bottom)
                bottom.linkTo(ivIcon.bottom)
            }
        ) …
Run Code Online (Sandbox Code Playgroud)

android android-constraintlayout android-jetpack android-jetpack-compose

2
推荐指数
1
解决办法
3403
查看次数

文本超出屏幕android约束布局

我有一个listView,该项包含两个组件imageView和textView,我试图在textView中设置文本背景.

所以我想如果我能够将Textview宽度设置为wrap_content那么可以使用android:background标签.

我所做的是将textview宽度和高度设置为wrap_content,它只有在我有一个短文本时才有效但如果我有一个很长的文本,那么一些文本将不在屏幕之外

<?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"
app:layout_constraintWidth_default="wrap"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteY="25dp"
tools:layout_editor_absoluteX="0dp">

<ImageView
    android:id="@+id/imageViewChat"
    android:layout_width="50dp"
    android:layout_height="50dp"
    app:srcCompat="@drawable/googleg_standard_color_18"
    android:layout_marginStart="16dp"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="26dp"
    android:layout_marginLeft="16dp"
    app:layout_constraintLeft_toLeftOf="parent" />

<TextView
    android:id="@+id/textViewChat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"

    android:layout_marginRight="16dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="26dp"
    android:background="@drawable/rounded_corner"

    android:fontFamily="serif"
    android:text="ssssss ssssss ssssss ssssss ssssss ssssss  ssssss 
    ssssss  ssssss ssssss "
    android:textAlignment="textStart"
    android:textColor="@color/bb_darkBackgroundColor"
    android:textSize="15sp"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toRightOf="@+id/imageViewChat"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />



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

android android-constraintlayout

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

如何在布局中使用CSS“负边距”的相同技术?

我想创建您在图片中看到的这种布局,并且我认为CSS的负边距没有结果。请帮我实现它。 在此处输入图片说明

<?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=".HomeActivity">
    <TextView
    android:id="@+id/txt_home_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/box"
    android:elegantTextHeight="true"
    android:padding="30dp"
    android:text="Welcome!"
    android:textColor="#ffffff"
    android:textSize="28sp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp">
    <ImageView
        android:id="@+id/home_logo"
        android:layout_width="90dp"
        android:layout_height="80dp"
        android:scaleType="fitCenter"
        android:src="@drawable/mylogo" />
</FrameLayout>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

这是盒子:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#2d3669"></solid>
            <corners android:radius="50dp"></corners>
            <gradient android:angle="90"
                android:startColor="#392800"
                android:endColor="#faae03"/>
        </shape>
    </item>
</selector>
Run Code Online (Sandbox Code Playgroud)

我知道ConstraintLayout如何使物品彼此靠近,但是我不知道如何将原木放到木头上,TextView使一半留在外面!我在Google上找不到导致此类示例的关键字。

layout android android-layout android-constraintlayout

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

ConstraintLayout-wrap_content + toRightOf布局问题

我正在使用约束布局,并且尝试使textview在准则的右侧,并且还包装其宽度。但是由于"app:layout_constraintLeft_toRightOf="@id/guideline1"某种原因,textview没有使用约束

在此处输入图片说明

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout android:id="@+id/chat_model"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="15dp"
    android:layout_marginTop="15dp"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.constraint.Guideline
        android:id="@+id/guideline1"
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent=".25" />

    <TextView
        android:id="@+id/texview_chatmodel_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bubble_user"
        android:padding="8dp"
        android:text="Username: aaaaaaaa aaaaa aaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        android:textColor="@color/colorBlack"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@id/guideline1"
        android:gravity="right"/>

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

当我将textview的宽度设置为0时,它起作用

android:layout_width="0dp"
Run Code Online (Sandbox Code Playgroud)

但是,这会导致短消息一直延伸到指南的所有地方,从而形成空白。

在此处输入图片说明

如何获得该消息,以便使消息布局位于准则的右侧,并且还包装其内容?

这就是我要的:

在此处输入图片说明

我用wrap_width和horizo​​ntal_bias实现了

<TextView
      android:id="@+id/textview_chatmodel_message"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:background="@drawable/bubble_user"
      android:padding="8dp"
      android:text="Username: aaaaaaaa "
      android:textColor="@color/colorBlack"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintLeft_toRightOf="@id/guideline1"
      app:layout_constraintHorizontal_bias="1"
      android:gravity="right"/>
Run Code Online (Sandbox Code Playgroud)

但这会导致不遵守Left_toRightOf guideline约束的问题:

在此处输入图片说明

如何获得该消息,以便使消息布局位于准则的右侧,并且还包装其内容?

android android-constraintlayout

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

How to achieve the following layout with dynamic width in Constraint Layout?

I have a constraint layout with a simple card view with a text inside, and a text at the bottom. I want the card's width to be at least the width of the bottom text. So something like setting a minimum width.

I drew a picture illustrating what I want but wasn't able to implement it in Constraint Layout.

布局

android android-layout android-constraintlayout

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