以编程方式设置Android工具栏标题外观

时间:2018-05-09 00:31:27

标签: android xamarin.forms

我在Xamarin Forms项目中创建了一个Android工具栏:

extension ViewController{

    //------------------------------------------------
    //MARK: Get CIImageProperyOrientation From UIImage
    //------------------------------------------------


    /// Converts A UIImageOrientation To A CGImagePropertyOrientation
    ///
    /// - Parameter orientation: UIImageOrientation
    /// - Returns: CGImagePropertyOrientation
    func cgImagePropertyOrientation(_ orientation: UIImageOrientation) -> CGImagePropertyOrientation {
        switch orientation {
        case .up:
            return .up
        case .upMirrored:
            return .upMirrored
        case .down:
            return .down
        case .downMirrored:
            return .downMirrored
        case .leftMirrored:
            return .leftMirrored
        case .right:
            return .right
        case .rightMirrored:
            return .rightMirrored
        case .left:
            return .left
        }
    }

    //---------------------
    //MARK: File Management
    //---------------------

    /// Returns The Documents Directory
    ///
    /// - Returns: URL
    func getDocumentsDirectory() -> URL {

        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        let documentsDirectory = paths[0]
        return documentsDirectory

    }

}

extension ViewController: ARSCNViewDelegate{

    func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

        //1. If Out Target Image Has Been Detected Than Get The Corresponding Anchor
        guard let currentImageAnchor = anchor as? ARImageAnchor else { return }

        //2. Get The Targets Name
        let name = currentImageAnchor.referenceImage.name!

        //3. Get The Targets Width & Height
        let width = currentImageAnchor.referenceImage.physicalSize.width
        let height = currentImageAnchor.referenceImage.physicalSize.height

        //4. Log The Reference Images Information
        print("""
            Image Name = \(name)
            Image Width = \(width)
            Image Height = \(height)
            """)

        //5. Create A Plane Geometry To Cover The ARImageAnchor
        let planeNode = SCNNode()
        let planeGeometry = SCNPlane(width: width, height: height)
        planeGeometry.firstMaterial?.diffuse.contents = UIColor.white
        planeNode.opacity = 0.25
        planeNode.geometry = planeGeometry

        //6. Rotate The PlaneNode To Horizontal
        planeNode.eulerAngles.x = -.pi/2

        //7. The Node Is Centered In The Anchor (0,0,0)
        node.addChildNode(planeNode)

        //8. Create AN SCNBox
        let boxNode = SCNNode()
        let boxGeometry = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)

        //9. Create A Different Colour For Each Face
        let faceColours = [UIColor.red, UIColor.green, UIColor.blue, UIColor.cyan, UIColor.yellow, UIColor.gray]
        var faceMaterials = [SCNMaterial]()

        //10. Apply It To Each Face
        for face in 0 ..< 5{
            let material = SCNMaterial()
            material.diffuse.contents = faceColours[face]
            faceMaterials.append(material)
        }
        boxGeometry.materials = faceMaterials
        boxNode.geometry = boxGeometry

        //11. Set The Boxes Position To Be Placed On The Plane (node.x + box.height)
        boxNode.position = SCNVector3(0 , 0.05, 0)

        //12. Add The Box To The Node
        node.addChildNode(boxNode)
    }
}

class ViewController: UIViewController {

    //1. Create A Reference To Our ARSCNView In Our Storyboard Which Displays The Camera Feed
    @IBOutlet weak var augmentedRealityView: ARSCNView!

    //2. Create Our ARWorld Tracking Configuration
    let configuration = ARWorldTrackingConfiguration()

    //3. Create Our Session
    let augmentedRealitySession = ARSession()

    //4. Create An Array To Store Our Reference Images
    var customReferenceImages = [ARReferenceImage]()

    //5. Create An Identifier So We Can Create A Unique Name For Each Image
    var identifier = 0

    //--------------------
    //MARK: View LifeCycle
    //--------------------

