Add the FixedGridLayout sample code.
This time with an Android.mk P.S. Hi from Google I/O!
This commit is contained in:
12
samples/FixedGridLayout/Android.mk
Normal file
12
samples/FixedGridLayout/Android.mk
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
LOCAL_PATH:= $(call my-dir)
|
||||||
|
include $(CLEAR_VARS)
|
||||||
|
|
||||||
|
LOCAL_MODULE_TAGS := samples
|
||||||
|
|
||||||
|
LOCAL_SRC_FILES := $(call all-subdir-java-files)
|
||||||
|
|
||||||
|
LOCAL_PACKAGE_NAME := FixedGridLayout
|
||||||
|
|
||||||
|
LOCAL_SDK_VERSION := current
|
||||||
|
|
||||||
|
include $(BUILD_PACKAGE)
|
||||||
15
samples/FixedGridLayout/AndroidManifest.xml
Normal file
15
samples/FixedGridLayout/AndroidManifest.xml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
package="com.example.android.fixedgridlayout"
|
||||||
|
android:versionCode="1"
|
||||||
|
android:versionName="1.0">
|
||||||
|
<application android:label="@string/app_name">
|
||||||
|
<activity android:name=".FixedGridLayoutTest"
|
||||||
|
android:label="@string/app_name">
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
|
</intent-filter>
|
||||||
|
</activity>
|
||||||
|
</application>
|
||||||
|
</manifest>
|
||||||
BIN
samples/FixedGridLayout/res/drawable/bugdroid.png
Normal file
BIN
samples/FixedGridLayout/res/drawable/bugdroid.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.9 KiB |
42
samples/FixedGridLayout/res/layout/main.xml
Normal file
42
samples/FixedGridLayout/res/layout/main.xml
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<com.example.android.fixedgridlayout.FixedGridLayout
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res/com.example.android.fixedgridlayout"
|
||||||
|
android:id="@+id/grid"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="fill_parent"
|
||||||
|
app:cellWidth="80dp"
|
||||||
|
app:cellHeight="100dp"
|
||||||
|
>
|
||||||
|
<ImageView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:src="@drawable/bugdroid"
|
||||||
|
/>
|
||||||
|
<ImageView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:src="@drawable/bugdroid"
|
||||||
|
/>
|
||||||
|
<ImageView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:src="@drawable/bugdroid"
|
||||||
|
/>
|
||||||
|
<ImageView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:src="@drawable/bugdroid"
|
||||||
|
/>
|
||||||
|
<ImageView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:src="@drawable/bugdroid"
|
||||||
|
/>
|
||||||
|
<ImageView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:src="@drawable/bugdroid"
|
||||||
|
/>
|
||||||
|
</com.example.android.fixedgridlayout.FixedGridLayout>
|
||||||
|
|
||||||
7
samples/FixedGridLayout/res/values/attrs.xml
Normal file
7
samples/FixedGridLayout/res/values/attrs.xml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<resources>
|
||||||
|
<declare-styleable name="FixedGridLayout">
|
||||||
|
<attr name="cellWidth" format="dimension" />
|
||||||
|
<attr name="cellHeight" format="dimension" />
|
||||||
|
</declare-styleable>
|
||||||
|
</resources>
|
||||||
|
|
||||||
4
samples/FixedGridLayout/res/values/strings.xml
Normal file
4
samples/FixedGridLayout/res/values/strings.xml
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<resources>
|
||||||
|
<string name="app_name">FixedGridLayoutTest</string>
|
||||||
|
</resources>
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2009 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.fixedgridlayout;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.res.TypedArray;
|
||||||
|
import android.util.AttributeSet;
|
||||||
|
import android.util.SparseIntArray;
|
||||||
|
import android.view.Gravity;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.view.ViewDebug;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A layout that arranges its children in a grid. The size of the
|
||||||
|
* cells is set by the {@link #setCellSize} method and the
|
||||||
|
* android:cell_width and android:cell_height attributes in XML.
|
||||||
|
* The number of rows and columns is determined at runtime. Each
|
||||||
|
* cell contains exactly one view, and they flow in the natural
|
||||||
|
* child order (the order in which they were added, or the index
|
||||||
|
* in {@link #addViewAt}. Views can not span multiple cells.
|
||||||
|
*/
|
||||||
|
public class FixedGridLayout extends ViewGroup {
|
||||||
|
int mCellWidth;
|
||||||
|
int mCellHeight;
|
||||||
|
|
||||||
|
public FixedGridLayout(Context context) {
|
||||||
|
super(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
public FixedGridLayout(Context context, AttributeSet attrs) {
|
||||||
|
super(context, attrs);
|
||||||
|
|
||||||
|
// Read the resource attributes.
|
||||||
|
TypedArray a = context.obtainStyledAttributes(
|
||||||
|
attrs, R.styleable.FixedGridLayout);
|
||||||
|
mCellWidth = a.getDimensionPixelSize(
|
||||||
|
R.styleable.FixedGridLayout_cellWidth, -1);
|
||||||
|
mCellHeight = a.getDimensionPixelSize(
|
||||||
|
R.styleable.FixedGridLayout_cellHeight, -1);
|
||||||
|
a.recycle();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCellWidth(int px) {
|
||||||
|
mCellWidth = px;
|
||||||
|
requestLayout();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCellHeight(int px) {
|
||||||
|
mCellHeight = px;
|
||||||
|
requestLayout();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||||
|
int cellWidthSpec = MeasureSpec.makeMeasureSpec(mCellWidth,
|
||||||
|
MeasureSpec.AT_MOST);
|
||||||
|
int cellHeightSpec = MeasureSpec.makeMeasureSpec(mCellHeight,
|
||||||
|
MeasureSpec.AT_MOST);
|
||||||
|
|
||||||
|
int count = getChildCount();
|
||||||
|
for (int index=0; index<count; index++) {
|
||||||
|
final View child = getChildAt(index);
|
||||||
|
child.measure(cellWidthSpec, cellHeightSpec);
|
||||||
|
}
|
||||||
|
// Use the size our parents gave us
|
||||||
|
setMeasuredDimension(resolveSize(mCellWidth*count, widthMeasureSpec),
|
||||||
|
resolveSize(mCellHeight*count, heightMeasureSpec));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onLayout(boolean changed, int l, int t, int r, int b) {
|
||||||
|
int cellWidth = mCellWidth;
|
||||||
|
int cellHeight = mCellHeight;
|
||||||
|
int columns = (r - l) / cellWidth;
|
||||||
|
if (columns < 0) {
|
||||||
|
columns = 1;
|
||||||
|
}
|
||||||
|
int x = 0;
|
||||||
|
int y = 0;
|
||||||
|
int i = 0;
|
||||||
|
int count = getChildCount();
|
||||||
|
for (int index=0; index<count; index++) {
|
||||||
|
final View child = getChildAt(index);
|
||||||
|
|
||||||
|
int w = child.getMeasuredWidth();
|
||||||
|
int h = child.getMeasuredHeight();
|
||||||
|
|
||||||
|
int left = x + ((cellWidth-w)/2);
|
||||||
|
int top = y + ((cellHeight-h)/2);
|
||||||
|
|
||||||
|
child.layout(left, top, left+w, top+h);
|
||||||
|
if (i >= (columns-1)) {
|
||||||
|
// advance to next row
|
||||||
|
i = 0;
|
||||||
|
x = 0;
|
||||||
|
y += cellHeight;
|
||||||
|
} else {
|
||||||
|
i++;
|
||||||
|
x += cellWidth;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.example.android.fixedgridlayout;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.os.Bundle;
|
||||||
|
|
||||||
|
public class FixedGridLayoutTest extends Activity
|
||||||
|
{
|
||||||
|
/** Called when the activity is first created. */
|
||||||
|
@Override
|
||||||
|
public void onCreate(Bundle savedInstanceState)
|
||||||
|
{
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
setContentView(R.layout.main);
|
||||||
|
|
||||||
|
FixedGridLayout grid = (FixedGridLayout)findViewById(R.id.grid);
|
||||||
|
grid.setCellWidth(80);
|
||||||
|
grid.setCellHeight(80);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user