新增 卡片布局及其示例

This commit is contained in:
hyb1996
2018-10-18 12:09:03 +08:00
parent 9e3b40e3c9
commit 270b2f7844
7 changed files with 218 additions and 37 deletions

View File

@@ -0,0 +1,42 @@
"ui";
ui.layout(
<vertical>
<appbar>
<toolbar id="toolbar" title="卡片布局"/>
</appbar>
<card w="*" h="70" margin="10 5" cardCornerRadius="2dp"
cardElevation="1dp" gravity="center_vertical">
<vertical padding="18 8" h="auto">
<text text="写操作系统作业" textColor="#222222" textSize="16sp"/>
<text text="明天第12节" textColor="#999999" textSize="14sp"/>
</vertical>
<View bg="#f44336" h="*" w="10"/>
</card>
<card w="*" h="70" margin="10 5" cardCornerRadius="2dp"
cardElevation="1dp" gravity="center_vertical">
<vertical padding="18 8" h="auto">
<text text="修复ui模式的Bug" textColor="#222222" textSize="16sp"/>
<text text="无限期" textColor="#999999" textSize="14sp"/>
</vertical>
<View bg="#ff5722" h="*" w="10"/>
</card>
<card w="*" h="70" margin="10 5" cardCornerRadius="2dp"
cardElevation="1dp" gravity="center_vertical">
<vertical padding="18 8" h="auto">
<text text="发布Auto.js 5.0.0正式版" textColor="#222222" textSize="16sp"/>
<text text="2019年1月" textColor="#999999" textSize="14sp"/>
</vertical>
<View bg="#4caf50" h="*" w="10"/>
</card>
<card w="*" h="70" margin="10 5" cardCornerRadius="2dp"
cardElevation="1dp" gravity="center_vertical">
<vertical padding="18 8" h="auto">
<text text="完成毕业设计和论文" textColor="#222222" textSize="16sp"/>
<text text="2019年4月" textColor="#999999" textSize="14sp"/>
</vertical>
<View bg="#2196f3" h="*" w="10"/>
</card>
</vertical>
);

View File

@@ -0,0 +1,46 @@
"ui";
ui.layout(
<frame>
<vertical>
<appbar>
<toolbar id="toolbar" title="卡片布局" />
</appbar>
<list id="todos">
<card w="*" h="70" margin="10 5" cardCornerRadius="2dp"
cardElevation="1dp" gravity="center_vertical">
<vertical padding="18 8" h="auto">
<text text="{{this.title}}" textColor="#222222" textSize="16sp" />
<text text="{{this.summary}}" textColor="#999999" textSize="14sp" />
</vertical>
<View bg="{{this.color}}" h="*" w="10" />
</card>
</list>
</vertical>
</frame>
);
var todos = [
{
title: "",
summary: "",
color: ""
},
{
title: "",
summary: "",
color: ""
},
{
title: "",
summary: "",
color: ""
},
{
title: "",
summary: "",
color: ""
}
];
ui.todos.setDataSource(todos);

View File

