2011-06-28 35 views
13

Với tôi có thể vẽ nền để tạo ra bulletpoints cho TextView như thế này:<size> thuộc tính không hữu ích khi sử dụng danh sách lớp?

Example of what I want

Sau đó, mã XML của tôi trông như thế này:

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

<layer-list 
     xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:right="235dp"> 
     <shape android:shape="oval"> 
      <padding android:left="10dp" /> 
      <size android:height="5dp" android:width="5dp"/> 
      <solid android:color="@color/my_pink"/> 
     </shape> 
    </item> 
    <item android:left="10dp"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#ffffff"/> 
      <padding android:left="10dp" /> 
     </shape> 
    </item> 
</layer-list> 

Nhưng một khi tôi sử dụng mã hiển thị ở trên, tôi điểm bullet trông như thế này:

Result of the XML shown above

Có vẻ như thẻ <size> bị bỏ qua hoàn toàn.

Bạn giải quyết vấn đề này như thế nào? Sử dụng một 9patch, có tôi biết .. có lẽ đó là dễ nhất để làm .. nhưng trong thực tế, tôi đã hy vọng sẽ tìm thấy một giải pháp XML vì nó linh hoạt hơn trong tương lai.

Bản vẽ tùy chỉnh cũng nằm ngoài câu hỏi.

+1

Nhìn câu trả lời của tôi http://stackoverflow.com/questions/26841517/layer-list-ignore-size-tag/ – offset

Trả lời

6

<size> thẻ chắc chắn làm việc trong layer-list và để hiển thị các điểm đạn cho TextView bạn có thể sử dụng xml thuộc tính android:drawableLeft ->link

Với phương pháp này, không có nhu cầu 9-patch và bản vẽ tùy chỉnh.

Mã bưu điện & ảnh chụp màn hình tại đây để tham khảo.

res/drawable/bullet_point_layer.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:right="3dp"> 
    <shape android:shape="oval"> 
     <padding android:left="10dp" /> 
     <size android:height="10dp" android:width="10dp"/> 
     <solid android:color="#ff0080"/> 
    </shape> 
</item> 

res/layout/main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView one" 
    android:textColor="@android:color/black" 
    android:drawableLeft="@drawable/bullet_point_layer" 
    android:background="@android:color/white" /> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView Two" 
    android:textColor="@android:color/black" 
    android:drawableLeft="@drawable/bullet_point_layer" 
    android:background="@android:color/white" /> 

</LinearLayout> 

Làm thế nào nó trông

+0

Đó chắc chắn là giải pháp tốt. Đã không nghĩ về drawableLeft trước đây. Cảm ơn bạn! –

+0

@Vijay C: thẻ đang hoạt động hay không trong danh sách lớp ?? bạn nói với " thẻ chắc chắn hoạt động trong danh sách lớp", tôi cũng có vấn đề trong RatingsBar như điểm bullet này, ngôi sao bị kéo dài theo chiều dọc, có cách nào để thiết lập chiều cao để wrap_content ?? – Jayesh

+0

@Jayesh bạn đã sử dụng drawableLeft chưa? –

5

Đây là một bài đăng cũ nhưng tôi nghĩ rằng tôi sẽ thêm một cái gì đó rất quan trọng mà là mất tích trên bài đăng này.

Từ kinh nghiệm của tôi, mục cuối cùng của <layer-list> sẽ là mục có giá trị kích thước được lấy từ đó.

Vì vậy, trong ví dụ này tôi tạo một đường chia hai dòng. Tôi chỉ định một mục trống ở cuối để chỉ định chiều cao có thể chia ngăn.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item> 
     <shape android:shape="rectangle"> 
      <solid android:color="#535353" /> 
     </shape> 
    </item> 

    <item android:top="@dimen/common_divider_line_half_size"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#737373" /> 
     </shape> 
    </item> 

    <!-- Last item empty to specify divider height --> 
    <item> 
     <shape android:shape="rectangle"> 
      <size android:height="@dimen/common_divider_line_size"/> 
      <solid android:color="@android:color/transparent" /> 
     </shape> 
    </item> 
</layer-list> 
+1

Rất thú vị. –

+0

Đặt ImageView scaleType thành fitXY, theo cách này bạn không phải đặt thứ nguyên. Sau đây sẽ hoạt động: Johan

+0

No. Dưới Android 21, thẻ kích thước hoặc thuộc tính android: width/height được hệ thống bỏ qua. – DYS

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