如何在Android中使用边框和彩色区域的背景

2022-01-25 13:14:38 标签 androidandroid-layout

我想有一个布局,我把其他布局(如约束布局),那些布局应该有边框和彩色区域。所以我想我必须改变背景,但我不知道怎么做。这是我当前片段的截图,我高亮显示了我想添加的东西:

基本上,它看起来应该类似于理想的布局。

这是我拥有的xml布局文件

<?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">
    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_marginTop="@dimen/_28sdp"
        android:layout_width="@dimen/_220sdp"
        android:layout_height="@dimen/_240sdp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.93"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.75">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/_30sdp"
            android:orientation="horizontal"></LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/_190sdp"
            android:orientation="vertical"></LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/_20sdp"
            android:orientation="horizontal"></LinearLayout>
    </LinearLayout>
    <Button
        android:id="@+id/navigation_button_language"
        android:layout_width="@dimen/_31sdp"
        android:layout_height="@dimen/_31sdp"
        android:background="@drawable/navigation_buttom"
        android:text="Dummy"
        android:textAllCaps="false"
        android:textColor="#121212"
        android:textSize="@dimen/_9ssp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.95"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.02" />
    <TextView
        android:id="@+id/textView_language"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_3ssp"
        android:text="Dummy"
        android:textColor="#121212"
        android:textSize="@dimen/_9ssp"
        app:layout_constraintEnd_toEndOf="@+id/navigation_button_language"
        app:layout_constraintStart_toStartOf="@+id/navigation_button_language"
        app:layout_constraintTop_toBottomOf="@+id/navigation_button_language" />
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="@dimen/_240sdp"
        app:layout_constraintBottom_toBottomOf="@+id/linearLayout"
        app:layout_constraintEnd_toStartOf="@+id/linearLayout"
        android:layout_marginLeft="@dimen/_6sdp"
        android:layout_marginRight="@dimen/_6sdp"
        android:background="@color/colorGrey"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/linearLayout"
        app:layout_constraintVertical_bias="0.447">
        <ScrollView
            android:layout_width="47dp"
            android:layout_height="44dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" />
        </ScrollView>
        <TextView
            android:id="@+id/textView_Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test Name"
            android:textColor="#000000"
            android:textSize="@dimen/_14ssp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
        <TextView
            android:id="@+id/textView_QuantityText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_1sdp"
            android:text="Quantity: "
            android:textSize="@dimen/_9ssp"
            app:layout_constraintEnd_toEndOf="@+id/textView_Name"
            app:layout_constraintStart_toStartOf="@+id/textView_Name"
            app:layout_constraintTop_toBottomOf="@+id/textView_Name" />
        <TextView
            android:id="@+id/textView_QuantityNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2"
            android:textSize="@dimen/_9ssp"
            app:layout_constraintBottom_toBottomOf="@+id/textView_QuantityText"
            app:layout_constraintStart_toEndOf="@+id/textView_QuantityText"
            app:layout_constraintTop_toTopOf="@+id/textView_QuantityText" />
        <TextView
            android:id="@+id/textView_TableNumberText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_1sdp"
            android:text="Table: "
            android:textSize="@dimen/_9ssp"
            app:layout_constraintEnd_toEndOf="@+id/textView_QuantityText"
            app:layout_constraintStart_toStartOf="@+id/textView_QuantityText"
            app:layout_constraintTop_toBottomOf="@+id/textView_QuantityText" />
        <TextView
            android:id="@+id/textView_TableNumberValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3"
            android:textSize="@dimen/_9ssp"
            app:layout_constraintBottom_toBottomOf="@+id/textView_TableNumberText"
            app:layout_constraintStart_toEndOf="@+id/textView_TableNumberText"
            app:layout_constraintTop_toTopOf="@+id/textView_TableNumberText" />
        <ImageButton
            android:id="@+id/imageButton_message"
            android:layout_width="@dimen/_25sdp"
            android:layout_height="@dimen/_25sdp"
            android:layout_marginTop="8dp"
            android:background="@drawable/ic_baseline_markunread_24"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.221"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView_TableNumberText" />
        <ImageButton
            android:id="@+id/imageButton_video"
            android:layout_width="@dimen/_25sdp"
            android:layout_height="@dimen/_25sdp"
            android:background="@drawable/ic_baseline_videocam_24"
            app:layout_constraintBottom_toBottomOf="@+id/imageButton_message"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.827"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/imageButton_message" />
        <TextView
            android:id="@+id/textView_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_1sdp"
            android:text="Message"
            app:layout_constraintEnd_toEndOf="@+id/imageButton_message"
            app:layout_constraintStart_toStartOf="@+id/imageButton_message"
            app:layout_constraintTop_toBottomOf="@+id/imageButton_message" />
        <TextView
            android:id="@+id/textView_Video"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Video"
            android:layout_marginTop="@dimen/_1sdp"
            app:layout_constraintEnd_toEndOf="@+id/imageButton_video"
            app:layout_constraintStart_toStartOf="@+id/imageButton_video"
            app:layout_constraintTop_toBottomOf="@+id/imageButton_video" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout> 

