如何在Android Studio中对齐/格式化代码?

时间:2015-01-29 19:41:20

标签: android android-studio code-formatting code-readability

是否有一种方法/快捷方式/内置功能可以对齐操作数的代码,例如'='符号?

例如,Xcode(https://github.com/qfish/XAlign)有一个XAlign,允许用户选择需要对齐的代码并使用快捷方式自动对齐。

Android Studio中有一个 Fields Group 功能,可以“对齐列”,但它不适用于已编写的代码。

7 个答案:

答案 0 :(得分:40)

Android Studio中的缩进代码:

<强>窗

  

Ctrl + Alt + L

<强>的Mac:

  

选项 + 命令 + L

答案 1 :(得分:4)

在Android Studio Bundle附带的Eclipse中,快捷键如下所示,以启动 Auto Formatter

Ctrl + alt + L

如果您想更改此组合,请转到菜单首选项键盘并设置为自定义。

答案 2 :(得分:2)

变量声明的对齐仅适用于函数之外的对齐。菜单中的对齐功能首选项代码样式环绕和支撑字段组“对齐在列“不适用于括号/大括号内的那些。

以下是JetBrains网站上的问题单:https://youtrack.jetbrains.com/issue/IDEA-56947#

此功能可用,但尚未在稳定版本中提供。它可以从build:140.1761

获得

在撰写此答案时,我目前的版本是“135.1653844,建于2014年12月18日”,发布于Stable Release。

答案 3 :(得分:1)

适用于Android Studio中的Windows和Linux:

  

Ctrl + Alt + L

对于Mac:

  

选项 + 命令 + L

答案 4 :(得分:1)

Windows:

Ctrl + Alt + L

Linux:

Ctrl + Shift + Alt + L

macOS:

⌥选项 + ⌘命令 + L

答案 5 :(得分:0)

Ubuntu Linux:Ctrl-alt-L(由其他响应建议)由窗口管理器捕获并锁定屏幕。因此,您需要更改它:文件->设置->键盘映射->主菜单->代码->重新格式化代码。我使用双标签,因为我不希望该标签向右移动选择。

这将按照您在首选项中指定的格式设置代码格式。要处理您的特定请求(对齐equals语句),请转到:(文件->设置->编辑器->代码样式->(选择语言)->包装和括号->组声明->在列中对齐变量

这些样式是特定于语言的,因此此特定选项可能在您选择的语言中不存在,或者命名不同。

答案 6 :(得分:0)

Windows:

Option Explicit

Public Sub GetNFLMatchInfo()
    Dim html As HTMLDocument, html2 As HTMLDocument

    Set html = New HTMLDocument: Set html2 = New HTMLDocument

    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://www.sportsinteraction.com/football/nfl-betting-lines/", False
        .send
        html.body.innerHTML = .responseText
    End With

    Dim allNodes As Object, i As Long, resultsTable(), r As Long, headers()
    Dim dateValue As String, timeValue As String, title As String, html3 As HTMLDocument
    headers = Array("Date", "Time", "Title", "Team", "Pointspread handicap", "Pointspread price", "Moneyline price", "O/U Name", "O/U Handicap", "O/U Price")

    Set allNodes = html.querySelectorAll(".date, .time, .title, .gameBettingContent") 'nodeList of all items of interest. gameBettingContent is a block _
                                                                that will be further subdivided by reading its html into a 'surrogate' HTMLDocument
    ReDim resultsTable(1 To html.querySelectorAll("#runnerNames li").Length, 1 To UBound(headers) + 1)

    r = 1: Set html3 = New HTMLDocument

    For i = 0 To allNodes.Length - 1
        With allNodes.item(i)
            Select Case .className
            Case "date"
                dateValue = .innerText
            Case "time"
                timeValue = .innerText
            Case "title"
                title = Trim$(.innerText)
            Case "gameBettingContent"
                Dim runners  As Object, contentDivs As Object, pointSpreadHandicaps As Object
                Dim pointSpreadPrices As Object, moneyLinePrices As Object, runners As Object
                Dim OuHandicaps As Object, OuPrices As Object

                r = r + 2                        'then fill line one at r-2, and line 2 at r-1
                html2.body.innerHTML = .outerHTML

                Set runners = html2.querySelectorAll("#runnerNames li")

                resultsTable(r - 2, 1) = dateValue: resultsTable(r - 1, 1) = dateValue
                resultsTable(r - 2, 2) = timeValue: resultsTable(r - 1, 2) = timeValue
                resultsTable(r - 2, 3) = title: resultsTable(r - 1, 3) = title
                resultsTable(r - 2, 4) = runners.item(0).innerText: resultsTable(r - 1, 4) = runners.item(1).innerText

                Set contentDivs = html2.querySelectorAll(".betTypeContent")
                html3.body.innerHTML = contentDivs.item(0).outerHTML

                'populate resultsTable for two rows relating to current gameBettingContent
                Set pointSpreadHandicaps = html3.querySelectorAll(".handicap")
                Set pointSpreadPrices = html3.querySelectorAll(".price")

                resultsTable(r - 2, 5) = pointSpreadHandicaps.item(0).innerText: resultsTable(r - 1, 5) = pointSpreadHandicaps.item(1).innerText
                resultsTable(r - 2, 6) = pointSpreadPrices.item(0).innerText: resultsTable(r - 1, 6) = pointSpreadPrices.item(1).innerText

                html3.body.innerHTML = contentDivs.item(1).outerHTML 'Set html3 content to next content div to right

                Set moneyLinePrices = html3.querySelectorAll(".price")
                resultsTable(r - 2, 7) = moneyLinePrices.item(0).innerText: resultsTable(r - 1, 7) = moneyLinePrices.item(1).innerText

                html3.body.innerHTML = contentDivs.item(2).outerHTML

                Set runners = html3.querySelectorAll(".name")
                Set OuHandicaps = html3.querySelectorAll(".handicap")
                Set OuPrices = html3.querySelectorAll(".price")

                resultsTable(r - 2, 8) = runners.item(0).innerText: resultsTable(r - 1, 8) = runners.item(1).innerText
                resultsTable(r - 2, 9) = OuHandicaps.item(0).innerText: resultsTable(r - 1, 9) = .item(1).innerText
                resultsTable(r - 2, 10) = OuPrices.item(0).innerText: resultsTable(r - 1, 10) = OuPrices.item(1).innerText
            End Select
        End With
    Next
    With ThisWorkbook.Worksheets("Sheet1")
        .Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
        .Cells(2, 1).Resize(UBound(resultsTable, 1), UBound(resultsTable, 2)) = resultsTable
    End With
End Sub

Linux:

CTRL-ALT-L

macOS:

CTRL-SHIFT-ALT-L