有沒有一種簡單的方法能夠在Android View的頂部和底部添加邊框?

我有一個TextView,我想沿其頂部和底部邊框添加黑色邊框。 我嘗試將android:drawableTopandroid:drawableBottom到TextView中,但這隻會致使整個視圖變黑。 android

<TextView
    android:background="@android:color/green"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawableTop="@android:color/black"
    android:drawableBottom="@android:color/black"
    android:text="la la la" />

有沒有一種方法能夠在Android中輕鬆地向視圖(尤爲是TextView)添加上下邊框? 佈局


#1樓

您還可使用9條路徑完成您的工做。 建立它以使彩色像素不會在高度上相乘,而只會在透明像素上相乘。 spa


#2樓

我使用了一個技巧,使邊框顯示在容器外部。 使用此技巧時,僅畫一條線,所以將顯示基礎視圖的背景。 code

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:bottom="1dp"
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="1dp"
                android:color="#FF000000" />

            <solid android:color="#00FFFFFF" />

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

</layer-list>

#3樓

選項1:可繪製形狀

若是要在能夠設置背景的佈局或視圖周圍使用邊框,這是最簡單的選項。 在drawable文件夾中建立一個以下所示的XML文件: xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#8fff93" />

    <stroke
        android:width="1px"
        android:color="#000" />

</shape>

若是不想填充,能夠刪除solid 。 在您的佈局/視圖上設置background="@drawable/your_shape_drawable"對象

選項2:背景視圖

這是我在RelativeLayout使用的一個小技巧。 基本上,您要提供邊框的視圖下有一個黑色正方形,而後對該視圖進行填充(不是邊距!),這樣黑色正方形就會在邊緣顯示出來。 utf-8

顯然,這僅在視圖沒有透明區域的狀況下才能正常工做。 若是能夠的話,我建議您編寫一個自定義的BorderView ,它僅繪製邊框-它應該只有幾十行代碼。 it

<View
    android:id="@+id/border"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/image"
    android:layout_alignLeft="@+id/image"
    android:layout_alignRight="@+id/image"
    android:layout_alignTop="@+id/main_image"
    android:background="#000" />

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_...
    android:padding="1px"
    android:src="@drawable/..." />

若是你想知道,它確實與工做adjustViewBounds=true 。 可是,若是您想在整個RelativeLayout使用背景,則沒法使用,由於有一個錯誤使您沒法使用View填充RelativeLayout 。 在這種狀況下,我建議使用Shape drawable。 io

選項3:9片

最後一種選擇是使用9補丁可繪製對象,以下所示: 容器

您能夠在能夠設置android:background="@drawable/..."任何視圖上使用它。 是的,它確實必須是6x6-我嘗試了5x5,但沒有用。

這種方法的缺點是您不能很容易地更改顏色,可是若是您想要精美的邊框(例如,僅在頂部和底部的邊框,如本問題所述),則可能沒法使用Shape進行更改。 drawable,不是很強大。

選項4:額外的意見

若是您只須要視圖上方和下方的邊框,我會忘記說起這個很是簡單的選項。 您能夠將視圖放入垂直的LinearLayout (若是還沒有放置),而後在其上方和下方添加空的View ,以下所示:

<View android:background="#000" android:layout_width="match_parent" android:layout_height="1px"/>

#4樓

個人答案基於@Emile版本,但我使用透明顏色而不是純色。
本示例將繪製2dp底部邊框。

<?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="2dp"
                     android:color="#50C0E9" />
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
    <item  android:bottom="2dp" >
        <shape android:shape="rectangle" >
            <stroke  android:width="2dp"
                     android:color="@color/bgcolor" />
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
</layer-list>

@ color / bgcolor是繪製帶有邊框的視圖時背景的顏色。

若是要更改邊框的位置,請使用如下其中一項更改偏移量:

android:bottom="2dp"
android:top="2dp"
android:right="2dp"
android:left="2dp"

或合併它們以具備2個或更多邊界:

android:bottom="2dp" android:top="2dp"

#5樓

<TextView
    android:id="@+id/textView3"
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="#72cdf4"
    android:text=" aa" />

只需在要添加邊框的文本下方添加此TextView

相關文章
相關標籤/搜索