简体   繁体   English

Android中不推荐使用的AbsoluteLayout替代品是什么?

[英]What's alternative to deprecated AbsoluteLayout in Android?

I have a Xamarin project. 我有一个Xamarin项目。 I develop for IOS,Android and UWP. 我为IOS,Android和UWP开发。 In my application, I have my manual layout logic for UI Elements. 在我的应用程序中,我有UI元素的手动布局逻辑。 In IOS, I can use the frame property in order to set where the view is going to be rendered. 在IOS中,我可以使用frame属性来设置视图的渲染位置。 I can do the same in UWP by using Canvas as the container and Canvas.Left,Canvas.Top properties to set x,y locations and my code has the logic to do the layout. 我可以通过使用Canvas作为容器并使用Canvas.Left,Canvas.Top属性设置x,y位置来在UWP中执行相同的操作,并且我的代码具有执行布局的逻辑。 I am confused about how to achieve this in Android. 我对如何在Android中实现这一目标感到困惑。 AbsoluteLayout seemed to be a perfect match, but it's deprecated. AbsoluteLayout似乎是完美的搭配,但已弃用。 Can I achieve this with some other Layout or should I create my custom ViewGroup class? 我可以使用其他布局实现此功能,还是应该创建自定义ViewGroup类?

You can use a FrameLayout and position items in it using the top and left margin. 您可以使用FrameLayout并使用顶部和左侧边距在其中放置项目。 In XML it would look something like: 在XML中,它看起来像:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <View
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="100dp"
    />

</FrameLayout>

If you want to set it from code then you can use the LayoutParams: 如果要通过代码进行设置,则可以使用LayoutParams:

FrameLayout.LayoutParams param = (FrameLayout.LayoutParams) view.getLayoutParams();
param.leftMargin = 100;
param.topMargin = 100;
param.height = 50;
param.width = 50;
view.setLayoutParams(param);

Note that the values in code are pixels not dp so you would have to convert. 请注意,代码中的值是像素而不是dp,因此您必须进行转换。 I'm not sure how this would convert to Xaramin but it gives you the idea. 我不确定如何将其转换为Xaramin,但可以为您提供想法。

Either way you'll have to consider what will happen when a user with an unusual device size uses your app. 无论哪种方式,您都必须考虑当设备尺寸异常的用户使用您的应用时会发生什么。 The reason Android doesn't have much use for absolute layouts is that there are so many different device sizes/densities that they are usually impractical. Android不能在绝对布局中使用太多的原因是,设备尺寸/密度如此之大,以至于它们通常是不切实际的。

You can use Relative Layout/ Frame Layout / Custom Layout. 您可以使用相对布局/框架布局/自定​​义布局。 instead of Absolute Layout, As Absolute layout is harder to maintain is the reason its depreciated. 而不是绝对布局,因为绝对布局更难维护是其贬值的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM