2014-03-26 78 views
1

我想要一個按鈕,其中有一個小圖標和按鈕內的文本,該圖標坐在按鈕的左側,我如何將它靠近以居中的文本?使圖像和文字在按鈕內並排顯示?

這是我的按鈕XML:

<Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:drawableLeft="@drawable/home_icon" 
     android:text="BACK" 
     android:id="@+id/button" 
     android:width="160dp" 
     android:height="70dp" 
     android:textStyle="bold" /> 

回答

1

不幸的是,這是不可能與drawableLeft屬性。該屬性將圖像定位在最左邊的位置(不管文字在哪裏)。

您需要創建自己的按鈕,這樣的:

<LinearLayout 
    android:id="@+id/button" 
    style="?android:attr/buttonStyle" 
    android:layout_width="160dip" 
    android:layout_height="70dip" 
    android:gravity="center" 
    android:orientation="horizontal" > 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/home_icon" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="BACK" 
     android:textColor="@android:color/black" /> 
</LinearLayout> 
0

你不能移動它更接近。你唯一的選擇,如果你想擁有它們更接近一起建設自己的按鈕:

<RelativeLayout 
      android:id="@+id/rlButton" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

    <TextView 
      android:id="@+id/tvText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/button_text"/> 

    <ImageView 
      android:id="@+id/ivLeft" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/home_icon" 
      android:layout_toLeftOf="@id/tvText"/> 

</RelativeLayout> 

你可以用保證金來控制TextViewImageView之間的距離。您可以使用它幾乎相同的按鈕:

RelativeLayour rlButton = (RelativeLayout) view.findViewById(R.id.rlButton); 
rlButton.setOnClickListener(new OnClickListener() { 
    .... 
}); 

但是,當然,你有你的按鈕樣式應用到RelativeLayout的,所以它看起來一樣的按鈕。

0
<Button 
android:id="@+id/button1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:drawableLeft="@android:drawable/btn_star_big_on" 
android:text="Drawable left"/>