简体   繁体   中英

Adding images to buttons in listview

How to put images on ListView buttons? I found some tutorials but don't know how to implement in my xml that I have already. I still working only with xml files and didn't touch anything in the code of the generated 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);
}
}

This is the current 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>

So I want to add images on the left side of this 4 buttons.

Anyone can help with this task? Update:

Currently I have this structure on the left. Half of the screen (upper) is image. Second half I have 4 buttons. I want to put image on each button on the left side like is in the right picture with where is red aquare. 在此处输入图片说明

这是将图像和文本同时放在同一按钮上的方法

<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Testing" android:drawableLeft="@drawable/logo"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM