在RelativeLayout中定位ImageButtons

时间:2012-05-12 21:41:28

标签: android

我正在尝试为游戏编写菜单,现在想要进行关卡选择。它应该有三行,每行有5个按钮。

for(int i=0; i<3; ++i){
        for(int j=0; j<5; ++j){
            ImageButton button = new ImageButton(this);

            RelativeLayout.LayoutParams paramsBut = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            paramsBut.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            paramsBut.addRule(RelativeLayout.ALIGN_PARENT_TOP);

            int marginLeft = (int)((float)(sizeLeftWidth/6) * (j+1)) + j*buttonSize;
            int marginTop = (int)((float)(sizeLeftHeight/4) * (i+1)) + i*buttonSize;
            paramsBut.leftMargin = marginLeft;
            paramsBut.topMargin = marginTop;

            button.setAdjustViewBounds(true);
            button.setMaxWidth(buttonSize);
            button.setMaxHeight(buttonSize);
            button.setBackgroundDrawable(null);

            Bitmap bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.level),buttonSize,buttonSize,false);
            Drawable d = new BitmapDrawable(bmp);
            button.setImageDrawable(d);

            layout.addView(button,paramsBut);
        }
    }

RelativeLayout的:

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

问题是按钮不在正确的位置,我认为问题在于边距。 我用边距做得对,还是整个代码完全愚蠢? (如果我得到提示如何改进我的编程风格,我总是很高兴^^)

3 个答案:

答案 0 :(得分:0)

我使用了一个简单的xml文件,而不是addRule()使用RelativeLayout函数。在java文件中创建整个RelativeLayout结构是一件痛苦的事,并且涉及大量编码,这在xml中更容易实现。您只需要为相同的布局创建一个xml并使用以下函数来获取RelativeLayout的对象(如果它是根布局):

RelativeLayout relativeLayout=(RelativeLayout) View.inflate(R.layout.your_xml_here);

答案 1 :(得分:0)

您不应该通过代码初始化您的UI,Android意味着您使用布局文件。例如,您可以使用以下XML代码在res / layout文件夹中创建名为“main.xml”的文件(注意ImageButton的“ID”是“myButton”)。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
    <ImageButton android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:marginLeft="10dp" android:marginRight="10dp" />
</RelativeLayout>

在您的活动的onCreate方法中,您可以致电:

setContentView(R.layout.main);

您将获得图像按钮的引用(ID以XML格式指定):

ImageButton btn = (ImageButton)findViewById(R.id.myButton);
btn.setBitmapImage(myImage); //you can use this
btn.setBitmapResouce(R.drawable.drawable_file); //or this

RelativeLayouts的目的也是指定视图相对于另一个视图的位置,所以你可以使用像android这样的XML属性:layout_below =“@ + id / viewId”和android:layout_toRightOf =“@ + id / viewId”。否则你可以使用LinearLayout,如果不需要RelativeLayout,它应该是速度增加。

答案 2 :(得分:0)

RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
layout.setLayoutParams(params);
layout.setGravity(Gravity.CENTER);    //<<---

这是你需要的吗?