有没有一种方法可以将ImageView的src定义为样式ID

时间:2019-05-04 23:58:49

标签: android xml android-imageview

我想根据主题更改图像(对于“昼夜模式”)。

我尝试在style.xml中创建一个名称为“ logo”的ID,其中包含徽标的可绘制引用,并根据样式进行更改,但是我不知道如何将此ID设置为ImageView来源。

在style.xml中:

import UIKit
import Firebase

class RootPageViewController: UIPageViewController, UIPageViewControllerDataSource {

    lazy var viewControllerList:[UIViewController] = {

        let sb = UIStoryboard(name: "Main", bundle: nil)

        let vc1 = sb.instantiateViewController(withIdentifier: "timelineView")
        let vc2 = sb.instantiateViewController(withIdentifier: "mainView")

        return [vc1, vc2]
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.dataSource = self

        if let secondViewController = viewControllerList[1] as? MainViewController {
            self.setViewControllers([secondViewController], direction: .forward, animated: true, completion: nil)
            secondViewController.buttonAdd.action = #selector(addData(_:))
            secondViewController.buttonReminder.addTarget(self, action: #selector(goToReminder(_:)), for: .touchUpInside)
            secondViewController.buttonSettings.addTarget(self, action: #selector(goToSettings(_:)), for: .touchUpInside)
            secondViewController.buttonUser.addTarget(self, action: #selector(goToProfile(_:)), for: .touchUpInside)

        }
    }


    @objc func addData(_ sender: Any) {
        performSegue(withIdentifier: "goToAdd", sender: self)
    }

    @objc func goToReminder(_ sender: Any) {
        performSegue(withIdentifier: "goToNotifications", sender: self)
    }

    @objc func goToSettings(_ sender: Any) {
        performSegue(withIdentifier: "goToSettings", sender: self)
    }

    @objc func goToProfile(_ sender: Any) {
        if let user = Auth.auth().currentUser {
            self.performSegue(withIdentifier: "goToEdit", sender: self)
        } else {
        performSegue(withIdentifier: "goToProfile", sender: self)
        }
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        guard let vcIndex = viewControllerList.firstIndex(of: viewController) else { return nil }

        let previousIndex = vcIndex - 1
        guard previousIndex >= 0 else { return nil }

        guard viewControllerList.count > previousIndex else { return nil }

        return viewControllerList[previousIndex]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        guard let vcIndex = viewControllerList.firstIndex(of: viewController) else { return nil }

        let nextIndex = vcIndex + 1

        guard viewControllerList.count != nextIndex else { return nil }

        guard viewControllerList.count > nextIndex else { return nil }



        return viewControllerList[nextIndex]
    }

}

我不知道如何在ImageView中引用它,因为当我尝试在“ app:srcCompat”中编写内容时,无法看到智能徽标找到任何“徽标” ID,您可以看到here

如何根据主题更改ImageView源?

1 个答案:

答案 0 :(得分:0)

没关系,只是发现了色彩。

style.xml就是这样

<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
...
<!-- This sets the image tint for the style. -->
<item name="android:tint">@color/colorPrimary</item>
...
</style>