2014-10-28 180 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