如何创建显示图像的按钮?

时间:2015-03-13 09:02:09

标签: android

我创建了三个按钮,每个按钮在单击时显示文本。现在我需要添加,以便按钮显示文本+图像。只有单击按钮,才会显示所有文本和图像。例如,当用户点击第一个按钮时,会出现text1 + image1。当用户单击第二个按钮时,text2 + image2出现在text1和image1的相同位置。有什么帮助吗?这是我的代码:

XML(我无法在此处发布第一个代码部分,但下面是最重要的部分)

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Recipe 1"
    android:id="@+id/click_btn"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/response"
    android:layout_below="@+id/click_btn"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/button15"
    android:layout_alignEnd="@+id/button15" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Recipe 2"
    android:id="@+id/button15"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Recipe 3"
    android:id="@+id/button16"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView4"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/response"
    android:layout_toEndOf="@+id/response" />

</RelativeLayout>

爪哇:

package com.example.android.ch;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import static com.example.android.R.*;


public class Recipes extends ActionBarActivity implements View.OnClickListener {
TextView resp; ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(layout.activity_recipes);
    resp = (TextView)this.findViewById(id.response);
    Button b = (Button)this.findViewById(id.click_btn);
    Button c = (Button)this.findViewById(id.button15);
    Button d = (Button)this.findViewById(id.button16);
    ImageView imageView = (ImageView) findViewById(id.imageView);
    b.setOnClickListener(this);
    c.setOnClickListener(this);
    d.setOnClickListener(this);

}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);


    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {



    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

    }
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_recipes, 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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {

    switch(v.getId()){

        case R.id.click_btn: /** on click_btn button click */
            resp.setText("2   medium sweet potatoes (about 1 pound total) \n1/2  teaspoon salt\n1/2  teaspoon ground cumin\n1/2  teaspoon chili powder\n1/2  teaspoon paprika\n1/4  teaspoon ground black pepper");
            break;

        case R.id.button15: /** on button15 button click */
            resp.setText("1 large egg yolk, at room temperature\n1/2 cup extra-virgin olive oil; more for grilling\n2 teaspoons minced fresh flat-leaf parsley\n1 teaspoon minced fresh tarragon\n1 1/2 teaspoons fresh lemon juice; more to taste Kosher salt");
            break;

        case R.id.button16: /** on button16 button click */
            resp.setText("1/2 cup butter, softened\n1 cup packed light brown sugar\n1 1/2 cups all-purpose flour\n3 cups rolled oats\n1 teaspoon ground cinnamon");
            break;
    }
}

}

3 个答案:

答案 0 :(得分:1)

是的,您可以将图像或图标添加到带有文本的按钮。 使用下面的代码 -

   <Button
     android:drawableRight="@drawable/ic_logout"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Recipe 3"
     android:id="@+id/button16"
     android:layout_alignParentTop="true"
     android:layout_alignParentRight="true"
     android:layout_alignParentEnd="true" />

这里我添加了一个属性 android:drawableRight 你可以将它改为 android:drawableLeft android:drawableTop ,或者 android:drawableBottom 并在按钮上设置图像。

答案 1 :(得分:1)

使用drawableLeft(或任何方向)属性显示带有文本的图像

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    android:drawableLeft="@drawable/button_icon"
    ... />

上面的代码会显示enter image description here

之类的图片

答案 2 :(得分:0)

在布局中包含imageview和textview,并将其可见性保持为不可见/消失。然后单击onclick侦听器方法内的按钮(例如button1),您需要尝试

imageview.setimage("your image");
textview.setText("your text");

使用各种值更改每个buttonclick侦听器中的此代码。

相关问题