安卓 : How to Create Dividers between Components

标签 android android-layout user-interface

我的目标是获得类似于下图所示的分隔线: Goal Picture Divider

我需要在 LinearLayout 之间放置一个水平和垂直分隔线

这是我的用户界面 XML 代码:

<?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"
tools:context=".MainActivity"
android:background="@drawable/maingradiant">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:orientation="vertical"
    android:gravity="center">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:gravity="center"
        android:orientation="horizontal">

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:gravity="center"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/wheatallergyambericon" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="اطلاعات ورودی"
                android:textColor="@android:color/black"
                android:textSize="16sp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/wheatallergyambericon" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="فاکتور"
                android:textColor="@android:color/black"
                android:textSize="16sp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/wheatallergyambericon" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="هزینه ها"
                android:textColor="@android:color/black"
                android:textSize="16sp"
                android:textStyle="bold" />
        </LinearLayout>

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:gravity="center"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/wheatallergyambericon" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="تسویه"
                android:textColor="@android:color/black"
                android:textSize="16sp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/wheatallergyambericon" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="سامانه"
                android:textColor="@android:color/black"
                android:textSize="16sp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/wheatallergyambericon" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ارسال بار"
                android:textColor="@android:color/black"
                android:textSize="16sp"
                android:textStyle="bold" />
        </LinearLayout>

    </LinearLayout>


</LinearLayout>

我目前正在使用 RelativeLayout 作为 Root Element,并且对于每一行,我都应用水平 LinearLayout,然后是垂直 < b>线性布局。 根据我目前的布局,我能否获得关于如何在我的界面上放置分隔线的帮助,就像提供的图片一样?
谢谢!

最佳答案

首先,我建议您为此目的使用 RecyclerView 和 GridLayoutManager。 creating recyclerview with gridmanager

但是如果你想坚持当前的设计,你可以通过在 xml 中创建一个 View 来添加行

对于垂直线

<View
    android:layout_width="2dp" //thickness 
    android:layout_height="match_parent"
    android:background="@color/colorPrimary" />

对于水平线

<View
        android:layout_width="match_parent"  
        android:layout_height="2dp"//thickness
        android:background="@color/colorPrimary" />

对于阴影颜色,你应该在 android 中寻找类似渐变的东西。 How ro create Gradient in android

代码如下所示。

<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="@drawable/maingradiant"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="4"
            android:gravity="center"
            android:orientation="horizontal">

        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:gravity="center"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/wheatallergyambericon" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="اطلاعات ورودی"
                    android:textColor="@android:color/black"
                    android:textSize="16sp"
                    android:textStyle="bold" />
            </LinearLayout>

            <View
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="@color/colorPrimary" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/wheatallergyambericon" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="فاکتور"
                    android:textColor="@android:color/black"
                    android:textSize="16sp"
                    android:textStyle="bold" />
            </LinearLayout>

            <View
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="@color/colorPrimary" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/wheatallergyambericon" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="هزینه ها"
                    android:textColor="@android:color/black"
                    android:textSize="16sp"
                    android:textStyle="bold" />
            </LinearLayout>

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@color/colorPrimary" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:gravity="center"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/wheatallergyambericon" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="تسویه"
                    android:textColor="@android:color/black"
                    android:textSize="16sp"
                    android:textStyle="bold" />
            </LinearLayout>

            <View
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="@color/colorPrimary" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/wheatallergyambericon" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="سامانه"
                    android:textColor="@android:color/black"
                    android:textSize="16sp"
                    android:textStyle="bold" />
            </LinearLayout>

            <View
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:background="@color/colorPrimary" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/wheatallergyambericon" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="ارسال بار"
                    android:textColor="@android:color/black"
                    android:textSize="16sp"
                    android:textStyle="bold" />
            </LinearLayout>

        </LinearLayout>


    </LinearLayout>

编码愉快!!

关于安卓 : How to Create Dividers between Components,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55255291/

相关文章:

android - Android Studio中 "(v) ->"是什么意思?

Android Assets 太慢

unity-game-engine - 如果填充文本组件的文本未知,如何调整文本组件的高度?

Java - 连接滚动条但位置不同

java - 监听另一个组件中的事件并等待回复

android - 将 android arsenal 库放入哪个 build.gradle 文件

android - 存储数组或自定义对象(持久数据)的方法是什么?

android - ConstraintLayout 在底部工作 TableView 中无法正常工作

android - 如何使用 layoutinflator 在运行时添加 View ?

Android:更改 Spinner 下拉 View