垂直线不在垂直方向上扩展 [Android形状]

3
    <?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:visible="true"
    android:shape="line">

<stroke
        android:width="1dp"
        android:color="#008000"
        android:dashWidth="2dp"
        android:dashGap="2dp"/>


</shape>

#horizontal_line.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate
    android:fromDegrees="90"
    android:toDegrees="90"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/horizontal_line"
    >

</rotate>

#以上是垂直线的XML代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@color/dark_red_color">


<View
        android:layout_width="50dp"
        android:layout_height="310dp"
        android:background="@drawable/vertical_line"
        >

</View>
</LinearLayout>

我正在尝试在以上布局或视图中使用竖线。但是,竖线无法完全延伸到其高度。


1
你有解决方案吗?因为我也遇到了同样的问题。 - Deepak Rana
我有一个解决方案。在您的布局xml中使用负边距,像这样:<ImageView android:layout_width="100dp" android:layout_height="100dp" android:layout_marginLeft="-50dp" android:layout_marginRight="-50dp" android:src="@drawable/dotted_vertical_line" /> - Darsh Sanghvi
2个回答

2
我有一个解决方法。在您的布局xml中使用负边距,像这样:

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginLeft="-50dp"
    android:layout_marginRight="-50dp"
    android:src="@drawable/dotted_vertical_line" />

1

您可以使用此代码代替shape

<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="#FF0000FF"
android:rotation="90"
android:background="@android:color/darker_gray"/>

如果你想使用 Shape 而不是使用 line,你可以像这样使用 rectangle

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke android:width="1dp" android:color="@color/red"/>
<size android:width="2dp" android:height="290dp" />
</shape>

编辑

您需要将旧形状替换为此形状,我认为它会起作用:)

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:shape="rectangle">
    <stroke
       android:width="1dp"
       android:dashWidth="10px"
       android:dashGap="10px"
       android:rotation="90" 
       />
    </shape>

我想要一个垂直扩展的虚线。通过简单的背景/可绘制图像可以实现,但它无法生成虚线。 - jily
通过减小矩形的宽度...左右两边的线重叠,因此无法实现完美的垂直虚线。 - jily

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接