片段:OnCreateView中的RelativeLayout可见性不可更改 [英] Fragment: RelativeLayout Visibility not changeable in OnCreateView

查看:108
本文介绍了片段:OnCreateView中的RelativeLayout可见性不可更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我正在使用Kotlin) 这是片段中的OnCreateView.

(Im using Kotlin) So here is my OnCreateView in the Fragment.

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view: View = inflater!!.inflate(R.layout.fragment_bots, container, false)

        BotDiv2.visibility = View.VISIBLE
        startUp()
        return view
    }

这是relativelayout的xml:

and here is the xml of the relativelayout:

<RelativeLayout
                android:id="@+id/BotDiv2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="33.3"
                android:visibility="invisible">

                <ImageButton
                    android:id="@+id/BotBtn1"
                    android:layout_width="90dp"
                    android:layout_height="90dp"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:background="@null"
                    android:scaleType="fitCenter"
                    android:src="@android:drawable/btn_star_big_on" />

                <TextView
                    android:id="@+id/uselessLevel1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/BotBtn1"
                    android:layout_alignStart="@+id/BotBtn1"
                    android:text="Level:" />

                <TextView
                    android:id="@+id/BotWorth1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/BotBtn1"
                    android:layout_centerHorizontal="true"
                    android:text="$500"
                    android:textAppearance="@style/TextAppearance.AppCompat.Large" />

                <TextView
                    android:id="@+id/levelBot1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/BotBtn1"
                    android:layout_alignEnd="@+id/BotBtn1"
                    android:text="1" />

            </RelativeLayout>

要在其他功能中使用它,但这是

In want to use it in another Function, but this:

BotDiv2.visibility = View.VISIBLE

导致NPE,我也尝试使用findViewById,但这也会导致NPE(或不影响Kotlin的?"的原因).

causes NPEs I also tried to use findViewById, but that causes an NPE as well (or not affecting, cuz of Kotlin's "?").

推荐答案

除了ID似乎不正确之外,如上面的评论中所述.

Other than the ID seemingly not being right, as mentioned in a comment above...

由于在此阶段尚未为Fragment设置View(尚未将其返回到框架),所以不能在Fragment本身上调用findViewById,但是可以改为对刚膨胀的View进行findViewById调用:

Since at this stage your View is not set for your Fragment yet (you haven't returned it to the framework), you can't call findViewById on the Fragment itself, but you can make a findViewById call on the newly inflated View instead:

val view: View = inflater!!.inflate(R.layout.fragment_bots, container, false)
val bd2 = view.findViewById(R.id.BotDiv2)
bd2.visibility = View.VISIBLE

如果您使用的是Kotlin Android扩展程序,则可以使用以下语法进行相同的操作:

If you're using Kotlin Android Extensions, you can do the same with this syntax:

val view: View = inflater!!.inflate(R.layout.fragment_bots, container, false)
view.BotDiv2.visibility = View.VISIBLE

这篇关于片段:OnCreateView中的RelativeLayout可见性不可更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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