0

我正在尝试为游戏编写菜单,现在想要进行关卡选择。它应该有三行,每行有 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 layout = new RelativeLayout(this);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
    layout.setLayoutParams(params);

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

4

3 回答 3

0

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

RelativeLayout relativeLayout=(RelativeLayout) View.inflate(R.layout.your_xml_here);
于 2012-05-12T22:08:20.893 回答
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);    //<<---

这是你需要的吗?

于 2012-05-13T04:41:38.497 回答
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 的目的是指定视图的位置应相对于另一个视图的位置,因此您可以使用 XML 属性,如 android:layout_below="@+id/viewId" 和 android:layout_toRightOf="@+id/viewId"。否则,您可以只使用 LinearLayout,如果不需要 RelativeLayout,这应该会提高速度。

于 2012-05-12T22:48:26.923 回答