0

我想在图像视图中的图像上显示按钮。任何人都可以知道如何做到这一点。我是android编程的新手。

谢谢。

4

2 回答 2

1

通过使用这个你可以动态地在位图上创建按钮......

public class MainAct extends GrapActivity implements OnClickListener {


private Button saveButton;
private Button clearButton;

// onCreate Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);          

 // content view 
signature = new MyView(this); 

// Dynamically created button on bitmap & canvas

RelativeLayout myLayout = new RelativeLayout(this);        
myLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

// For Save Button
saveButton = new Button(this); 
saveButton.setText("Save"); 
saveButton.setOnClickListener(this); 

// For Clear Button
clearButton = new Button(this); 
clearButton.setText("Clear"); 
clearButton.setOnClickListener(this); 

myLayout.addView(signature); 
myLayout.addView(saveButton); 
myLayout.addView(clearButton); 

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT); 
//Alignments
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
saveButton.setLayoutParams(params); 

//Alignments
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT); 
params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
clearButton.setLayoutParams(params2); 

saveButton.bringToFront(); 
clearButton.bringToFront(); 
this.setContentView(myLayout); 
new Thread(new RefreshRunner()).start(); 

 // onclick listner for CLEAR button
 clearButton.setOnClickListener(new OnClickListener() {          
    public void onClick(View v) {
        //Activity for Clearing the Screen
        startActivity(new Intent(ThisAct.this, ThisAct.class));
        finish();
    }
});

// onclick listner for SAVE button
    saveButton.setOnClickListener(new OnClickListener() {  
        public void onClick(View v) { 
            //capture the image  
            try {                    
                saveAsJpg(mBitmap);     
                startActivity(new Intent(ThisAct.this, MainActivity.class));
                finish();
            } catch (IOException e) {                   
                e.printStackTrace();
            } 
        }  
    });     
 } 

}
于 2012-09-04T18:17:47.473 回答
1

使用布局容器,例如FrameLayoutRelativeLayout来包含ButtonandImageView并为它们提供适当的属性,以便它们根据需要重叠。

于 2012-09-04T18:18:30.813 回答