如何在android relativelayout周围放置边框?

Aya*_*dro 18 android border android-relativelayout

我已经看过这个 关于在an​​droid textview周围设置边框的主题,并且我使用了它.但现在,我想在小部件周围放置边框,这些小部件位于相对布局中.我该怎么做?

RWI*_*WIL 46

  1. 在您的res/drawable文件夹中,创建一个新文件background_border.xml

在此文件中,您将为此窗口小部件定义背景:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" >
    <!-- This is the stroke you want to define -->
    <stroke android:width="1dp" 
            android:color="@color/color_stroke"/>

    <!-- Optional, round your corners -->
    <corners android:bottomLeftRadius="0dp"
             android:topLeftRadius="5dp"
             android:bottomRightRadius="5dp"
             android:topRightRadius="0dp" />

    <!-- Optional, fill the rest of your background with a color or gradient, use transparent if you only want the border to be displayed-->
    <gradient android:startColor="@android:color/transparent"
              android:endColor="@android:color/transparent"
              android:angle="90"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
  1. 将窗口小部件的背景设置为刚刚创建的可绘制配置

例如.如果你想把你的边框放在relativelayout上:

<RelativeLayout            
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/background_border"
            android:padding="15dp">
    ...
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)


小智 11

RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.borderEffect); // id fetch from xml
ShapeDrawable rectShapeDrawable = new ShapeDrawable(); // pre defined class

// get paint
Paint paint = rectShapeDrawable.getPaint();

// set border color, stroke and stroke width
paint.setColor(Color.GRAY);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(5); // you can change the value of 5
layout.setBackgroundDrawable(rectShapeDrawable);
Run Code Online (Sandbox Code Playgroud)


mah*_*mah 1

创建一个获取边框的背景颜色以及边框宽度的边距或填充的 FrameLayout,并将该 FrameLayout 放置在您的relativelayout 中。将 TextView 放置在 FrameLayout 中,而不是直接放置在relativelayout 中。即时边框。