2013-02-19 83 views
3

我的問題是,如果有一種方法可以在ScrollView周圍放置一個簡單的黑線邊框,以便用戶確切知道ScrollView開始,停止和它的寬度。我無法在Android文檔中找到任何類型的XML或Java代碼來說明如何執行此操作。我知道這一定是可能的。把滾動視圖周圍的黑線邊框 - Android

以下是我的ScrollView的XML代碼,我需要有一個邊框。

<LinearLayout 
      .... /> 


    <TextView 
     ... /> 

//BORDER STARTS HERE   
    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="94.5" 
     android:paddingBottom="5dp" 
     android:fillViewport="true" > 

     <LinearLayout 
      ... > 

      <TextView 
       ... /> 

     </LinearLayout> 
    </ScrollView> 
//BORDER ENDS HERE 

    <Button 
     ... /> 
</LinearLayout> 

編輯

我只是增加了一個scrollviewborder.xml用下面的代碼作爲ScrollView的背景。

<?xml version="1.0" encoding="utf-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <stroke 
     android:width="2dp" 
     android:color="#000000"/> 
</shape> 

下面的截圖就是它產生:

enter image description here

這不是完全是我想要的。我想要一個很窄的黑線,而不是一個黑匣子。

回答

7

在你的新要求輕,試試下面的代碼:

<?xml version="1.0" encoding="utf-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 
    <solid 
     android:color="@android:color/transparent"/> 
    <stroke 
     android:width="2dp" 
     android:color="#000000"/> 
</shape> 
+0

當然,如果您想要黑色邊框,請用邊距替換marginTop和marginBottom。 – 2013-02-19 16:20:25

+0

我有一個背景圖片,用於我想要保留並顯示在ScrollView中的整個活動。請看我編輯的開幕帖子。 – Matt 2013-02-20 12:53:18

+1

我已更新我的回答 – 2013-02-20 13:34:29

7

您可以將其背景設置爲帶筆劃的shape drawable

例子:

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:layout_weight="94.5" 
    android:paddingBottom="5dp" 
    android:background="@drawable/stroke_bg" 
    android:fillViewport="true" > 

而在你的可繪製文件夾中,有一個stroke_bg.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <stroke 
     android:width="2dp" 
     android:color="#000000"/> 
</shape> 

這將導致與寬度2DP的黑色邊框滾動視圖。

+0

請看我編輯過的開場白。我試過你的解決方案,沒有得到我想要的結果。 – Matt 2013-02-20 12:52:28

1

Is there an easy way to add a border to the top and bottom of an Android View?

有Android中沒有內置的「邊境」的概念,我能想到的。可以模擬一個通佈局,如:

裹TextView的一個的LinearLayout並添加上面和下面它的平面圖,與具有所希望的機器人視圖:背景顏色和適當的機器人:layout_height(例如,1像素,1dip)。

將TextView包裝在LinearLayout中,然後將ImageView小部件添加到所需的邊框圖像的上方和下方。

將TextView包裹在RelativeLayout中,添加一個平鋪視圖(高度適當的背景&),錨定到底部的另一個平面視圖以及錨定到頂部和底部的TextView。這利用了RelativeLayout的z軸支持,所以邊界將位於TextView佔用的空間內,而不像前兩種方法那樣位於TextView之外。

爲TextView提供九個修補程序的PNG文件作爲具有邊框的背景。從XML的角度來看,這是最簡單的,但是你必須製作一個合適的九塊圖像。

2

不要像佈局一樣包裝它,因爲有人建議。這會使你的應用渲染速度變慢(可能不明顯,但仍然)。 相反,創建一個只定義一個孩子的形狀,並將其用作ScrollView的背景。