【问题标题】:Add Lines around an Image在图像周围添加线条
【发布时间】:2020-04-18 12:33:02
【问题描述】:

各位,

我想在我的 Android 屏幕中添加 4 行视图。我可以添加 2 条线,一条在顶部,一条在左侧,但我不知道如何在右侧添加一条,在底部添加一条。

这是我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:background="@color/colorPrimary"
    app:layout_constraintHeight_percent="0.25"
    app:layout_constraintWidth_percent="0.25"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imvBalle"
        android:layout_width="24dp"
        android:layout_height="20dp"
        android:layout_gravity="center"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.482"
        app:srcCompat="@drawable/balle" />

    <View
        android:id="@+id/lineTop"
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:background="@android:color/darker_gray" />

    <View
        android:id="@+id/lineLeft"
        android:layout_width="10dp"
        android:layout_height="match_parent"
        android:background="@android:color/darker_gray" />

</android.support.constraint.ConstraintLayout>

【问题讨论】:

    标签: android android-layout android-constraintlayout


    【解决方案1】:

    使用layout_constraintEnd_toEndOflayout_constraintBottom_toBottomOf 约束:

        <View
            android:id="@+id/lineRight"
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:background="@android:color/darker_gray"
            app:layout_constraintEnd_toEndOf="parent" />
    
        <View
            android:id="@+id/lineBottom"
            android:layout_width="10dp"
            android:layout_height="match_parent"
            android:background="@android:color/darker_gray"
            app:layout_constraintBottom_toBottomOf="parent" />
    

    【讨论】:

    • 它不起作用;它保持在左侧和底部的相同位置
    • 很高兴能帮上忙
    【解决方案2】:

    您可以通过创建边框形状在视图周围添加背景。

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item >
            <shape android:shape="rectangle" >
    
                <stroke 
                    android:width="1dp"
                    android:color="#000000"
                    android:dashWidth="5dp"></stroke>
    
            </shape>
        </item>
    </selector>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-12
      相关资源
      最近更新 更多