0

我在我的Android项目中创建了一个形状,我想在左上角添加一个按钮。

这是我的形状:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/transparent" />
    <stroke android:width="1dp" android:color="#000000" />
    <padding android:left="2dp" android:top="1dp" android:right="2dp"
        android:bottom="1dp" />
</shape>

我添加了一个屏幕截图以进行更多说明:

在此处输入图像描述

我想要我的矩形左上角的十字架,但正如你所看到的那样,实际上并不是这样:(。

在此先感谢您的帮助。

4

1 回答 1

0

After a Long Discussion with you, I just add a Shape Right and Below to crossImageButton in Relativelayout as a parent.

Here i have shared the code, Do the needed modification as your requirement

        RelativeLayout parent = (RelativeLayout)findViewById(R.id.parent);
        ImageButton ib = new ImageButton(getApplicationContext());
        ib.setId(1);
        ib.setBackgroundResource(R.drawable.cross_button);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        ib.setLayoutParams(lp);
        parent.addView(ib);

        ImageView iv = new ImageView(getApplicationContext()); 
        iv.setBackgroundResource(R.drawable.myshape);
        iv.setImageResource(R.drawable.ic_launcher);
        RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(50,50);
        lp1.addRule(RelativeLayout.RIGHT_OF, ib.getId());
        lp1.addRule(RelativeLayout.BELOW, ib.getId());
        iv.setLayoutParams(lp1);
        parent.addView(iv); 
于 2013-11-14T14:57:13.220 回答