知道我该怎么做吗?我将高度感谢每一个评论,并将感谢您的帮助。

提醒:没有人知道我该怎么做吗?我经常看到这样的布局,所以我想做的应该不是非常不寻常的。

# # #回答:

创建一个可绘制的->rim。xml

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
     <stroke
        android:width="@dimen/space_lg"
        android:height="@dimen/space_lg"
        android:color="@color/#fff"
        android:dashGap="@dimen/space_sm" />
</shape>

在布局中只需使用:

 android:background="@drawable/rim"
            

# # #可拉的→rim。xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FFFFFF"/>
    <stroke
        android:width="3dp"
        android:color="#0FECFF"/>
</shape>

然后按照你想要的布局(在图片中),我做了一个简化的相应布局:

<?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:id="@+id/outerConstraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.35" />
    <View
        android:id="@+id/v_left_background"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="8dp"
        android:background="@drawable/rim"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/guideline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:id="@+id/textView_Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="Left Side"
        android:textAlignment="center"
        android:textColor="#000000"
        android:textSize="16sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="@id/v_left_background"
        app:layout_constraintStart_toStartOf="@id/v_left_background"
        app:layout_constraintTop_toTopOf="@id/v_left_background" />
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/ll_navbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@android:color/holo_green_dark"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="@id/v_left_background"
        app:layout_constraintStart_toStartOf="@id/v_left_background"
        app:layout_constraintTop_toBottomOf="@id/textView_Name">
        <ImageView
            android:id="@+id/iv_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_delete"
            app:layout_constraintEnd_toStartOf="@id/iv_2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:tint="@android:color/white" />
        <ImageView
            android:id="@+id/iv_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_delete"
            app:layout_constraintEnd_toStartOf="@id/iv_3"
            app:layout_constraintStart_toEndOf="@id/iv_1"
            app:layout_constraintTop_toTopOf="parent"
            app:tint="@android:color/white" />
        <ImageView
            android:id="@+id/iv_3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_delete"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/iv_2"
            app:layout_constraintTop_toTopOf="parent"
            app:tint="@android:color/white" />
        <TextView
            android:id="@+id/tv_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Order sheet"
            android:textAlignment="center"
            android:textColor="#fff"
            android:textSize="12sp"
            app:layout_constraintEnd_toEndOf="@id/iv_1"
            app:layout_constraintStart_toStartOf="@id/iv_1"
            app:layout_constraintTop_toBottomOf="@id/iv_1" />
        <TextView
            android:id="@+id/tv_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Order"
            android:textAlignment="center"
            android:textColor="#fff"
            android:textSize="12sp"
            app:layout_constraintEnd_toEndOf="@id/iv_2"
            app:layout_constraintStart_toStartOf="@id/iv_2"
            app:layout_constraintTop_toBottomOf="@id/iv_2" />
        <TextView
            android:id="@+id/tv_3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Arrange products"
            android:textAlignment="center"
            android:textColor="#fff"
            android:textSize="12sp"
            app:layout_constraintEnd_toEndOf="@id/iv_3"
            app:layout_constraintStart_toStartOf="@id/iv_3"
            app:layout_constraintTop_toBottomOf="@id/iv_3" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="8dp"
        app:layout_constraintBottom_toTopOf="@id/button_okay"
        app:layout_constraintEnd_toEndOf="@id/v_left_background"
        app:layout_constraintStart_toStartOf="@id/v_left_background"
        app:layout_constraintTop_toBottomOf="@id/ll_navbar" />
    <Button
        android:id="@+id/button_okay"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@android:color/holo_green_light"
        android:text="Okay"
        android:textAllCaps="false"
        android:textColor="#fff"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="@id/v_left_background"
        app:layout_constraintEnd_toEndOf="@id/v_left_background"
        app:layout_constraintStart_toStartOf="@id/v_left_background" />
    <View
        android:id="@+id/v_right_background"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:background="@drawable/rim"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/guideline"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

