【问题标题】:Android Wrapping ButtonsAndroid 包装按钮
【发布时间】:2012-03-11 09:54:15
【问题描述】:

我完全震惊,我找不到任何关于如何做到这一点的信息。如何将图像包装在布局中以仅下拉到下一行..并继续这样做,直到显示所有图像?我使用 80x80 图像作为按钮,当一行中可以容纳许多图像时,我希望它将它们包装到下一行并继续。当那些已经结束时,我希望它再次包装。

谁能告诉我如何构建这个布局?谢谢。

【问题讨论】:

  • 您使用的是什么 API? Honeycomb 3.2 中改变了这一技术
  • 我们不是代码编写服务;我们希望初学者在他们的问题上付出一些努力。一般来说,这里不接受没有代码的任务。请阅读:如何创建minimal reproducible exampleHow to Ask?

标签: android layout word-wrap


【解决方案1】:

我认为您可能正在寻找的是使用 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;
        }
    }

}

【讨论】:

  • 是的,这就是他真正想要的答案。 GridView是解决问题的android方式。
【解决方案2】:

在 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;

【讨论】:

    猜你喜欢
    • 2013-11-17
    • 2021-07-20
    • 1970-01-01
    • 2014-08-03
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多