-->

机器人添加点线布局(android adding dots line to layout)

2019-10-19 05:45发布

我需要的线路点添加到我的布局是这样

这是我的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/deals_list_item_bckg"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/dealImg"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:scaleType="fitXY"
        android:src="@drawable/deals_list_img" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/dealDescription"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:maxLines="3"
            android:text="Lorem Ipsum dolor sit amet dolor sed a ite amkt Lantin dolor latim dk kuitshen sed iditur anet" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TextView
                android:id="@+id/dealNewPrice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_marginRight="5dp"
                android:text="1248$"
                android:textColor="@color/deals_list_new_price"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/dealOldPrice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginRight="15dp"
                android:layout_toLeftOf="@+id/dealNewPrice"
                android:text="2500$"
                android:textColor="@color/deals_list_old_price"
                android:textSize="15sp" />
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

以何种方式,我可以做这件事情我也尝试使用ShapeDrawable,但仍然不明白它是如何工作的,我需要一个例子来看看它是如何工作的,以及如何做这可以说明的TextView和价格textviews之间布局

Answer 1:

尝试这个

注:以上版本,没有android:layerType属性将无法正常工作。 现在添加android:layerType与属性software值,如下

android:layerType="software"

欲了解更多详情,请参见LAYER_TYPE_SOFTWARE

更新后的布局应是如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/rounded_edge"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/dealImg"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:scaleType="fitXY"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/dealDescription"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:maxLines="3"
            android:text="Lorem Ipsum dolor sit amet dolor sed a ite amkt Lantin dolor latim dk kuitshen sed iditur anet" />

        <View
            android:layout_width="match_parent"
            android:layout_height="5dip"
            android:background="@drawable/dash_line"
            android:layerType="software"
            android:orientation="vertical" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TextView
                android:id="@+id/dealNewPrice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_marginRight="5dp"
                android:text="1248$"
                android:textColor="@color/scoreColor"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/dealOldPrice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginRight="15dp"
                android:layout_toLeftOf="@+id/dealNewPrice"
                android:text="2500$"
                android:textColor="@color/scoreColor"
                android:textSize="15sp" />
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

虚线是dash_line.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="line" >

       <solid android:color="#fdfdfd" >
        </solid>

      <stroke
          android:dashGap="5px"
          android:dashWidth="5px"
          android:width="2dp"
          android:color="@color/scoreColor" >
      </stroke>

</shape>


Answer 2:

绘制/ dot_repeated.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/dot"
    android:tileMode="repeat" />

布局

...

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/dealDescription"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:maxLines="3"
        android:text="Lorem Ipsum dolor sit amet dolor sed a ite amkt Lantin dolor latim dk kuitshen sed iditur anet" />

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/dot_height"
        android:background="@drawable/dot_repeated"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

...


文章来源: android adding dots line to layout