0

ゲームのメニューをプログラムしようとしていますが、レベルを選択したいと思っています。行ごとに 5 つのボタンを含む 3 つの行が必要です。

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

コードを使用して 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:layout_below="@+id/viewId" や android:layout_toRightOf="@+id/viewId" などの XML 属性を使用できます。それ以外の場合は、RelativeLayout が必要ない場合は速度が向上するはずの LinearLayout を使用できます。

于 2012-05-12T22:48:26.923 に答える
0

addRule()function forを使用する代わりに、単純な xml ファイルを使用していたでしょうRelativeLayout。構造全体RelativeLayoutを Java ファイルで作成するのは面倒で、多くのコーディングが必要になります。これは、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 に答える