【问题标题】:how to draw horizontal and vertical lines in android shape如何在android形状中绘制水平线和垂直线
【发布时间】:2021-07-29 11:18:13
【问题描述】:

我正在尝试在 android 的矩形中绘制垂直和水平线。 我的代码已准备好绘制一个矩形,但我遇到了水平线和垂直线问题

我绘制矩形的代码

<?xml version="1.0" encoding="UTF-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rectangle_background_shape">
    <stroke android:width="3dp" android:color="#0000FF"  />
    <padding android:left="0dp"
        android:top="0dp"
        android:right="0dp"
        android:bottom="0dp" />
    <corners android:radius="2dp" />
</shape>

我正在尝试绘制下面的图像

【问题讨论】:

    标签: android xml kotlin drawable


    【解决方案1】:

    如果您使用的是ConstraintLayout,请尝试以下代码

    <?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"
     android:padding="10dp">
    
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/lay_bg"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    
        <View
            android:layout_width="3dp"
            android:layout_height="0dp"
            android:background="#0000FF"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <View
            android:layout_width="0dp"
            android:layout_height="3dp"
            android:background="#0000FF"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
     </androidx.constraintlayout.widget.ConstraintLayout>
     </androidx.constraintlayout.widget.ConstraintLayout>
    

    如果你想在那个盒子里做一些单独的事情,那么试试GridViewRecyclerViewGridLayoutManager检查this

    注意:android:background="@drawable/lay_bg" 它是矩形的可绘制对象

    如果您想尝试使用LinearLayout,请尝试以下代码

    <?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"
    android:padding="10dp">
    
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/lay_bg"
        android:orientation="vertical"
        android:weightSum="2"
        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="0dp"
            android:layout_weight="1"
            android:weightSum="2">
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />
    
            <View
                android:layout_width="3dp"
                android:layout_height="match_parent"
                android:background="#0000FF" />
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        </LinearLayout>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:layout_gravity="center"
            android:background="#0000FF" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:weightSum="2">
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />
    
            <View
                android:layout_width="3dp"
                android:layout_height="match_parent"
                android:background="#0000FF" />
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        </LinearLayout>
    
    </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    Output for both code

    【讨论】: