简体   繁体   English

使用 Kotlin 更改片段中的按钮背景

[英]Change Button Background in a Fragment with Kotlin

I'm totally new in kotlin so I don't know how many things I'm doing wrong.我是 kotlin 的新手,所以我不知道我做错了多少。 I'm trying to set the button background of a fragment from code, but when I start the application in my phone, there is just the background and an empty button我正在尝试从代码设置片段的按钮背景,但是当我在手机中启动应用程序时,只有背景和一个空按钮

class start : Fragment() {


    override fun onCreateView(Layoutinflater: LayoutInflater,
                              container: ViewGroup?, savedInstantState: Bundle?):View? {
        return Layoutinflater.inflate(R.layout.fragment_start, container, false)

        val button_start = view!!.findViewById<Button>(R.id.button_start_1_id)

        button_start.setBackgroundResource(R.drawable.button_start1)


    }


}

And

<ImageButton

    android:id="@+id/button_start_1_id"
    android:layout_width="124dp"
    android:layout_height="69dp"
    android:onClick="button_start_pushed">

</ImageButton>

I'm also new in this forum so if a doing something wrong please let me know我也是这个论坛的新手,所以如果做错了什么,请告诉我

Welcome to stackoverflow.欢迎使用 stackoverflow。

Answer to your question is a simple one.回答你的问题很简单。 First of all you do not explicitly need to get reference to your view using the following code, and it is redundant.首先,您不需要使用以下代码明确引用您的视图,这是多余的。

        val button_start = view!!.findViewById<Button>(R.id.button_start_1_id)

You can easily get the reference to your view / imageview using view's id.您可以使用视图的 id 轻松获取对视图/图像视图的引用。 In your case, and you can just call background property in your view class and set the background using resource.getDrawable(R.drawable.button_start1)在您的情况下,您可以在视图类中调用 background 属性并使用 resource.getDrawable(R.drawable.button_start1) 设置背景

If your API level 16 or above, you can use the following code.如果您的 API 级别为 16 或更高,则可以使用以下代码。

class ImageFragment : Fragment(){

override fun onCreateView(Layoutinflater: LayoutInflater,
                          container: ViewGroup?, savedInstantState: Bundle?): View? {
    return Layoutinflater.inflate(R.layout.fragment_start, container, false)

}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    button_start.background = resources.getDrawable(R.drawable.button_start1)
    }
}

resources.getDrawable gives deprecation warning in Kotlin because it requires Resource.Theme as second parameter. resources.getDrawable 在 Kotlin 中给出了弃用警告,因为它需要 Resource.Theme 作为第二个参数。 If you have Resource.Theme, give it as a second parameter.如果您有 Resource.Theme,请将其作为第二个参数。 And your layout would be the following:您的布局如下:

<ImageButton

    android:id="@+id/button_start"
    android:layout_width="124dp"
    android:layout_height="69dp"
    android:onClick="button_start_pushed">

</ImageButton>

If you are using API level below 16, then you could use the following code:如果您使用的 API 级别低于 16,则可以使用以下代码:

class ImageFragment : Fragment(){

override fun onCreateView(Layoutinflater: LayoutInflater,
                          container: ViewGroup?, savedInstantState: Bundle?): View? {
    return Layoutinflater.inflate(R.layout.fragment_start, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    button_start.setImageResource(R.drawable.button_start1)
}
}

Just make sure you have button_start1 icon or img in your drawable, and your layout name is correct.只要确保你的 drawable 中有 button_start1 图标或 img,并且你的布局名称是正确的。

Another things to note: it would be a lot easier for you to have your own consistent naming convention, or at least follow the recommended one by Kotlin or Java.另一件需要注意的事情:拥有自己一致的命名约定会容易得多,或者至少遵循 Kotlin 或 Java 推荐的命名约定。

I hope it helps you!我希望它能帮助你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM