2016-03-06 102 views
9

我想提出一個TEditButton動態地的ListView所以我這樣做的搜索框如何刪除listview搜索框中的清除按鈕?

ListView       := TListView.Create(Self);  
ListView.Parent     := Self; 
ListView.Name     := 'hello'; 
ListView.Height     := 369; 
ListView.Width     := 369; 
ListView.Align     := TAlignLayout.Bottom; 
ListView.SearchVisible   := True; 
ListView.BeginUpdate;  

for i := 0 to ListView.controls.Count - 1 do 
begin 
    if ListView.controls[i].ClassType = TSearchBox then 
    begin  
    SearchBox := TSearchBox(ListView.controls[i]); 
    end; 
end; 

OKbtn := TEditButton.Create(SearchBox); 
OKbtn.Parent := SearchBox;  
OKbtn.Text := 'OK'; 
OKbtn.Width := 30; 

SearchBox.AddObject(OKbtn); 
ListView.EndUpdate;  

但問題是clear button也要來的,而編輯搜索框。如何刪除搜索框右側的清除按鈕(X)?

回答

11

enter image description here

搜索框是TEDIT的後裔,並使用FireMonkey控件樣式。

將您的表單上TEDIT並打開其StyleLookup物業:

enter image description here

你可以看到,目前已經有不同stlyes。 所以我們想改變我們的SearchBox的StyleLookup。

由於SearchBox是ListView控件的私有成員(FSearchEdit),因此您無法直接訪問它。

您可以創建自己的ListView控件,它是TListView(TListViewBase)的後代或使用類助手。我選擇後者。

TListViewHelper = class helper for TListViewBase 
private 
    function GetClearButton: Boolean; 
    procedure SetClearButton(const Value: Boolean); 
public 
    property ShowSearchEditClearButton: Boolean read GetClearButton write SetClearButton; 
end; 

{ TListViewHelper } 

function TListViewHelper.GetClearButton: Boolean; 
begin 
    Result := Self.FSearchEdit.StyleLookup = ''; // default style 
end; 

procedure TListViewHelper.SetClearButton(const Value: Boolean); 
begin 
    if Value then 
    Self.FSearchEdit.StyleLookup := '' // default style 
    else 
    Self.FSearchEdit.StyleLookup := 'editstyle'; 
end; 

在FORMCREATE我們可以稱之爲ListView1.ShowSearchEditClearButton := False;和清除按鈕不見了。

enter image description here

然而放大鏡圖標玻璃也消失了,因爲它不是我們設置爲StyleLookupeditstyle風格的一部分。

要取回圖標,我們必須創建自己的帶有放大鏡玻璃圖標但沒有清晰按鈕的樣式。

刪除一個TEDIT的形式,右鍵單擊它,然後選擇edit customized style

enter image description here

我們現在的範本:編輯和訪問控制佈局。

添加TActiveStyleObject到所有制結構,其重命名爲magnifierglass enter image description here

更改TActiveStyleObject的ActiveLink位圖。

在BitmapLinks-Editor中找到放大鏡玻璃圖標並選擇它(對於ActiveLink和SourceLink)。

enter image description here

現在文字將與圖標重疊。

enter image description here

要解決它,你對這些內容的左頁邊距(這是當前設置爲2px)更改爲類似20

enter image description here

enter image description here

您可以更高現在在樣式創建時以及窗體的樣式表中刪除窗體上的編輯。

打開樣式書並將新樣式的StyleName重命名爲searcheditstylenoclearbtn

enter image description here

保存它,並在你的classhelper功能改變

Self.FSearchEdit.StyleLookup := 'editstyle'; 

Self.FSearchEdit.StyleLookup := 'searcheditstylenoclearbtn'; 

現在清除按鈕不見了。

enter image description here

如果你不想去通過創建自己的searcheditstylenoclearbtn您可以將下面的代碼保存爲searcheditstylenoclearbtn.style並在範本:編輯器加載的麻煩。

enter image description here

object TStyleContainer 
    object TLayout 
    StyleName = 'searcheditstylenoclearbtn' 
    Position.X = 530.000000000000000000 
    Position.Y = 399.000000000000000000 
    Size.Width = 100.000000000000000000 
    Size.Height = 22.000000000000000000 
    Size.PlatformDefault = False 
    Visible = False 
    TabOrder = 0 
    object TActiveStyleObject 
     StyleName = 'background' 
     Align = Contents 
     SourceLookup = 'Windows 10 Desktopstyle.png' 
     Size.Width = 100.000000000000000000 
     Size.Height = 22.000000000000000000 
     Size.PlatformDefault = False 
     ActiveTrigger = Focused 
     ActiveLink = < 
     item 
      CapInsets.Left = 7.000000000000000000 
      CapInsets.Top = 7.000000000000000000 
      CapInsets.Right = 7.000000000000000000 
      CapInsets.Bottom = 7.000000000000000000 
      SourceRect.Left = 266.000000000000000000 
      SourceRect.Top = 81.000000000000000000 
      SourceRect.Right = 305.000000000000000000 
      SourceRect.Bottom = 110.000000000000000000 
     end> 
     SourceLink = < 
     item 
      CapInsets.Left = 7.000000000000000000 
      CapInsets.Top = 7.000000000000000000 
      CapInsets.Right = 7.000000000000000000 
      CapInsets.Bottom = 7.000000000000000000 
      SourceRect.Left = 225.000000000000000000 
      SourceRect.Top = 81.000000000000000000 
      SourceRect.Right = 264.000000000000000000 
      SourceRect.Bottom = 110.000000000000000000 
     end> 
     TouchAnimation.Link = <> 
    end 
    object TLayout 
     StyleName = 'content' 
     Align = Client 
     Locked = True 
     Margins.Left = 20.000000000000000000 
     Margins.Top = 2.000000000000000000 
     Margins.Right = 2.000000000000000000 
     Margins.Bottom = 2.000000000000000000 
     Size.Width = 6.000000000000000000 
     Size.Height = 18.000000000000000000 
     Size.PlatformDefault = False 
    end 
    object TLayout 
     StyleName = 'buttons' 
     Align = Right 
     Locked = True 
     Margins.Top = 2.000000000000000000 
     Margins.Right = 2.000000000000000000 
     Margins.Bottom = 2.000000000000000000 
     Position.X = 48.000000000000000000 
     Position.Y = 2.000000000000000000 
     Size.Width = 50.000000000000000000 
     Size.Height = 18.000000000000000000 
     Size.PlatformDefault = False 
    end 
    object TBrushObject 
     StyleName = 'foreground' 
     Brush.Color = claBlack 
    end 
    object TBrushObject 
     StyleName = 'selection' 
     Brush.Color = x7F2A96FF 
    end 
    object TFontObject 
     StyleName = 'font' 
    end 
    object TLabel 
     StyleName = 'prompt' 
     Locked = True 
     Opacity = 0.500000000000000000 
     Visible = False 
    end 
    object TActiveStyleObject 
     StyleName = 'magnifierglass' 
     Align = Left 
     CapMode = Tile 
     Margins.Top = 1.000000000000000000 
     SourceLookup = 'Windows 10 Desktopstyle.png' 
     Position.Y = 1.000000000000000000 
     Size.Width = 20.000000000000000000 
     Size.Height = 21.000000000000000000 
     Size.PlatformDefault = False 
     WrapMode = Center 
     ActiveTrigger = Pressed 
     ActiveLink = < 
     item 
      SourceRect.Left = 4.000000000000000000 
      SourceRect.Top = 358.000000000000000000 
      SourceRect.Right = 20.000000000000000000 
      SourceRect.Bottom = 374.000000000000000000 
     end> 
     SourceLink = < 
     item 
      SourceRect.Left = 4.000000000000000000 
      SourceRect.Top = 358.000000000000000000 
      SourceRect.Right = 20.000000000000000000 
      SourceRect.Bottom = 374.000000000000000000 
     end> 
     TouchAnimation.Link = <> 
    end 
    end 
end 
5

如果你不希望看到Clearbutton在所有搜索框在您的應用程序,你可以修改FMX.Searchbox.Style.pas

  1. 在FMX文件夾中找到FMX.Searchbox.Style.pas(默認情況下,C:\Program Files (x86)\Embarcadero\Studio\{your_version, e.g.17.0}\source\fmx\FMX.SearchBox.Style.pas和複製文件到您的項目文件夾(在your_application.dpr文件附近)
  2. 在新文件中查找並評論下一個行:

對於德爾福西雅圖:

procedure TStyledSearchBox.RealignButtons; 
begin 
    if (LeftLayout <> nil) and (FMagGlass <> nil) then 
    LeftLayout.Width := FMagGlass.Width; 
    if (ButtonsLayout <> nil) and (FClearButton <> nil) then 
    //if Model.Text.IsEmpty then 
     ButtonsLayout.Width := 0 
    //else 
    // ButtonsLayout.Width := FClearButton.Width; 
end; 

對於XE7:

procedure TStyledSearchBox.DoChangeTracking; 
begin 
    inherited; 
    if (ButtonsLayout <> nil) and (FClearButton <> nil) then 
    //if Model.Text.IsEmpty then 
     ButtonsLayout.Width := 0 
    //else 
    // ButtonsLayout.Width := FClearButton.Width; 
end; 

正如你看到的,代碼不是取決於德爾福版本太大的不同,對於其他版本,你可以自己找到它。

  1. 編譯和啓動應用程序。

這些代碼更改適用於所有平臺。

相關問題