    override func viewDidLoad() {

        setupARSession()

        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

    //--------------------------------
    //MARK: Creation Of Dynamic Images
    //--------------------------------

    /// Saves The Snapshot Of An ARSCNView
    @IBAction func saveScreenShot(){

        //1. Create A Snapshot Of The ARView
        let screenShot = self.augmentedRealityView.snapshot()

        //2. Convert It To A PNG
        guard let imageData = UIImagePNGRepresentation(screenShot) else { return }

        //3. Store The File In The Documents Directory
        let fileURL = getDocumentsDirectory().appendingPathComponent("custom\(identifier).png")

        //4. Write It To The Documents Directory & Increase The Identifier
        do {
           try imageData.write(to: fileURL)
           identifier += 1
        } catch  {
            print("Error Saving File")
        }

        //5. Load The Custom Images
        loadCustomImages()
    }


    /// Loads Any Custom Images From The Documents Directory & Appends Them To A Custom [ARReferenceImage]
    func loadCustomImages(){

        //1. Get Reference To The NSFileManager
        let fileManager = FileManager.default

        //2. Get The URL Of The Documents Directory
        let documentsDirectory = getDocumentsDirectory()

        do {

            //a. Get All Files In The Documents Directory
            let fileURLs = try fileManager.contentsOfDirectory(at: documentsDirectory, includingPropertiesForKeys: nil)

            //b. Loop Through Them And If The Path Contains Our Custom Prefix Then Convert To CGImage & Then ARReference Image
            for file in fileURLs{

                if file.lastPathComponent.hasPrefix("custom"){

                    if let arImage = UIImage(contentsOfFile: file.path), let arCGImage = arImage.cgImage{

                        /* Here You Will Need To Work Out The Pysical Widht Of The Image In Metres */

                        let widthInCM: CGFloat = CGFloat(arCGImage.width) / CGFloat(47)
                        let widthInMetres: CGFloat = widthInCM * 0.01

                        let arReferenceImage = ARReferenceImage(arCGImage,
                                                                orientation: cgImagePropertyOrientation(arImage.imageOrientation),
                                                                physicalWidth: widthInMetres)

                        arReferenceImage.name = file.lastPathComponent

                        customReferenceImages.append(arReferenceImage)
                    }
                }
            }

        } catch {

            print("Error Listing Files \(documentsDirectory.path): \(error.localizedDescription)")
        }


        //3. Set Our ARSession Configuration Detection Images
        configuration.detectionImages = Set(customReferenceImages)
        augmentedRealitySession.run(configuration, options:  [.resetTracking, .removeExistingAnchors ])


    }

    //---------------
    //MARK: ARSession
    //---------------

    /// Sets Up The ARSession
    func setupARSession(){

        //1. Set The AR Session
        augmentedRealityView.session = augmentedRealitySession

        //2. Conifgure The Type Of Plane Detection
        configuration.planeDetection = []

        //3. If In Debug Mode Show Statistics
        #if DEBUG
        augmentedRealityView.showsStatistics = true
        #endif

        //4. Run The Session
        augmentedRealitySession.run(configuration, options:  [.resetTracking, .removeExistingAnchors])
        augmentedRealityView.delegate = self

    }

}

通过执行以下操作,我可以轻松设置背景颜色和其他布局参数:

var toolbar = activity.LayoutInflater
   .Inflate(FormsAppCompatActivity.ToolbarResource, null)
   .JavaCast<Android.Support.V7.Widget.Toolbar>();

然而,我无法想出以编程方式设置字体大小的方法,因为设置它的方法是:

toolbar.LayoutParameters = new LayoutParams(LayoutParams.MatchParent, LayoutParams.WrapContent);
toolbar.SetBackgroundColor(backgroundColor.ToAndroid());

我不想用配置的资源设置它。我想用字体大小和/或其他样式属性动态设置它。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

我不知道使用java是否是同样的事情。当我遇到这个问题时,我在我的 java 代码中添加了一个setSupportActionBar(toolbar);。结果是这样的:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(loggedUserName);
setSupportActionBar(toolbar);

工作得很好。

相关问题