2011-11-30 54 views
15
<LinearLayout 
      android:layout_width="fill_parent" android:layout_height="wrap_content" 
      android:orientation="vertical" android:background="@drawable/settings_border" android:padding="1dp" 
      android:layout_marginLeft="15dip" android:layout_marginRight="15dip" android:layout_marginTop="5dip"> 
      <RelativeLayout android:id="@+id/map_refresh" 
       android:layout_height="fill_parent" android:layout_width="wrap_content" 
       android:background="@drawable/settings_selector_up" 
       android:padding="15dp"> 
       <TextView android:id="@+id/Text1" 
        android:layout_height="wrap_content" android:layout_width="wrap_content" 
        android:text="Map refresh period"> 
       </TextView> 
       <TextView android:id="@+id/TextView09" 
        android:layout_height="wrap_content" android:layout_width="wrap_content" 
        android:text="1 min" 
        android:layout_alignParentRight="true" 
        android:paddingRight="5dp"> 
       </TextView> 
      </RelativeLayout> 

      <RelativeLayout android:id="@+id/own_location" 
       android:layout_height="fill_parent" android:layout_width="wrap_content" 
       android:padding="15dp" 
       android:background="@drawable/settings_selector_mid"> 
       <TextView android:id="@+id/Text1" 
        android:layout_height="wrap_content" android:layout_width="wrap_content" 
        android:text="Own location update period"> 
       </TextView> 
       <TextView android:id="@+id/TextView09" 
        android:layout_height="wrap_content" android:layout_width="wrap_content" 
        android:text="1 min" 
        android:layout_alignParentRight="true" 
        android:paddingRight="5dp"> 
       </TextView> 
      </RelativeLayout> 

我只想在relativelayout中設置底部邊框。我想顯示列表視圖風格,但不使用列表視圖。可以說每個listitem都是relativlayout。我只想設置底部邊框,使其看起來像一個列表視圖的分隔線。如何在相關佈局中添加底部邊框

+0

這是我回答一個非常類似的問題。問題是通過一個輕量級的直接庫來解決的。 http://stackoverflow.com/a/30802089/3649279 – PAD

+0

請嘗試我的答案[BorderedRelativeLayout](https://stackoverflow.com/a/45959525/6258971) – suulisin

回答

12

我希望我能理解你說的話。

在res

文件夾中創建一個新的文件夾(如果你還沒有的話)命名繪製

那裏創建一個名爲「borders.xml」的XML

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

    <item android:state_pressed="true"><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
     <gradient android:angle="90" android:centerColor="#6da23f" android:endColor="#8bc45d" android:startColor="#2f481c" /> 

      <stroke android:width="2dp" android:color="#999999" /> 

      <padding android:bottom="4dp" android:left="3dp" android:right="3dp" android:top="6dp" /> 

      <corners android:radius="10px"/> 
     </shape> 
    </item> 
    <item> 
     <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
      <gradient android:angle="90" android:centerColor="#6da23f" android:endColor="#8bc45d" android:startColor="#4c8a39" /> 

      <stroke android:width="1dp" android:color="#FFFFFF" /> 

      <padding android:bottom="4dp" android:left="3dp" android:right="3dp" android:top="6dp" /> 

      <corners android:radius="10px"/> 
     </shape> 
    </item> 
</selector> 

您可以進一步編輯隨你便。 然後從「大綱」中選擇佈局,然後單擊「背景屬性」,然後選擇您創建的邊框xml。

這將爲所有4.邊框或者,你可以添加一個簡單的

<View android:layout_width="1dip" 
    android:layout_height="fill_parent" 
    android:background="#FFFFFF" /> 

行並將其添加到您的佈局的底部,並改變顏色/尺寸根據自己的喜好。

+9

它使所有4個邊框可見,而不僅僅是底部。 –

0

顯示邊框的簡單方法是創建一個水平的LinearLayout,並根據所需的邊框設置其背景顏色和高度。

+1

我不認爲這是一個好主意:LinearLayout不是用來製作邊界的... – mithrop

+0

不,這不是一個好方法 –

+0

是一種簡單的方法,並且您在邊界上獲得了一些屬性。 – Charleston

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

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <stroke 
     android:width="1dip" 
     android:color="#FF8000" /> 

    <solid 
     android:color="#00FFFFFF" 
     android:paddingLeft="10dip" 
     android:paddingTop="10dip"/> 

    <corners android:radius="10px"/> 

    <padding 
     android:left="10dip" 
     android:top="10dip" 
     android:right="10dip" 
     android:bottom="10dip" /> 
</shape> 

您可以在res /文件夾繪製這個保存爲borderframe.xml(創建它,如果它不存在尚未),並引用它,如下所示:android:background="@drawable/borderframe"

10

由於某些原因,其他解決方案對我無效 - 無論我如何改變它,所有邊框都會顯示。這工作:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item> 
     <shape 
      android:shape="rectangle"> 
      <stroke android:width="1dp" android:color="@color/gray" /> 
      <solid android:color="@color/gray" /> 
     </shape> 
    </item> 

    <item android:bottom="1dp"> 
     <shape 
      android:shape="rectangle"> 
      <stroke android:width="1dp" android:color="@color/white" /> 
      <solid android:color="@color/white" /> 

     </shape> 
    </item> 
</layer-list> 

邊框的顏色是灰色背景是容器白色(LinearLayout中在我的例子)。您可以簡單地更改第二個項目,使邊框變粗或在頂部/左側/右側有邊框。

這是佈局的xml的樣子:

<LinearLayout 
     android:id="@+id/searchWrapper" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="5dp" 
     android:paddingBottom="5dp" 
     android:background="@drawable/bottom_gray_border" 
     android:layout_alignParentTop="true"> 

     <EditText 
      android:id="@+id/searchEditText" 
      style="@style/EditTextSearch" 
      android:hint="@string/find" 
      android:layout_marginLeft="5dp"/> 

</LinearLayout> 

我從這裏的想法:Is there an easy way to add a border to the top and bottom of an Android View?

+0

適用於我的作品 – akohout

+0

我只想添加下邊框,它添加了所有4個邊框。 –

+0

這是一個爲我工作的人,最後,謝謝! – OneThreeSeven

相關問題