@@ -0,0 +1,71 @@
package com.stardust.autojs.core.ui.attribute;
import android.graphics.Color;
import android.support.v7.widget.CardView;
import android.view.View;
import com.stardust.autojs.core.ui.inflater.ResourceParser;
import com.stardust.autojs.core.ui.inflater.util.Dimensions;
public class CardAttributes extends ViewAttributes {
public CardAttributes(ResourceParser resourceParser, View view) {
super(resourceParser, view);
}
@Override
protected void onRegisterAttrs() {
super.onRegisterAttrs();
registerAttr("cardBackgroundColor", Color::parseColor, getView()::setCardBackgroundColor);
registerPixelAttr("cardCornerRadius", getView()::setRadius);
registerPixelAttr("cardElevation", getView()::setCardElevation);
registerPixelAttr("cardMaxElevation", getView()::setMaxCardElevation);
registerBooleanAttr("cardPreventCornerOverlap", getView()::setPreventCornerOverlap);
registerBooleanAttr("cardUseCompatPadding", getView()::setUseCompatPadding);
registerAttr("contentPadding", this::setContentPadding);
registerIntPixelAttr("contentPaddingBottom", this::setContentPaddingBottom);
registerIntPixelAttr("contentPaddingLeft", this::setContentPaddingLeft);
registerIntPixelAttr("contentPaddingTop", this::setContentPaddingTop);
registerIntPixelAttr("contentPaddingRight", this::setContentPaddingRight);
}
private void setContentPadding(String value) {
int[] pixels = Dimensions.parseToIntPixelArray(getView(), value);
getView().setContentPadding(pixels[0], pixels[1], pixels[2], pixels[3]);
}
private void setContentPaddingBottom(int value) {
CardView cardView = getView();
cardView.setContentPadding(cardView.getContentPaddingLeft(), cardView.getContentPaddingTop(),
cardView.getContentPaddingRight(), value);
}
private void setContentPaddingLeft(int value) {
CardView cardView = getView();
cardView.setContentPadding(value, cardView.getContentPaddingTop(),
cardView.getContentPaddingRight(), cardView.getContentPaddingBottom());
}
private void setContentPaddingTop(int value) {
CardView cardView = getView();
cardView.setContentPadding(cardView.getContentPaddingLeft(), value,
cardView.getContentPaddingRight(), cardView.getContentPaddingBottom());
}
private void setContentPaddingRight(int value) {
CardView cardView = getView();
cardView.setContentPadding(cardView.getContentPaddingLeft(), cardView.getContentPaddingTop(),
value, cardView.getContentPaddingBottom());
}
@Override
public CardView getView() {
return (CardView) super.getView();
}
}

View File

