我正尝试使用Sencha Touch构建图像库。 我创建了一个hbox布局和宽度等于window.innerWidth的容器对象。然后,我在里面添加三个容器。每一个使用vbox布局和宽度等于window.innerWidth/3.在不同的Android版本上呈现图像差异
我添加的图像中的宽度等于CSS设置为'自动'的列宽和高度。
这是我的代码:
Ext.define('Oasis.view.ImgTest',{
extend: 'Ext.Container',
xtype:'mosaic',
config:{
cls: 'gallery',
layout: {
type: 'hbox', // This is a column-based mosaic
pack: 'center',
align: 'start'
},
columnWidth: -1,//
numCols: 3, // Number of columns
lastColumnUsed: -1, // Last column where a image were added. -1 for none.
autoInstanceColumns: false, // If true, instantiate each column on initialize method
items:[],
scrollable: {
direction: 'vertical',
directionLock : true
}
},
initialize: function(){
var me = this;
me.setColumnWidth(window.innerWidth/me.getNumCols());
Ext.Viewport.on('orientationchange', function(){
// Updates columns widths on screen orientation changes
me.setColumnWidth(window.innerWidth/me.getNumCols());
for(var c = 0; c < me.getNumCols();c++){
// Column
var column = me.getAt(c);
for(var i = 0; i < column.getItems().getCount();i++){
// Items per column
var element = column.getAt(i);
element.setWidth(me.getColumnWidth());
}
}
}, this, {buffer: 50 });
// Creates all columns and add to the mosaic
if(me.getAutoInstanceColumns())
for(var i = 0; i < me.getNumCols();i++){
me.add({
xtype:'container',
id: 'col-'+i,
cls: 'gallery',
layout: 'vbox',
flex:1
});
}
// Add images
me.insert(Ext.create('Ext.Img',{
src:'resources/images/0228_FEA_Pet_dog_WCGHS_cutt.jpg',
cls: 'mosaicitem',
mode: 'element'
}));me.insert(Ext.create('Ext.Img',{
src:'resources/images/027c076a1c-1152x864.jpg',
cls: 'mosaicitem',
mode: 'element'
}));me.insert(Ext.create('Ext.Img',{
src:'resources/images/0913_LIF_PET_DOG_CUTTY_WCG.jpg',
cls: 'mosaicitem',
mode: 'element'
}));
},
__get_next_column_index: function(){
var me = this;
var column = 0;
// If some column was used in the last iteraction (if there was one),
// calculates the next column that should be used. Else, use column 0.
if(me.getLastColumnUsed() >= 0){
column = (me.getLastColumnUsed() + 1) % me.getNumCols();
}
return column
},
insert: function(element){
var me = this;
// Se as colunas não forem instanciadas logo na initialização da classe, instancia uma a uma até que todas estejam instanciadas.
if(!me.getAutoInstanceColumns() && me.getItems().getCount() < me.getNumCols()){
var column = me.getItems().getCount();
target_column = me.add({
xtype:'container',
id: 'col-'+column,
cls: 'column',
flex:1
});
}else{
var column = me.__get_next_column_index();
var target_column = me.getAt(column);
}
// Set element width
element.setWidth(me.getColumnWidth());
target_column.add(element);
me.setLastColumnUsed(column);
},
})
这是我的CSS:
.gallery {
line-height: 0;
-webkit-column-gap: 0px;
margin: 2px 2px 2px 2px;
height: 100%;
}
.mosaicitem {
height:auto !important;
border: 2px solid rgba(0,0,0,0.4);
border-radius: 5px;
padding: 2px;
background: rgba(0,0,0,0.5);
position: relative;
-webkit-animation: fadein 1s, translateZ 0.6s; /* Safari and Chrome */
-webkit-animation-delay: 0s, 0s;
-moz-animation: fadein 1s, translateZ 0.6s; /* Firefox */
-moz-animation-delay: 0s, 0s;
-ms-animation: fadein 1s, translateZ 0.6s; /* Internet Explorer */
-ms-animation-delay: 0s, 0s;
-o-animation: fadein 1s, translateZ 0.6s; /* Opera */
-o-animation-delay: 0s, 0s;
animation: fadein 1s, translateZ 0.6s;
animation-delay: 0s, 0s;
}
/* Animations */
@keyframes fadein {
from { opacity: 0 ; }
to { opacity: 1 ; }
}
@keyframes translateZ {
from { top : 100px ; }
to { top: 0px ; }
}
/* Firefox */
@-moz-keyframes fadein {
from { opacity: 0 ; }
to { opacity: 1 ; }
}
@-moz-keyframes translateZ {
from { top : 100px ; }
to { top: 0px ; }
}
/* Safari and Chrome */
@-webkit-keyframes fadein {
from { opacity: 0 ; }
to { opacity: 1 ; }
}
@-webkit-keyframes translateZ {
from { top : 100px ; }
to { top: 0px ; }
}
/* Internet Explorer */
@-ms-keyframes fadein {
from { opacity: 0 ; }
to { opacity: 1 ; }
}
@-ms-keyframes translateZ {
from { top : 100px ; }
to { top: 0px ; }
}
/* Opera */
@-o-keyframes fadein {
from { opacity: 0 ; }
to { opacity: 1 ; }
}
@-o-keyframes translateZ {
from { top : 100px ; }
to { top: 0px ; }
}
在运行的是Android 4.4.2设备它完美的作品。但是,在Android 4.2.2上,它看起来好像高度设置为100%或类似的东西。
这是一个android模拟器的比较。
这是怎么回事?
你真的想知道你的代码有什么问题吗?或者可以用另一种方法来获得相同的结果吗? –
我在代码中注意到的第一件事是您错用了布局。当你在一个容器上使用布局时,它的子对象会自动调整大小(如果父对象具有Hbox或父对象具有Vbox,那么父对象的宽度就是这个宽度),所以你并不需要做这些计算。 –
实现相同结果的另一种方法将会很棒=) –