2017-02-21 119 views
1

我已經創建了六個按鈕與圖像和文本......我只是希望這些圖像和文字apears在按鈕的中心注意到文字只是像下,這...是我的XML代碼......如果任何人能解決這個只寫XML代碼樓下,對不起,我的英語不好,我希望這會是無可厚非如何設置圖像和文本在android中的按鈕的中心

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:weightSum="3" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:weightSum="2" > 

      <Button 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:drawableTop="@drawable/list" 
       android:text="Order Station" 
       android:textStyle="bold"/> 

      <Button 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:text="2" /> 
     </LinearLayout> 


     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:weightSum="2" > 

      <Button 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:drawableTop="@drawable/power" 
       android:background="#ff4545" 
       android:text="Quick Order" 
       android:textStyle="bold" /> 

      <Button 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:text="4" 
       /> 
     </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:weightSum="2" > 
     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:drawableTop="@drawable/coffeecup" 
      android:text="Take Away" 
      android:textStyle="bold"/> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="4" /> 
    </LinearLayout> 
    </LinearLayout> 
</FrameLayout> 
+0

你想裁剪的圖像適合作爲背景的按鈕大小和文本仍然居中? –

回答

0

要麼你必須使用另一種佈局(線性或相對)並將drawableTop放置在TextView上,並通過給出onClickListener將該LinearLayout用作Button。

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="3" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:weightSum="2" > 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:layout_weight="1"> //give onclick to this layout to act as a button 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:drawableTop="@drawable/list" 
       android:gravity="center" 
       android:text="Order Station" 
       android:textStyle="bold"/> 

     </LinearLayout> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="2" /> 
    </LinearLayout> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:weightSum="2" > 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:background="#ff4545" 
      android:layout_weight="1"> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:drawableTop="@drawable/power" 
       android:gravity="center" 
       android:text="Quick Order" 
       android:textStyle="bold"/> 

     </LinearLayout> 
     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="4" 
      /> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:weightSum="2" > 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:layout_weight="1"> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:drawableTop="@drawable/coffeecup" 
       android:gravity="center" 
       android:text="Take Away" 
       android:textStyle="bold"/> 

     </LinearLayout> 
     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="4" /> 
    </LinearLayout> 
</LinearLayout> 

或設置高度按鈕WRAP_CONTENT的。

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:weightSum="2" > 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:drawableTop="@drawable/list" 
      android:text="Order Station" 
      android:textStyle="bold"/> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="2" /> 
    </LinearLayout> 
+0

非常感謝,但它是真正有幫助的,但我將此按鈕更改爲相對佈局,因爲我可以把圖像和文本視圖,因爲我需要 – 7arooney

+0

是的其實我的意思是添加另一個佈局 - 線性或相對。 –

0

如果你要裁剪的圖像,並保持居中你可以設置圖像是按鈕的背景的文本:android:background="@drawable/YOUR_IMAGE"

下面是它會是什麼樣子爲你的XML:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="3" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:weightSum="2" > 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="Order Station" 
      android:textStyle="bold" 
      android:background="@drawable/list" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="2" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:weightSum="2" > 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="Quick Order" 
      android:textStyle="bold" 
      android:background="@drawable/power" /> 


     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="4" 
      /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:weightSum="2" > 
     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="Take Away" 
      android:textStyle="bold" 
      android:background="@drawable/coffeecup"/> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="4" /> 
    </LinearLayout> 
</LinearLayout> 
</FrameLayout> 
+0

非常感謝,但它是真正有幫助的,但我更改此按鈕爲相對佈局,因爲我可以把圖像和文本視圖,因爲我需要 - 7arooney – 7arooney

相關問題