@@ -25,6 +25,7 @@ import com.stardust.autojs.core.ui.inflater.util.Drawables;
import com.stardust.autojs.core.ui.inflater.util.Gravities;
import com.stardust.autojs.core.ui.inflater.util.Ids;
import com.stardust.autojs.core.ui.inflater.util.Strings;
import com.stardust.autojs.rhino.debug.Dim;
import com.stardust.util.BiMap;
import com.stardust.util.Supplier;
@@ -45,6 +46,14 @@ import static com.stardust.autojs.core.ui.inflater.inflaters.BaseViewInflater.VI
public class ViewAttributes {
public interface Getter<T> {
T get();
}
public interface Setter<T> {
void set(T value);
}
public interface Attribute {
String get();
@@ -73,40 +82,30 @@ public class ViewAttributes {
}
}
protected interface AttributeGetter {
String get();
protected interface AttributeGetter extends Getter<String> {
}
protected interface AttributeSetter {
void set(String value);
protected interface AttributeSetter extends Setter<String> {
}
protected interface ValueConverter<T> {
T convert(String value);
}
protected interface ValueApplier<T> {
void apply(T value);
}
protected interface ValueGetter<T> {
T get();
}
protected static class MappingAttributeSetter<T> implements AttributeSetter {
private final ValueConverter<T> mValueConverter;
private final ValueApplier<T> mValueApplier;
private final Setter<T> mSetter;
public MappingAttributeSetter(ValueConverter<T> valueConverter, ValueApplier<T> valueApplier) {
public MappingAttributeSetter(ValueConverter<T> valueConverter, Setter<T> Setter) {
mValueConverter = valueConverter;
mValueApplier = valueApplier;
mSetter = Setter;
}
@Override
public void set(String value) {
mValueApplier.apply(mValueConverter.convert(value));
mSetter.set(mValueConverter.convert(value));
}
}
@@ -186,14 +185,14 @@ public class ViewAttributes {
registerDrawableAttrs(new String[]{"bg", "background"}, mView::setBackground);
registerAttr("layout_gravity", Gravities::parse, this::setLayoutGravity);
registerAttr("layout_weight", Float::parseFloat, this::setLayoutWeight);
registerAttr("layout_margin", this::parseDimension, this::setMargin);
registerAttr("layout_margin", this::setMargin);
registerAttr("layout_marginLeft", this::parseDimension, this::setMarginLeft);
registerAttr("layout_marginRight", this::parseDimension, this::setMarginRight);
registerAttr("layout_marginTop", this::parseDimension, this::setMarginTop);
registerAttr("layout_marginBottom", this::parseDimension, this::setMarginBottom);
registerAttr("layout_marginStart", this::parseDimension, this::setMarginStart);
registerAttr("layout_marginEnd", this::parseDimension, this::setMarginEnd);
registerAttr("padding", this::parseDimension, this::setPadding);
registerAttr("padding",this::setPadding);
registerAttr("paddingLeft", this::parseDimension, this::setPaddingLeft);
registerAttr("paddingRight", this::parseDimension, this::setPaddingRight);
registerAttr("paddingTop", this::parseDimension, this::setPaddingTop);
@@ -320,7 +319,7 @@ public class ViewAttributes {
mAttributes.put(name, attribute);
}
protected <V> void registerAttr(String name, ValueGetter<V> getter, ValueApplier<V> setter, BiMap<String, V> biMap) {
protected <V> void registerAttr(String name, Getter<V> getter, Setter<V> setter, BiMap<String, V> biMap) {
mAttributes.put(name, new Attribute() {
@Override
public String get() {
@@ -330,7 +329,7 @@ public class ViewAttributes {
@Override
public void set(String value) {
V v = biMap.get(value);
setter.apply(v);
setter.set(v);
}
});
}
@@ -353,11 +352,11 @@ public class ViewAttributes {
mAttributes.put(name, new BaseAttribute(setter));
}
protected <T> void registerAttr(String name, ValueConverter<T> converter, ValueApplier<T> applier) {
protected <T> void registerAttr(String name, ValueConverter<T> converter, Setter<T> applier) {
mAttributes.put(name, new BaseAttribute(new MappingAttributeSetter<>(converter, applier)));
}
protected <T> void registerAttrs(String[] names, ValueConverter<T> converter, ValueApplier<T> applier) {
protected <T> void registerAttrs(String[] names, ValueConverter<T> converter, Setter<T> applier) {
registerAttrs(names, new MappingAttributeSetter<>(converter, applier));
}
@@ -371,30 +370,30 @@ public class ViewAttributes {
}
}
protected void registerDrawableAttr(String name, ValueApplier<Drawable> applier) {
protected void registerDrawableAttr(String name, Setter<Drawable> applier) {
mAttributes.put(name, new BaseAttribute(new MappingAttributeSetter<>(
this::parseDrawable, applier)));
}
protected void registerDrawableAttrs(String[] names, ValueApplier<Drawable> applier) {
protected void registerDrawableAttrs(String[] names, Setter<Drawable> applier) {
registerAttrs(names, new BaseAttribute(new MappingAttributeSetter<>(
this::parseDrawable, applier)));
}
protected void registerPixelAttr(String name, ValueApplier<Float> applier) {
protected void registerPixelAttr(String name, Setter<Float> applier) {
registerAttr(name, this::parseDimensionToPixel, applier);
}
protected void registerIntPixelAttr(String name, ValueApplier<Integer> applier) {
protected void registerIntPixelAttr(String name, Setter<Integer> applier) {
registerAttr(name, this::parseDimensionToIntPixel, applier);
}
protected void registerIdAttr(String name, ValueApplier<Integer> applier) {
protected void registerIdAttr(String name, Setter<Integer> applier) {
registerAttr(name, Ids::parse, applier);
}
protected void registerBooleanAttr(String name, ValueApplier<Boolean> applier) {
protected void registerBooleanAttr(String name, Setter<Boolean> applier) {
registerAttr(name, Boolean::parseBoolean, applier);
}
@@ -414,14 +413,16 @@ public class ViewAttributes {
}
}
protected void setMargin(int margin) {
protected void setMargin(String margin) {
if (mView.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mView.getLayoutParams();
params.bottomMargin = params.leftMargin = params.topMargin = params.rightMargin = margin;
int[] pixels = Dimensions.parseToIntPixelArray(getView(), margin);
params.setMargins(pixels[0], pixels[1], pixels[2], pixels[3]);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
params.setMarginStart(margin);
params.setMarginEnd(margin);
params.setMarginStart(pixels[0]);
params.setMarginEnd(pixels[2]);
}
getView().setLayoutParams(params);
}
}
@@ -469,8 +470,9 @@ public class ViewAttributes {
}
}
protected void setPadding(int padding) {
mView.setPadding(padding, padding, padding, padding);
protected void setPadding(String padding) {
int[] pixels = Dimensions.parseToIntPixelArray(getView(), padding);
mView.setPadding(pixels[0], pixels[1], pixels[2], pixels[3]);
}
protected void setPaddingLeft(int padding) {
@@ -567,7 +569,7 @@ public class ViewAttributes {
return Strings.parse(mView, value);
}
protected static <T1, T2> ValueApplier<T2> bind(Functions.VoidFunc2<T1, T2> func2, T1 t1) {
protected static <T1, T2> Setter<T2> bind(Functions.VoidFunc2<T1, T2> func2, T1 t1) {
return value -> func2.call(t1, value);
}

View File

@@ -1,6 +1,7 @@
package com.stardust.autojs.core.ui.attribute;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
@@ -22,6 +23,7 @@ public class ViewAttributesFactory {
sViewAttributesCreators.put(ImageView.class, ImageViewAttributes::new);
sViewAttributesCreators.put(AppBarLayout.class, AppbarAttributes::new);
sViewAttributesCreators.put(TextView.class, TextViewAttributes::new);
sViewAttributesCreators.put(CardView.class, CardAttributes::new);
}
public static void put(Class<? extends View> clazz, ViewAttributesCreator creator) {

View File

@@ -2,6 +2,7 @@ package com.stardust.autojs.core.ui.inflater.util;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.CardView;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.InflateException;
@@ -71,6 +72,21 @@ public class Dimensions {
public static int parseToIntPixel(String value, Context context) {
return Math.round(parseToPixel(value, context));
}
public static int[] parseToIntPixelArray(View view, String value) {
String[] split = value.split(" ");
int[] pixels = new int[4];
for (int i = 0; i < split.length; i++) {
pixels[i] = parseToIntPixel(split[i], view);
}
if (split.length == 1) {
pixels[1] = pixels[2] = pixels[3] = pixels[0];
} else if (split.length == 2) {
pixels[2] = pixels[0];
pixels[3] = pixels[1];
}
return pixels;
}
}

View File

@@ -3,6 +3,7 @@ package com.stardust.autojs.core.ui.xml;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.webkit.WebView;
@@ -82,6 +83,7 @@ public class XmlConverter {
.map("appbar", AppBarLayout.class.getName())
.map("tabs", JsTabLayout.class.getName())
.map("viewpager", JsViewPager.class.getName())
.map("card", CardView.class.getName())
);
private static final AttributeHandler ATTRIBUTE_HANDLER = new AttributeHandler.AttrNameRouter()
@@ -90,8 +92,8 @@ public class XmlConverter {
.handler("size", new AttributeHandler.DimenHandler("textSize"))
.handler("id", new AttributeHandler.IdHandler())
.handler("vertical", new AttributeHandler.OrientationHandler())
.handler("margin", new AttributeHandler.MarginPaddingHandler("layout_margin"))
.handler("padding", new AttributeHandler.MarginPaddingHandler("padding"))
.handler("margin", new AttributeHandler.DimenHandler("layout_margin"))
.handler("padding", new AttributeHandler.DimenHandler("padding"))
.handler("marginLeft", new AttributeHandler.DimenHandler("layout_marginLeft"))
.handler("marginRight", new AttributeHandler.DimenHandler("layout_marginRight"))
.handler("marginTop", new AttributeHandler.DimenHandler("layout_marginTop"))