2013-03-20 43 views
30

Tôi muốn tạo bố cục với các góc được làm tròn và nền màu được lấp đầy.Thêm nền màu và bán kính đường viền vào Bố cục

Đây là cách bố trí của tôi:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="210dp" 
    android:orientation="vertical" 
    android:layout_marginBottom="10dp"   
    android:background="@drawable/offerItemLayout"> 
    <LinearLayout 
     android:id="@+id/offerImageHolder" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
      . 
      . 
      . 
    </LinearLayout> 
</LinearLayout> 

Tôi có xml drawable sau (offerItemLayout) tạo ra biên giới một cách chính xác:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape xmlns:android="http://schemas.android.com/apk/res/android" 
       android:shape="rectangle"> 
      <corners 
       android:radius="5dp"/> 
      <stroke 
       android:width="1dp" 
       android:color="@color/bggrey" />    
     </shape> 
    </item> 
    // The lines below causes an inflation failure. 
    <item> 
     <fill 
      android:color="@color/white"/> 
    </item> 
</layer-list> 

Nhưng chèn mục với một điền gây ra lạm phát bố cục không thành công.

Ngoài ra, nếu tôi gán nền màu cho LinearLayout bên trong (offerImageHolder), nó sẽ ghi đè nền đầu tiên với các góc tròn của tôi.

Bất kỳ suy nghĩ nào về việc này đúng cách? :/

Trả lời

69

Bạn không cần mục điền riêng. Trong thực tế, nó không hợp lệ. Bạn chỉ cần thêm một khối solid vào shape. Các tiếp theo stroke dựa trên cùng của solid:

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

    <corners android:radius="5dp" /> 
    <solid android:color="@android:color/white" /> 
    <stroke 
     android:width="1dip" 
     android:color="@color/bggrey" /> 
</shape> 

Bạn cũng không cần layer-list nếu bạn chỉ có một shape.

+1

Nếu biên giới tôi muốn chỉ ở dưới cùng thì sao? – Si8

+0

@ SiKni8 có thể thực hiện được, nhưng đó là một câu hỏi khác và tôi thực sự không thể đăng nó ở đây. nếu bạn hỏi một câu hỏi mới và thông báo cho tôi, tôi sẽ đăng câu trả lời. –

+0

Tôi muốn thêm đầu viền màu của thẻ ... – Si8

11

background.xml trong thư mục có thể kéo.

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FFFFFF"/>  
<stroke android:width="3dp" 
     android:color="#0FECFF" /> 
<gradient       //specify graident 
    android:startColor="#ffffffff" 
    android:endColor="#110000FF" 
    android:angle="90"/> 

<padding android:left="5dp" 
     android:top="5dp" 
     android:right="5dp" 
     android:bottom="5dp"/> 
<corners android:bottomRightRadius="7dp" 
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" 
     android:topRightRadius="7dp"/> 
</shape> 

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="210dp" 
android:orientation="vertical" 
android:layout_marginBottom="10dp"   
android:background="@drawable/background"> 
Các vấn đề liên quan