LinearLayout和LinearLayoutCompat有什么区别 [英] What's the difference between LinearLayout and LinearLayoutCompat

查看:102
本文介绍了LinearLayout和LinearLayoutCompat有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 LinearLayoutCompat 是为我们提供了一些较新的方法,这些方法已添加到更高级别的android中,而降至更低级别的Android.

I know LinearLayoutCompat was realized to give us some newer methods which were added in a higher levels of android to lower levels of Android.

我的问题是这种方法:

 linearLayout.setPaddingRelative

哪个是在API 17中添加的,但我们应该通过使用以下代码在较低的API中使用它?

Which was added in API 17 but we should have it in lower API by using the following code right?

 linearLayoutCompat.setPaddingRelative

但是我的Android Studio仍然在上面显示以下错误.

But my Android Studio still shows the following error on it.

调用需要API级别17(当前最小值为15):android.view.View#setPaddingRelativ

Call requires API level 17 (current min is 15): android.view.View#setPaddingRelativ

那么 LinearLayout LinearLayoutCompat 有什么区别?

推荐答案

LinearLayoutCompat 以支持在旧API(例如分隔符)的较新API级别中添加的方法.如果您比较 LinearLayout View 类( LinearLayout 继承自 View 类).View类支持的方法取决于不同的API级别,这就是为什么在API级别17之前不能同时使用LinearLayout和LinearLayoutCompat不能使用此方法的原因.

LinearLayoutCompat was added to support methods which were added in newer API levels on old ones (like dividers). If you compare the methods in LinearLayout and LinearLayoutCompat you can see that the Compat layout has all methods of the LinearLayout without any API level limitation.
This brings us back to your question: You are trying to use a method which is part of the View class (LinearLayout inherits from the View class). The supported methods of the View class depend on the different API levels that's why you can't use this method before API level 17 neither with the LinearLayout nor the LinearLayoutCompat.

如果您要使用此方法,无论您处于哪种API级别,都可以使用

If you want to use this method no matter what API level you're on you can use the ViewCompat class and call ViewCompat.setPaddingRelative(View view, ...).

这篇关于LinearLayout和LinearLayoutCompat有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