Android环绕按钮

时间:2012-02-21 03:13:29

标签: android layout wrapping

我完全震惊于我找不到任何关于如何做到这一点。如何在布局中包装图像以下拉到下一行..并继续这样做,直到所有图像都显示出来?我使用80x80图像作为按钮,但是当多行可以放在一行时,我希望它将它们包装到下一行并继续。当那些已经到了最后,我希望它再次包装。

有人可以告诉我如何构建这个布局吗?谢谢。

2 个答案:

答案 0 :(得分:2)

我认为您可能正在寻找的是使用ListAdapter从中获取数据的GridView。有关详细信息,请参阅http://developer.android.com/reference/android/widget/GridView.html。另请参阅[Android SDK目录] /samples/ApiDemos/src/com/example/android/apis/view/Grid1.java,以获取使用网格的简单启动器的演示。 (下面列出了完整的示例代码。)

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis.view;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import java.util.List;

//Need the following import to get access to the app resources, since this
//class is in a sub-package.
import com.example.android.apis.R;


public class Grid1 extends Activity {

    GridView mGrid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        loadApps(); // do this in onresume?

        setContentView(R.layout.grid_1);
        mGrid = (GridView) findViewById(R.id.myGrid);
        mGrid.setAdapter(new AppsAdapter());
    }

    private List<ResolveInfo> mApps;

    private void loadApps() {
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
    }

    public class AppsAdapter extends BaseAdapter {
        public AppsAdapter() {
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i;

            if (convertView == null) {
                i = new ImageView(Grid1.this);
                i.setScaleType(ImageView.ScaleType.FIT_CENTER);
                i.setLayoutParams(new GridView.LayoutParams(50, 50));
            } else {
                i = (ImageView) convertView;
            }

            ResolveInfo info = mApps.get(position);
            i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));

            return i;
        }


        public final int getCount() {
            return mApps.size();
        }

        public final Object getItem(int position) {
            return mApps.get(position);
        }

        public final long getItemId(int position) {
            return position;
        }
    }

}

答案 1 :(得分:0)

在API 12及更早版本中,您需要:

public static final int BUTTON_WIDTH = 80;
public static final int BUTTON_HEIGHT = 80;
public static final int SPACING = 8;

Display display = getWindowManager().getDefaultDisplay();
public int screenWidth = display.getWidth();
public int screenHeight = display.getHeight();    

//somewhere a drawButton(int y, int x) should be defined

for (int i = 0, i < screenHeight, i += (BUTTON_HEIGHT + SPACING)) {
    for (int j = 0, j < screenWidth, j += (BUTTON_WIDTH + SPACING)) {
        drawButton(i, j);
    }
}

在API 13及更高版本中,使用以下内容替换第5-7行:

Point screen = new Point();
getWindowManager().getDefaultDisplay().getSize(screen);

public int screenWidth = screen.x;
public int screenHeight = screen.y;