然后屏幕看起来像这样:

Constraint-wise:

(您可以在xml的设计标签中检查预览,我希望在代码中的约束是详细的,足以让您能够遵循)

更新

代码没有指南:

<?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:id="@+id/outerConstraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">
    <View
        android:id="@+id/v_left_background"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="8dp"
        android:background="@drawable/rim"
        app:layout_constraintHeight_percent="0.8"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.35" />
    <TextView
        android:id="@+id/textView_Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="Left Side"
        android:textAlignment="center"
        android:textColor="#000000"
        android:textSize="16sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="@id/v_left_background"
        app:layout_constraintStart_toStartOf="@id/v_left_background"
        app:layout_constraintTop_toTopOf="@id/v_left_background" />
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/ll_navbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@android:color/holo_green_dark"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="@id/v_left_background"
        app:layout_constraintStart_toStartOf="@id/v_left_background"
        app:layout_constraintTop_toBottomOf="@id/textView_Name">
        <ImageView
            android:id="@+id/iv_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_delete"
            app:layout_constraintEnd_toStartOf="@id/iv_2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:tint="@android:color/white" />
        <ImageView
            android:id="@+id/iv_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_delete"
            app:layout_constraintEnd_toStartOf="@id/iv_3"
            app:layout_constraintStart_toEndOf="@id/iv_1"
            app:layout_constraintTop_toTopOf="parent"
            app:tint="@android:color/white" />
        <ImageView
            android:id="@+id/iv_3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_delete"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/iv_2"
            app:layout_constraintTop_toTopOf="parent"
            app:tint="@android:color/white" />
        <TextView
            android:id="@+id/tv_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Order sheet"
            android:textAlignment="center"
            android:textColor="#fff"
            android:textSize="12sp"
            app:layout_constraintEnd_toEndOf="@id/iv_1"
            app:layout_constraintStart_toStartOf="@id/iv_1"
            app:layout_constraintTop_toBottomOf="@id/iv_1" />
        <TextView
            android:id="@+id/tv_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Order"
            android:textAlignment="center"
            android:textColor="#fff"
            android:textSize="12sp"
            app:layout_constraintEnd_toEndOf="@id/iv_2"
            app:layout_constraintStart_toStartOf="@id/iv_2"
            app:layout_constraintTop_toBottomOf="@id/iv_2" />
        <TextView
            android:id="@+id/tv_3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Arrange products"
            android:textAlignment="center"
            android:textColor="#fff"
            android:textSize="12sp"
            app:layout_constraintEnd_toEndOf="@id/iv_3"
            app:layout_constraintStart_toStartOf="@id/iv_3"
            app:layout_constraintTop_toBottomOf="@id/iv_3" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="8dp"
        app:layout_constraintBottom_toTopOf="@id/button_okay"
        app:layout_constraintEnd_toEndOf="@id/v_left_background"
        app:layout_constraintStart_toStartOf="@id/v_left_background"
        app:layout_constraintTop_toBottomOf="@id/ll_navbar" />
    <Button
        android:id="@+id/button_okay"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@android:color/holo_green_light"
        android:text="Okay"
        android:textAllCaps="false"
        android:textColor="#fff"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="@id/v_left_background"
        app:layout_constraintEnd_toEndOf="@id/v_left_background"
        app:layout_constraintStart_toStartOf="@id/v_left_background" />
    <View
        android:id="@+id/v_right_background"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:background="@drawable/rim"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent="1"
        app:layout_constraintStart_toEndOf="@id/v_left_background"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.6" />
</androidx.constraintlayout.widget.ConstraintLayout>
阅读全文

▼ 版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说 cuoshuo.com —— 程序员的报错记录

部分内容根据CC版权协议转载;网站内容仅供参考,生产环境使用务必查阅官方文档

辽ICP备19011660号-5

×

扫码关注公众号:职场神器
发送: 1
获取永久解锁本站全部文章的验证码