【问题标题】:android custom view findviewId null and no visibleandroid自定义视图findviewId为null且不可见
【发布时间】:2018-01-18 07:53:45
【问题描述】:

我正在尝试使用我自己的自定义视图,其中包含 layout.xml 文件。我多次使用此视图,但有时它不会在 Android Studio 中预览。当我通过'findViewById'在Activity中获得这个自定义视图时(事实上,我使用Kotlin所以我只是直接写Id。)它总是返回null。 我读了很多关于“自定义视图 findviewById 返回 null”的问题,直到现在我还没有找到答案。我怎么了?

这是我的自定义类

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 = Context.LAYOUT_INFLATER_SERVICE
        val inflater = ctx.getSystemService(infService) as LayoutInflater
        val view = inflater.inflate(R.layout.cv_subtitle,this,false)

        mTitle = view.findViewById(R.id.subtitle_title)
        mSideBtn = view.findViewById(R.id.subtitle_btn)

        addView(view)
}


    private fun setAttributeSet(typedArray: TypedArray) {

        val titleText = typedArray.getString(R.styleable.SubTitleBar_title)
        mTitle.text = titleText

        val btnText = typedArray.getString(R.styleable.SubTitleBar_btnText)

        if (btnText!= null && btnText.isNotEmpty()) {
            mSideBtn.text = btnText
        } else {
            mSideBtn.visibility = TextView.GONE
        }

        val showDivider = typedArray.getBoolean(R.styleable.SubTitleBar_showDivider, false)

        if (showDivider) {
            subtitle_divider.visibility = View.VISIBLE
        }


        typedArray.recycle()
    }

    fun setTitle(title:String) {
        mTitle.text = title
    }

    fun setSideButtonClickListener(listener: (View) -> Unit) = mSideBtn.setOnClickListener(listener)

}

这是 cv_subtitle.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="52dp"
android:background="@color/white"
android:orientation="horizontal">
    <TextView
        android:id="@+id/subtitle_title"
        style="@style/SubTitleNormal"
        android:paddingStart="@dimen/contentStartPadding"
        android:layout_centerVertical="true"
        />
    <TextView
        android:id="@+id/subtitle_btn"
        style="@style/SubTitleSideBtn"
        android:layout_alignParentEnd="true"
        android:paddingEnd="@dimen/contentEndPadding"
        android:layout_centerVertical="true"
        />
    <View
        android:id="@+id/subtitle_divider"
        style="@style/PaddingDividerGray"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:visibility="gone"
        />
</RelativeLayout>

在 Fragment 布局资源文件中我是这样使用的。

    <com.sample.harleyquinn.view.SubTitleBar
        android:layout_width="match_parent"
        android:layout_height="@dimen/subTitleBarSize"
        android:id="@+id/my_apt_list_subtitle"
        app:title="apt"
        app:btnText="edit"
        />

在片段(或活动)中,我尝试这样。

val subTitle = my_apt_list_subtitle

我也尝试过,但不起作用。

val subTitle = view.findViewById(R.id.my_apt_list_subtitle)

【问题讨论】:

    标签: android kotlin custom-view findviewbyid


    【解决方案1】:

    您需要将包括 id 的属性传递给超类型。你必须改变

    class SubTitleBar(ctx:Context) : RelativeLayout(ctx) {
    

    class SubTitleBar(ctx:Context, attrs: AttributeSet) : RelativeLayout(ctx, attrs) {
    

    并相应地更改您的其他构造函数。

    【讨论】:

      【解决方案2】:

      尝试在 cv_subtitle.xml 中包含 SubTitleBar

      【讨论】:

      • 对不起,我迟到了-.. 我想用 xml 文件扩充自定义视图。我无法理解您在 cv_subtitle.xml 文件中包含 SubTitleBar 的目的。因为 cv_subtitle.xml 是 SubTitleBar 的 xml 布局(我的自定义视图)。
      猜你喜欢
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      相关资源
      最近更新 更多