Tag: findviewbyid

kotlin从NavigationView头获取视图

我有这样的差异,但我相信有exsists更好的方式来做到这一点 val header = nav_view.getHeaderView(0) as LinearLayout val userName = header.findViewById(R.id.user_name) as TextView 但是当它只是通过user_name获得它的时候它是空的

与Android的Kotlin:基类和Kotlin Android扩展

所以我使用Kotlin Android Extensions ,我觉得它很容易使用,非常值得。 没有更多的findViewById或Butterknife.Bind(…) 。 除了一种情况,我没有发现任何问题。 在基类中,例如, BaseActivity ,通常会有一堆视图出现在所有布局中,例如工具栏。 和常见的操作,如changeToolbarColor()或setToolbarTitle() 。 在这种简单的情况下,我不能使用Kotlin Android Extensions ,因为它是一个基类,视图本身将出现在多个布局上,而且不能被属性导入。 在这种情况下,我只是简单地使用by lazy {find(…) 。 有没有什么办法可以用内置的android扩展插件来完成?

android自定义视图findviewId为null并且不可见

我试图用我自己的自定义视图充满layout.xml文件。 我多次使用此视图,但有时在Android Studio中不会显示预览。 而当我通过’findViewById’得到这个自定义视图(实际上,我使用Kotlin所以我直接写Id),它总是返回null。 我读了很多关于“自定义视图findviewById返回null”的问题,直到现在我没有find答案。 我错了什么? 这是我的自定义类 class SubTitleBar(ctx:Context) : RelativeLayout(ctx) { private val mTitle:TextView private val mSideBtn:TextView constructor(ctx: Context, attrs: AttributeSet) : this(ctx) { val typedArray = context.obtainStyledAttributes(attrs, R.styleable.SubTitleBar) setAttributeSet(typedArray) } constructor(ctx: Context, attrs: AttributeSet, defStyle:Int) : this(ctx, attrs) { val typedArray = context.obtainStyledAttributes(attrs, R.styleable.SubTitleBar, defStyle, 0) setAttributeSet(typedArray) } init { val infService = […]

Android:创建较短版本的findViewById – 未经检查的转换

我写了一个方法来获取Android中的id这样的意见:例如 Button button = <Button>find(R.id.someId); 实际上不需要<Button>。 这是我写的方法: protected <T extends View> T find(int id){ return (T) findViewById(id); } 它工作的很好,但警告“未经检查的演员”困扰我。 我怎么能摆脱它? 这种方法保存吗? 我非常受Kotlin的启发,我想创建这个漂亮的小方法来使代码更漂亮。

kotlin从NavigationView头获取视图

我有这样的差异,但我相信有exsists更好的方式来做到这一点 val header = nav_view.getHeaderView(0) as LinearLayout val userName = header.findViewById(R.id.user_name) as TextView 但是当它只是通过user_name获得它的时候它是空的

与Android的Kotlin:基类和Kotlin Android扩展

所以我使用Kotlin Android Extensions ,我觉得它很容易使用,非常值得。 没有更多的findViewById或Butterknife.Bind(…) 。 除了一种情况,我没有发现任何问题。 在基类中,例如, BaseActivity ,通常会有一堆视图出现在所有布局中,例如工具栏。 和常见的操作,如changeToolbarColor()或setToolbarTitle() 。 在这种简单的情况下,我不能使用Kotlin Android Extensions ,因为它是一个基类,视图本身将出现在多个布局上,而且不能被属性导入。 在这种情况下,我只是简单地使用by lazy {find<>(…) 。 有没有什么办法可以用内置的android扩展插件来完成?