先上图,看一下效果
更改位置位于blocklylib-core/src/main/java/com/google/blockly/android/ui
CategoryTabs.java文件中的onCreateLabel()和onBindLabel()方法
/** Manages TextView labels derived from {@link R.layout#default_toolbox_tab}. */
protected class DefaultTabsAdapter extends CategoryTabs.LabelAdapter {
@Override
public View onCreateLabel() {
return LayoutInflater.from(getContext())
.inflate(R.layout.test_toolbox_imgtext, null);
}
/**
* Assigns the category name to the {@link TextView}. Tabs without labels will be assigned
* the text {@link R.string#blockly_toolbox_default_category_name} ("Blocks" in English).
*
* @param labelView The view used as the label.
* @param category The {@link BlocklyCategory}.
* @param position The ordering position of the tab.
*/
@Override
public void onBindLabel(View labelView, BlocklyCategory category, int position) {
String labelText = category.getCategoryName();
if (TextUtils.isEmpty(labelText)) {
labelText = getContext().getString(R.string.blockly_toolbox_default_category_name);
}
ImageView imageView = labelView.findViewById(R.id.robot_img);
TextView textView = labelView.findViewById(R.id.robot_text);
//((TextView) labelView).setText(labelText);
switch (position){
case 0:
imageView.setImageResource(R.drawable.apple);
textView.setText(R.string.apple);
break;
case 1:
imageView.setImageResource(R.drawable.watermelon);
textView.setText(R.string.watermelon);
break;
case 2:
imageView.setImageResource(R.drawable.strawberry);
textView.setText(R.string.strawberry);
break;
case 3:
imageView.setImageResource(R.drawable.lemur);
textView.setText(R.string.lemur);
break;
case 4:
imageView.setImageResource(R.drawable.tiger);
textView.setText(R.string.tiger);
break;
case 5:
imageView.setImageResource(R.drawable.zebra);
textView.setText(R.string.zebra);
break;
default:
break;
}
}
}
需要注意的是ToolBox侧边菜单栏图片方向的更改位置在布局文件中的
blockly:labelRotation属性,
<fragment android:name="com.google.blockly.android.CategorySelectorFragment"
android:id="@id/blockly_categories"
android:layout_below="@+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
blockly:labelRotation="none"
blockly:scrollOrientation="vertical"
tools:ignore="MissingPrefix"/>
blockly:labelRotation的属性值会影响onCreateLabel布局中控件的宽高的设置,为了简单,横屏应用可以直接设置为none,
具体影响的代码为blocklylib-core目录下的RotatedViewGroup.java文件
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int horzPadding = getPaddingLeft() + getPaddingRight();
int vertPadding = getPaddingTop() + getPaddingBottom();
int childWidthMSpec, childHeightMSpec;
if (isChildRotated()) {
// Swap both measure specs, and subtracted paddings.
childWidthMSpec = MeasureSpec.makeMeasureSpec(
Math.max(MeasureSpec.getSize(heightMeasureSpec) - vertPadding, 0),
MeasureSpec.getMode(heightMeasureSpec));
childHeightMSpec = MeasureSpec.makeMeasureSpec(
Math.max(MeasureSpec.getSize(widthMeasureSpec) - horzPadding, 0),
MeasureSpec.getMode(widthMeasureSpec));
} else {
// Subtract the paddings from measure specs.
childWidthMSpec = MeasureSpec.makeMeasureSpec(
Math.max(MeasureSpec.getSize(widthMeasureSpec) - horzPadding, 0),
MeasureSpec.getMode(widthMeasureSpec));
childHeightMSpec = MeasureSpec.makeMeasureSpec(
Math.max(MeasureSpec.getSize(heightMeasureSpec) - vertPadding, 0),
MeasureSpec.getMode(heightMeasureSpec));
}
int maxChildWidth = 0, maxChildHeight = 0;
int childCount = getChildCount();
for (int i = 0; i < childCount; ++i) {
View child = getChildAt(i);
child.measure(childWidthMSpec, childHeightMSpec);
maxChildWidth = Math.max(maxChildWidth, child.getMeasuredWidth());
maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
}
if (isChildRotated()) {
setMeasuredDimension(maxChildHeight + horzPadding, maxChildWidth + vertPadding);
} else {
setMeasuredDimension(maxChildWidth + horzPadding, maxChildHeight + vertPadding);
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
if (changed || mRotationChanged) {
int width = right - left;
int height = bottom - top;
switch (Rotation.normalize(mRotation, this)) {
default:
case Rotation.NONE:
mChildLayoutRect.set(getPaddingLeft(), getPaddingTop(),
width - getPaddingRight(), height - getPaddingBottom());
mEventTransformMatrix.reset();
mDrawTransformMatrix.reset();
break;
case Rotation.CLOCKWISE:
mChildLayoutRect.set(getPaddingTop(), getPaddingRight(),
height - getPaddingBottom(), width - getPaddingLeft());
mEventTransformMatrix.setRotate(-90);
mEventTransformMatrix.postTranslate(0, width);
mEventTransformMatrix.invert(mDrawTransformMatrix);
break;
case Rotation.COUNTER_CLOCKWISE:
mChildLayoutRect.set(getPaddingBottom(), getPaddingLeft(),
height - getPaddingTop(), width - getPaddingRight());
mEventTransformMatrix.setRotate(90);
mEventTransformMatrix.postTranslate(height, 0);
mEventTransformMatrix.invert(mDrawTransformMatrix);
break;
}
mRotationChanged = true;
}
int childCount = getChildCount();
for (int i = 0; i < childCount; ++i) {
View child = getChildAt(i);
child.layout(mChildLayoutRect.left, mChildLayoutRect.top, mChildLayoutRect.right,
mChildLayoutRect.bottom);
}
}
可以看到其中的onMesure()和onLayout()方法会根据旋转方向进行测量和摆放.