【问题标题】:How to put background shapes in ConstraintLayout?如何在 ConstraintLayout 中放置背景形状?
【发布时间】:2020-02-21 05:35:59
【问题描述】:

我嵌套了LinearLayouts,在阅读了一些文档后,我尝试切换到ConstraintLayout。但我没有找到如何插入视图以仅显示背景。
到目前为止,我只是在包含项目的布局上设置了背景。

这是旧嵌套结构的样子(在其他子布局中有更多嵌套项):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/black"
    android:gravity="center" >

 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     ... some margins and padding
     android:background="@drawable/global_background" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center"
        android:background="@drawable/text_area_background" >

        <TextView
        android:id="@+id/Text1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center" />

        <TextView
        android:id="@+id/Text2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center" />
    ...

通过这种方式,我能够轻松地将自己的形状设置为我所显示的不同项目组的背景。

我怎样才能使用ConstraintLayout 达到同等效果?

我了解如何限制我的项目以相同的方式组织,以及如何设置背景以与所有内容保持一致。但是如何插入简单的背景项

【问题讨论】:

    标签: android android-linearlayout android-constraintlayout android-shape


    【解决方案1】:

    我使用ImageView 来显示我的背景,并且(几乎没有)找到一种方法来在其中组装我的物品。
    一个问题是我收到了警告Image without contentDescription

    <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/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black">
    
        <ImageView
            android:id="@+id/background"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="10dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintDimensionRatio="2:5"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0"
            app:srcCompat="@drawable/global_background" />
    
        ...
    
        <Button
            android:id="@+id/buttonValidate"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="2dp"
            android:layout_marginEnd="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="8dp"
            android:background="@drawable/green_button"
            android:text="@string/validate"
            app:layout_constraintBottom_toBottomOf="@+id/background"
            app:layout_constraintDimensionRatio="15:6"
            app:layout_constraintEnd_toEndOf="@+id/background"
            app:layout_constraintStart_toStartOf="@+id/background"
            app:layout_constraintTop_toBottomOf="@+id/upperButton" />
    

    但我很难将另一个背景放入其中(本身包含几个TextView)。我的约束总是弄乱其中一个...我最终得到了一个嵌套的ConstraintLayout,它工作正常,可能不是最佳解决方案,但我所有其他尝试都失败了。

    【讨论】:

      猜你喜欢
      • 2012-03-05
      • 2014-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      相关资源
      最近更新 更多