使用Kotlin Android扩展程序以编程方式放大布局 [英] Programmatically inflated layout with Kotlin Android Extensions

查看:77
本文介绍了使用Kotlin Android扩展程序以编程方式放大布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@android:color/white"
    android:paddingLeft="20dp"
    android:paddingRight="20dp">

    <TextView
        android:id="@+id/tvErrorTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textColor="@android:color/background_dark"
        android:textSize="18sp"
        />
    <TextView
        android:id="@+id/tvErrorDesc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:textColor="@android:color/darker_gray"
        android:textSize="16sp"
        />
    <TextView
        android:id="@+id/tvAction"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="10dp"
        android:layout_gravity="end"
        android:padding="5dp"
        android:textSize="15sp"
        android:textStyle="bold"
        android:textAllCaps="true"
        android:textColor="@android:color/holo_purple"
        />
</LinearLayout>

当我想在以下活动之外使用 kotlin android扩展时,它不起作用.我最终做了findViewById.

When I want to use kotlin android extensions outside of activity like below, it doesn't work. I ended up doing findViewById.

...
...
import kotlinx.android.synthetic.main.dialog_error.*
...
...
 val view = LayoutInflater.from(context).inflate(R.layout.dialog_error, null, false)
    val tvErrorTitle = view.findViewById(R.id.tvErrorTitle) as TextView
    val tvErrorDesc = view.findViewById(R.id.tvErrorDesc) as TextView
    val tvErrorAction = view.findViewById(R.id.tvAction) as TextView

它不会直接从xml中提取视图.

It doesn't pull the views directly from xml. How to use it in programetically inflated layout and avoid findViewById?

注意:此问题严格属于 Kotlin Android扩展,而不是语言本身.

Note : This question strictly belongs to Kotlin Android Extensions, not the language itself.

修改 我都导入了:

import kotlinx.android.synthetic.main.dialog_error.view.*
import kotlinx.android.synthetic.main.dialog_error.*

但是Android Studio仍尝试从R.id导入,并且无法识别这两次导入.缺少什么吗?

But Android Studio still tries to import from R.id and doesn't recognize those two imports. Is there anything missing?

推荐答案

向您证明链接:

如果我们想在View上调用综合属性(在适配器类中很有用),我们还应该导入

If we want to call the synthetic properties on View (useful in adapter classes), we should also import

kotlinx.android.synthetic.main.activity_main.view.*.

即,也导入kotlinx.android.synthetic.main.layout.view.*以加载View扩展属性.

That is, import kotlinx.android.synthetic.main.layout.view.* as well to load the View extension properties.

然后:

val view = LayoutInflater.from(context).inflate(...)
view.tvErrorTitle.text = "test"

这篇关于使用Kotlin Android扩展程序以编程方式放大布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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