User interface 获取网格行绝对宽度
我试图模拟iOSUser interface 获取网格行绝对宽度,user-interface,xamarin,xamarin.forms,User Interface,Xamarin,Xamarin.forms,我试图模拟iOSUICollectionView来获取硬编码数据。实际上,我只需要在由2列3行组成的网格中显示5个正方形(最后一个是半emp…满) 每列的宽度应为屏幕的一半,这是使用星型(*)完成的。我需要高度总是比宽度小一点。这个想法是要有厚矩形 现在网格在scrollview中,我不确定这是否相关,但我们永远不知道。我这样做是为了让小型手机始终能够在网格中滚动,而其他手机可能只有空白 我一直在摆弄屏幕大小或列宽。我既不能得到列宽的绝对值,也不能得到屏幕宽度的绝对值(总是-1!?)。我可以很容
UICollectionView
来获取硬编码数据。实际上,我只需要在由2列3行组成的网格中显示5个正方形(最后一个是半emp…满)
每列的宽度应为屏幕的一半,这是使用星型(*)完成的。我需要高度总是比宽度小一点。这个想法是要有厚矩形
现在网格在scrollview中,我不确定这是否相关,但我们永远不知道。我这样做是为了让小型手机始终能够在网格中滚动,而其他手机可能只有空白
我一直在摆弄屏幕大小或列宽。我既不能得到列宽的绝对值,也不能得到屏幕宽度的绝对值(总是-1!?)。我可以很容易地得到我的网格项的星号值,即1,但我实际上只需要帧大小,double
值,这样我就可以在视图的构造函数中调整网格的大小,并给它一个绝对值
问题:
如何获得列的绝对宽度?或者如何将行高设置为与列宽相关的值
如果不可能,如何获得屏幕宽度,以便执行可怕的rowheight=screenWdith/2-padding
也许有一种非常简单的方法可以让这个过程变得简单
或者这是可能的吗?我将使用答案#2,获取屏幕宽度和高度,您需要依赖服务来实现这一点,您需要这样的服务:
interface IScreen
{
double Width { get; }
double Height { get; }
double convertPx(int px);
string locationName(double latitude, double longitude);
Task<string> locationNameAsync(double latitude, double longitude);
string Version { get; }
void ShowAlertMessage(string aTitle, string aMessage);
}
接口为屏幕
{
双宽度{get;}
双高{get;}
双转换器px(int-px);
字符串位置名称(双纬度、双经度);
任务位置NameAsync(双纬度、双经度);
字符串版本{get;}
void showartmessage(stringatitle,stringamessage);
}
安卓:
class Screen_Android : Java.Lang.Object, IScreen
{
public Screen_Android() { }
public double Width
{
get
{
var ctx = Forms.Context;
var metrics = ctx.Resources.DisplayMetrics;
return (ConvertPixelsToDp(metrics.WidthPixels));
}
}
public double Height
{
get
{
var ctx = Forms.Context;
var metrics = ctx.Resources.DisplayMetrics;
return (ConvertPixelsToDp(metrics.HeightPixels));
}
}
private static int ConvertPixelsToDp(float pixelValue)
{
var ctx = Forms.Context;
var dp = (int)((pixelValue) / ctx.Resources.DisplayMetrics.Density);
return dp;
}
public double convertPx(int px)
{
var ctx = Forms.Context;
//var dp = (int)((px) / ctx.Resources.DisplayMetrics.Density);
//return (int)((dp * ctx.Resources.DisplayMetrics.Density) + 0.5);
double density = ctx.Resources.DisplayMetrics.Density;
if (density >= 4.0)
{
//"xxxhdpi";
return px * 4;
}
if (density >= 3.0 && density < 4.0)
{
//"xxhdpi";
return px * 3;
}
if(density >= 2.0)
{
//xhdpi
return px * 2;
}
if(density >= 1.5 && density < 2.0)
{
//hdpi
return px * 1.5;
}
if(density >= 1.0 && density < 1.5)
{
//mdpi
return px * 1;
}
return px;
//return (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, px, ctx.Resources.DisplayMetrics);
//var resources = ctx.Resources;
//var metrics = resources.DisplayMetrics;
//int dp = px * ((int)metrics.DensityDpi / 160);
//return dp;
}
public string locationName(double latitude, double longitude)
{
//List<Address> addresses;
Geocoder geocoder = new Geocoder(Forms.Context, Locale.Default);
try
{
var addresses = geocoder.GetFromLocation(latitude, longitude, 10);
if (addresses.All(item => item == null)) return "";
string address = addresses[0].GetAddressLine(0);
string city = addresses[0].GetAddressLine(1);
string country = addresses[0].GetAddressLine(2);
return address + " " + city + " " + country;
}
catch
{
return "";
}
}
public string Version
{
get { throw new NotImplementedException(); }
}
public void ShowAlertMessage(string aTitle, string aMessage)
{
Toast.MakeText(Forms.Context, aMessage, ToastLength.Short).Show();
}
public System.Threading.Tasks.Task<string> locationNameAsync(double latitude, double longitude)
{
throw new NotImplementedException();
}
class-Screen\u Android:Java.Lang.Object,IScreen
{
公共屏幕_Android(){}
公共双宽度
{
得到
{
var ctx=Forms.Context;
var metrics=ctx.Resources.DisplayMetrics;
返回(ConvertPixelsToDp(metrics.WidthPixels));
}
}
公众双高
{
得到
{
var ctx=Forms.Context;
var metrics=ctx.Resources.DisplayMetrics;
返回(ConvertPixelsToDp(metrics.HeightPixels));
}
}
私有静态int ConvertPixelsToDp(浮点像素值)
{
var ctx=Forms.Context;
var dp=(int)((像素值)/ctx.Resources.DisplayMetrics.Density);
返回dp;
}
公共双转换器px(int px)
{
var ctx=Forms.Context;
//var dp=(int)((px)/ctx.Resources.DisplayMetrics.Density);
//返回值(int)((dp*ctx.Resources.DisplayMetrics.Density)+0.5);
双密度=ctx.Resources.DisplayMetrics.density;
如果(密度>=4.0)
{
//“xxxhdpi”;
返回px*4;
}
如果(密度>=3.0和密度<4.0)
{
//“xxhdpi”;
返回px*3;
}
如果(密度>=2.0)
{
//xhdpi
返回px*2;
}
如果(密度>=1.5和密度<2.0)
{
//hdpi
返回px*1.5;
}
如果(密度>=1.0和密度<1.5)
{
//mdpi
返回px*1;
}
返回px;
//return(int)TypedValue.ApplyDimension(ComplexUnitType.Dip、px、ctx.Resources.DisplayMetrics);
//var resources=ctx.resources;
//var-metrics=resources.DisplayMetrics;
//int dp=px*((int)metrics.DensityDpi/160);
//返回dp;
}
公共字符串位置名称(双纬度、双经度)
{
//列出地址;
Geocoder Geocoder=新的Geocoder(Forms.Context,Locale.Default);
尝试
{
var addresses=geocoder.GetFromLocation(纬度,经度,10);
if(addresses.All(item=>item==null))返回“”;
字符串地址=地址[0]。GetAddressLine(0);
字符串city=地址[0]。GetAddressLine(1);
字符串country=地址[0]。GetAddressLine(2);
返回地址+“”+城市+“”+国家;
}
抓住
{
返回“”;
}
}
公共字符串版本
{
获取{抛出新的NotImplementedException();}
}
public void ShowAlertMessage(字符串aTitle,字符串aMessage)
{
Toast.MakeText(Forms.Context、aMessage、ToastLength.Short).Show();
}
public System.Threading.Tasks.Task locationNameAsync(双纬度,双经度)
{
抛出新的NotImplementedException();
}
iOS:
公共类屏幕\u iOS:IScreen
{
公共双宽度
{
得到
{
返回UIScreen.MainScreen.Bounds.Width;
}
}
公众双高
{
得到
{
返回UIScreen.MainScreen.Bounds.Height;
}
}
公共双转换器px(int px)
{
抛出新的NotImplementedException();
}
公共字符串位置名称(双纬度、双经度)
{
字符串locationName=“”;
CLLocation c=新的CLLocation(数学圆(纬度,2),数学圆(经度,2));
CLGeocoder geocoder=新的CLGeocoder();
public class Screen_iOS : IScreen
{
public double Width
{
get
{
return UIScreen.MainScreen.Bounds.Width;
}
}
public double Height
{
get
{
return UIScreen.MainScreen.Bounds.Height;
}
}
public double convertPx(int px)
{
throw new NotImplementedException();
}
public string locationName(double latitude, double longitude)
{
string locationName = "";
CLLocation c = new CLLocation(Math.Round(latitude, 2), Math.Round(longitude, 2));
CLGeocoder geocoder = new CLGeocoder();
geocoder.ReverseGeocodeLocation(c, (placemarks, error) =>
{
if ((placemarks != null) && (placemarks.Length > 0))
locationName = placemarks[0].Name + placemarks[0].PostalCode + placemarks[0].AdministrativeArea + placemarks[0].Country;
});
return locationName;
}
public string Version
{
get
{
NSObject ver = NSBundle.MainBundle.InfoDictionary["CFBundleShortVersionString"];
return ver.ToString();
}
//get { throw new NotImplementedException(); }
}
public void ShowAlertMessage(String aTitle, string aMessage)
{
UIAlertView error = new UIAlertView(aTitle, aMessage, null, "OK" , null);
error.Show();
}
public async Task<string> locationNameAsync(double latitude, double longitude)
{
string locationName = "";
CLLocation loc = new CLLocation(latitude, longitude);
CLGeocoder geocoder = new CLGeocoder();
CLPlacemark[] r = null;
var task = Task.Factory.StartNew(() =>
{
r = geocoder.ReverseGeocodeLocationAsync(loc).Result;
Console.WriteLine("it ran! {0}", r.Length);
});
task.Wait(TimeSpan.FromSeconds(10));
if ((r != null) && (r.Length > 0))
locationName = r[0].Name + r[0].PostalCode + r[0].AdministrativeArea + r[0].Country;
return locationName;
}
}