我想将 TEditButton 动态地放在 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)?

最佳答案

android - 如何删除 ListView 搜索框中的清除按钮?-LMLPHP

SearchBox是TEdit的后代,并使用FireMonkey控件样式。

在您的窗体上放置一个TEdit并打开其StyleLookup属性:

android - 如何删除 ListView 搜索框中的清除按钮?-LMLPHP

您可以看到已经有不同的样式。
因此,我们要更改SearchBox的StyleLookup。

由于SearchBox是ListView控件的私有(private)成员(FSearchEdit),因此您没有直接访问它的方法。

您可以创建属于TListView(TListViewBase)的后代的ListView控件,也可以使用类帮助器。我选择后者。

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;,清除按钮不见了。

android - 如何删除 ListView 搜索框中的清除按钮?-LMLPHP

但是,放大镜图标也消失了,因为它不是我们设置为editstyleStyleLookup样式的一部分。

为了使图标恢复原状,我们必须创建自己的样式,该样式具有放大镜图标但没有清除按钮。

将TEdit放到窗体上,右键单击它,然后选择edit customized style:

android - 如何删除 ListView 搜索框中的清除按钮?-LMLPHP

现在,我们处于StyleBook编辑器中,并可以访问控件布局。

将TActiveStyleObject添加到结构中,将其重命名为magnifierglass android - 如何删除 ListView 搜索框中的清除按钮?-LMLPHP

更改TActiveStyleObject的ActiveLink位图。

在BitmapLinks-Editor中,找到放大镜图标并选择它(对于ActiveLink和SourceLink)。

android - 如何删除 ListView 搜索框中的清除按钮?-LMLPHP

您的文本现在将与图标重叠。

android - 如何删除 ListView 搜索框中的清除按钮?-LMLPHP

要解决此问题,您必须将内容的左边距(当前设置为2px)更改为更高的值(例如20)。

android - 如何删除 ListView 搜索框中的清除按钮?-LMLPHP

android - 如何删除 ListView 搜索框中的清除按钮?-LMLPHP

现在,您可以在创建样式时以及在表单的StyleBook中删除表单上的编辑。

打开StyleBook,然后将新样式的StyleName重命名为searcheditstylenoclearbtn

android - 如何删除 ListView 搜索框中的清除按钮?-LMLPHP

保存并在您的classhelper函数中进行更改
Self.FSearchEdit.StyleLookup := 'editstyle';


Self.FSearchEdit.StyleLookup := 'searcheditstylenoclearbtn';

现在清除按钮消失了。

android - 如何删除 ListView 搜索框中的清除按钮?-LMLPHP

如果您不想经历创建自己的searcheditstylenoclearbtn的麻烦,可以将以下代码另存为searcheditstylenoclearbtn.style并将其加载到StyleBook编辑器中。

android - 如何删除 ListView 搜索框中的清除按钮?-LMLPHP
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

10-07 19:25