硬编码带边框的矩形

时间:2014-10-11 18:38:45

标签: android xml android-layout android-activity

我想画一个灰色的矩形,它周围只有一个红色的边框,但是我已经看到过这种方式有很多不同的方式,所以我希望有人告诉我哪个是为了达到这个目的最好的?下面是我的代码,但我想知道是否有更简单的方法。请不要java。我只想要xml。

            <RelativeLayout
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:background="@color/red"
            android:layout_weight=".25">

            <RelativeLayout
                android:layout_width="7dp"
                android:layout_height="7dp"
                android:background="@color/grey"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>
        </RelativeLayout>

布局未对齐

align error

        <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:background="@color/grey"
        android:layout_weight=".25" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="3"
            android:textColor="@color/black"/>

        <RelativeLayout
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:background="@color/red"
            android:layout_weight=".25">

            <RelativeLayout
                android:layout_width="7dp"
                android:layout_height="7dp"
                android:background="@color/grey"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="15dp"
            android:layout_height="10dp"
            android:background="@color/red"
            android:layout_alignParentLeft="true">

            <RelativeLayout
                android:layout_width="12dp"
                android:layout_height="7dp"
                android:background="@color/grey"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="15dp"
            android:layout_height="10dp"
            android:background="@color/red"
            android:layout_alignParentRight="true">

            <RelativeLayout
                android:layout_width="12dp"
                android:layout_height="7dp"
                android:background="@color/grey"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="15dp"
            android:layout_height="10dp"
            android:background="@color/red"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true">

            <RelativeLayout
                android:layout_width="12dp"
                android:layout_height="7dp"
                android:background="@color/grey"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="15dp"
            android:layout_height="10dp"
            android:background="@color/red"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:padding="1.5dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/grey"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="15dp"
            android:layout_height="10dp"
            android:background="@color/red"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true">

            <RelativeLayout
                android:layout_width="12dp"
                android:layout_height="7dp"
                android:background="@color/grey"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>
        </RelativeLayout>
    </RelativeLayout>

3 个答案:

答案 0 :(得分:1)

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:background="#ff0000"
android:padding="10dp">

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:background="#f4f4f4"
    >

</RelativeLayout>

答案 1 :(得分:1)

在你的绘画中 rectangle.xml

<?xml version="1.0" encoding="UTF-8"?> 

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

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

    <stroke
        android:width="0.5dp"
        android:color="#ff0000" >
    </stroke>

 </shape>

你的布局。

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

您可以更改所需的颜色代码和边框宽度。

答案 2 :(得分:0)

LinearLayout和RelativeLayout是视图组。相反,他们使用简单的View with

  

机器人:背景= “@绘制/矩形”   属性(见下一个答案)。