2014-10-11 69 views
0

我想繪製一個灰色的矩形,其周圍只有一個紅色的邊框,但是我已經看到這種方式有許多不同的方式,所以我希望有人告訴我爲了達到這個目的,哪種方法最好?以下是我的代碼,但我想知道是否有更簡單的方法。沒有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> 

回答

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> 

您可以更改所需的顏色代碼和邊框寬度。

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> 

+0

好的,你知道我怎樣才能將我的親戚從左側而不是中間水平對齊30%?請檢查我的代碼上面的標題**佈局不對齊** – MacaronLover 2014-10-11 22:04:58

+0

我認爲這似乎很大綱外....因爲沒有%的單位...和相對佈局不固定......我認爲這看起來不可能使用XML ....但通過Java代碼,我們可以做到這一點... – 2014-10-11 22:11:51

0

LinearLayout和RelativeLayout是視圖組。相反,他們使用簡單的視圖

android:background =「@ drawable/rectangle」 attribute(see next answer)。

+0

請不要參考下一個答案(這依賴於排序),而是複製粘貼必要的代碼。 – tacone 2014-10-11 20:28:35