2013-10-14 15 views
39

GridLayout'da, TextView kodunun altına çizilebilir bir alt yapı oluşturuyorum.Android TextView çekilebilir, çekilebilir ve metin arasındaki dolgu değiştirmek?

Çizgiyi TextView'un ortasına getirmek istiyorum; setCompoundDrawablePadding(-75) ile denedim ve yalnızca metnin konumunu değiştirir.

Güncel kodu: Ben TextView ortasına metin ve çekilebilir ayarlayabilirsiniz nasıl

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); 

?

cevap

86

İstenilen sonucu elde etmek için drawablePadding ve padding'u birleştirmeniz gerekir.

+0

Teşekkür ederiz! Yardımcı olur. – user1648374

+0

Farklı metinler için (örneğin farklı bölgeler) nasıl birleştirirsiniz? – orium

0

Katman listesini kullanabilirsiniz.

önce:

enter image description here

şekil için xml oluşturma - 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> 

sonra: xml olarak

enter image description here

,gibi bir şey yoktur. Ayrıca top, left, bottom'u da ekleyebilirsiniz. Bu şekilde örneğin verebilirsiniz: textView toplam yüksekliğini yeniden boyutlandırmak için sol ve sağ doldurulabilir. XML'de

4

:

robot: drawablePadding = paddingValue Programlı

veya

:

TextView'un txt;

txt.setCompoundDrawablePadding (paddingValue)

android: drawablePadding çekilebilir simgesine doldurma vermek için en kolay yoldur, ancak paddingRight veya çekilebilir simgesinin paddingLeft gibi özel bir tarafı dolgu veremez. Bu, çekilebilirliğin her iki tarafına dolgu sağlar.

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`