2013-10-14 13 views
39

Tôi đang tạo một TextView với mặt dưới có thể kéo được, trong một GridLayout.Android TextView có thể drawable, thay đổi đệm giữa drawable và text?

Tôi muốn mang bản vẽ đến giữa TextView; Tôi đã thử với setCompoundDrawablePadding(-75) và nó chỉ thay đổi vị trí của văn bản.

đang hiện tại:

TextView secondItem = new TextView(this); 
    GridLayout.LayoutParams second = new GridLayout.LayoutParams(row2, col1); 
    second.width = halfOfScreenWidth; 
    second.height = (int) (quarterScreenWidth * 1.5); 
    secondItem.setLayoutParams(second); 
    secondItem.setBackgroundResource(R.color.MenuSecond); 
    secondItem.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, R.drawable.ic_action_new); 
    secondItem.setText("Text"); 
    secondItem.setCompoundDrawablePadding(-180); 
    secondItem.setGravity(Gravity.CENTER); 
    secondItem.setTextAppearance(this, android.R.style.TextAppearance_Large_Inverse); 
    gridLayout.addView(secondItem, second); 

Làm thế nào tôi có thể thiết lập các văn bản và drawable đến giữa TextView?

Trả lời

86

Bạn cần kết hợp drawablePaddingpadding để nhận kết quả mong muốn.

+0

Cảm ơn bạn! Nó giúp. – user1648374

+0

bạn sẽ kết hợp nó như thế nào cho các văn bản khác nhau (ví dụ các ngôn ngữ khác nhau)? – orium

0

Bạn có thể sử dụng danh sách lớp.

Trước:

enter image description here

Tạo xml cho hình dạng - shape_rectangle.xml:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:right="5dp"> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 
     <solid android:color="@color/my_color" /> 
     <size 
      android:width="5dp" 
      android:height="5dp" /> 
    </shape> 
</item> 
</layer-list> 

Sau:

enter image description here

Trong xml, có cái gì đó giống như android:right. Bạn cũng có thể thêm top, left, bottom. Bằng cách này, bạn có thể đưa ra ví dụ: padding trái và phải để drawable mà không thay đổi kích thước tổng chiều cao của textView.

4

Trong XML:

android: drawablePadding = paddingValue

hoặc

lập trình:

TextView txt;

txt.setCompoundDrawablePadding (paddingValue)

android: drawablePadding là cách dễ nhất để cung cấp cho padding để biểu tượng drawable nhưng bạn không thể đưa ra cụ thể đệm một bên như paddingRight hoặc paddingLeft của biểu tượng drawable. Điều này cho phép padding ở hai bên của drawable.

0
<Button 
      android:id="@+id/otherapps" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@drawable/btn_background" 
      android:text="@string/other_apps" 
      android:layout_weight="1" 
      android:fontFamily="cursive" 
      android:layout_marginBottom="10dp" 
      android:drawableLeft="@drawable/ic_more" 
      android:paddingLeft="8dp" //for drawable padding to the left 
      android:textColor="@android:color/holo_red_light" />`enter code here` 
Các vấn đề liên quan