2014-10-28 134 views
0

如何將圖像放在ListView按鈕上?我發現了一些教程,但不知道如何在我的XML中實現,我已經。我仍然只使用xml文件,並且沒有觸及生成的MainActivity.java的代碼中的任何內容。將圖像添加到列表視圖中的按鈕

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
} 

這是當前XML

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

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.5" > 

    <ImageView 
     android:contentDescription="@string/food" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentBottom="true" 
     android:scaleType="centerCrop" 
     android:src="@drawable/food" /> 
</RelativeLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.5" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="35dp" 
     android:padding="4dp" 
     android:layout_marginTop="20dp" 
     android:gravity="left" 
     android:background="@drawable/bg_button" 
     android:text="@string/Menu1" /> 
    <!-- android:background="#c5e1b0" --> 
    <Button 
     android:id="@+id/button2" 
     android:layout_width="match_parent" 
     android:layout_height="35dp" 
     android:padding="4dp" 
     android:layout_marginTop="5dp" 

     android:gravity="left" 
     android:background="@drawable/bg_button" 
     android:text="@string/Menu2" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="match_parent" 
     android:layout_height="35dp" 
     android:padding="4dp" 
     android:layout_marginTop="5dp" 

     android:gravity="left" 
     android:background="@drawable/bg_button" 
     android:text="@string/Menu3" /> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="match_parent" 
     android:layout_height="35dp" 
     android:padding="4dp" 
     android:layout_marginTop="5dp" 

     android:gravity="left" 
     android:background="@drawable/bg_button" 
     android:text="@string/Menu4" /> 
</LinearLayout> 

</LinearLayout> 

所以我想在這4個按鈕的左側添加圖像。

任何人都可以幫助完成此任務嗎? 更新:

目前我有這種結構在左邊。屏幕的一半(上)是圖像。下半場我有4個按鈕。我想把圖像放在左邊的每個按鈕上,就像是在右邊的圖片中,紅色的方形在哪裏。 enter image description here

+0

可能重複http://stackoverflow.com/questions/12492064/android-文本中均添加圖像到按鈕) – 2014-10-28 10:27:28

+0

你可以繪製並上傳你的清單項目的手繪,所以我可以幫你 – 2014-10-28 10:30:53

+0

正如@ChVKishore所說,你的每件物品在清單中的樣子是怎樣的? – 2014-10-28 10:34:42

回答

0

這裏是你怎麼能有圖像,並在同一個按鈕

<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Testing" android:drawableLeft="@drawable/logo"/> 
的[Android的添加圖像按鈕(
+0

感謝您的幫助 – 2014-10-28 10:55:29

+0

不客氣,快樂編碼:) – 2014-10-28 10:58:16