2013-02-27 31 views

Trả lời

147

Chắc chắn. Bạn có thể thêm đường viền vào bất kỳ bố cục nào bạn muốn. Về cơ bản, bạn cần tạo một drawable tùy chỉnh và thêm nó làm nền cho layout của bạn. Ví dụ:

Tạo một tập tin gọi customborder.xml trong thư mục drawable của bạn:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <corners android:radius="20dp"/> 
    <padding android:left="10dp" android:right="10dp" android:top="10dp" android:bottom="10dp"/> 
    <stroke android:width="1dp" android:color="#CCCCCC"/> 
</shape> 

Bây giờ áp dụng nó như là một nền tảng để bố trí nhỏ hơn của bạn:

<LinearLayout android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/customborder"> 

Điều đó sẽ làm các trick.

Xem thêm:

+25

sẽ tô màu bằng hình chữ nhật. bạn chỉ cần sử dụng để chỉ vẽ đường viền. – NightFury

+1

Bí quyết ở đây thực sự là , đã cho tôi một thời gian để tìm hiểu lý do tại sao hình dạng của tôi không hoạt động. Với đệm nó hoạt động tốt. – John

+3

Cả hai ' 'và' 'điền vào toàn bộ hình chữ nhật? Đây có phải là lỗi trong mã của tôi không? – mr5

17

Tạo XML gọi border.xml trong thư mục drawable như sau:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
    <shape android:shape="rectangle"> 
     <solid android:color="#FF0000" /> 
    </shape> 
    </item> 
    <item android:left="5dp" android:right="5dp" android:top="5dp" > 
    <shape android:shape="rectangle"> 
     <solid android:color="#000000" /> 
    </shape> 
    </item>  
</layer-list> 

sau đó thêm này để bố trí tuyến tính như backgound như sau:

 android:background="@drawable/border" 
8

Hãy thử điều này:

Ví dụ, chúng ta hãy xác định res/drawable/my_custom_background.xml như:

(tạo bố trí này trong thư mục drawable của bạn) layout_border.xml

<?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:height="2dp" 
       android:color="#FF0000" /> 
      <solid android:color="#000000" /> 
      <padding android:left="1dp" android:top="1dp" android:right="1dp" 
       android:bottom="1dp" /> 

      <corners android:radius="1dp" android:bottomRightRadius="5dp" 
       android:bottomLeftRadius="0dp" android:topLeftRadius="5dp" 
       android:topRightRadius="0dp" /> 
     </shape> 
    </item> 

</layer-list> 

main.xml

<LinearLayout 
    android:layout_gravity="center" 
    android:layout_width="200dp" 
    android:layout_height="200dp" 
    android:background="@drawable/layout_border" /> 
</LinearLayout> 
9

Crea te một tập tin một xml trong thư mục drawable

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

<padding 
    android:bottom="5dp" 
    android:left="5dp" 
    android:right="5dp" 
    android:top="5dp" /> 

<corners android:radius="4dp" /> 

Bây giờ gọi xml này để nền bố trí nhỏ của bạn

android: background = "@ drawable/yourxml"

+0

Yếu tố đột quỵ không được phép ở đây. – hdavidzhu

3

Giải pháp này sẽ chỉ thêm đường viền, phần thân của LinearLayout sẽ trong suốt.

Thứ nhất, Tạo drawable biên giới này trong thư mục drawable, border.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="#ec0606"/> 
    <corners android:radius="10dp"/> 
</shape> 

Sau đó, trong LinearLayout Xem của bạn, thêm biên giới.xml làm hình nền như thế này

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@drawable/border"> 
1

bạn có thể làm điều đó một cách thực dụng cũng

GradientDrawable gradientDrawable=new GradientDrawable(); 
    gradientDrawable.setStroke(4,getResources().getColor(R.color.line_Input)); 

Sau đó thiết lập nền tảng của bố cục như:

LinearLayout layout = (LinearLayout) findViewById(R.id.ayout); layout .setBackground(gradientDrawable); 
1

Tôi sẽ thêm tài liệu Android liên kết đến khác câu trả lời.

https://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

Nó mô tả tất cả các thuộc tính của Shape drawable và stroke trong số đó để thiết lập biên giới.

Ví dụ:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <stroke android:width="1dp" android:color="#F00"/> 
    <solid android:color="#0000"/> 
</shape> 

Red biên giới với nền trong suốt.

Các vấn đề liên quan