【问题标题】:Check what type of child has a LinearLayout检查什么类型的孩子有一个 LinearLayout
【发布时间】:2021-10-19 11:36:41
【问题描述】:

我正在尝试检查哪种类型的儿童具有线性布局,但我一直认为 textview 与视图相同。如何区分 textview 和 view。

在我的线性布局中,我有一个视图和一个文本视图,并且在下面的代码中,当孩子是 textview 时,我在两个句子中都变得真实(我的意思是我在 view 是 View 而 view 是 TextView 如果那一刻的孩子是textview)。而且我想知道如果孩子是 textview 的话,当句子是 view 是 View 时,我怎么会出错。

这是我要检查孩子类型的地方:

private fun selectFromLayout(ll: LinearLayout, select: Boolean) {
    val childCount = ll.childCount
    for (i in 0 until childCount) {
        val view = ll.getChildAt(i)
        if (view is View) {
            if (select) {
                view.background = ContextCompat.getDrawable(context, R.drawable.back_select)
            } else {
                view.setBackgroundResource(R.color.white)
            }
        }

        if (view is TextView) {
            if (select) {
                view.setTextColor(ContextCompat.getColor(context, R.color.gray_dark))
                view.typeface = ResourcesCompat.getFont(context, R.font.montserrat_medium)
            } else {
                view.setTextColor(ContextCompat.getColor(context, R.color.gray_medium))
                view.typeface = ResourcesCompat.getFont(context, R.font.montserrat_regular)
            }
        }
    }
}

线性布局:

       <LinearLayout
        android:id="@+id/btn_tab1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="?attr/selectableItemBackground"
        android:focusable="true"
        android:gravity="bottom"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_tab1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:gravity="center"
            app:textSize="10sp"
            app:textType="regular"
            tools:text="Próximas" />

        <View
            android:id="@+id/tab1_shadow"
            android:layout_width="match_parent"
            android:layout_height="2dp" />

    </LinearLayout>

【问题讨论】:

    标签: kotlin view textview android-linearlayout


    【解决方案1】:

    没有必要检查 is View,因为它总是正确的,因为 android 中的所有视图都扩展自 View 类。

    private fun selectFromLayout(ll: LinearLayout, select: Boolean) {
        val childCount = ll.childCount
        for (i in 0 until childCount) {
            val view = ll.getChildAt(i)
            if (view is TextView) {
                if (select) {
                    view.setTextColor(ContextCompat.getColor(context, R.color.gray_dark))
                    view.typeface = ResourcesCompat.getFont(context, R.font.montserrat_medium)
                } else {
                    view.setTextColor(ContextCompat.getColor(context, R.color.gray_medium))
                    view.typeface = ResourcesCompat.getFont(context, R.font.montserrat_regular)
                }
            } else {
                if (select) {
                    view.background = ContextCompat.getDrawable(context, R.drawable.back_select)
                } else {
                    view.setBackgroundResource(R.color.white)
                }
            }
        }
            
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-30
      • 2021-01-29
      • 2018-08-07
      相关资源
      最近更新 更多