这个奇怪的白色矩形在我的工具栏后面是什么?

huangapple go评论106阅读模式
英文:

What's this strange white rectangle behind my toolbar?

问题

我一直在努力摆脱它,但一整天都没有成功。尝试为它周围的每个视图和布局设置透明背景颜色。还尝试减小包含它的布局和视图的大小。那些锐利的白色边缘仍然存在。它们是什么?还有,这是文本占位符,网站说有太多的代码和太少的文本。"嗨,文本占位符,你还好吗?"

  • activity_main.xml:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MainActivity">
  9. <include
  10. android:id="@+id/mainToolbar"
  11. layout="@layout/toolbar"
  12. app:layout_constraintEnd_toEndOf="parent"
  13. app:layout_constraintStart_toStartOf="parent"
  14. app:layout_constraintTop_toTopOf="parent" />
  15. <androidx.coordinatorlayout.widget.CoordinatorLayout
  16. android:layout_width="0dp"
  17. android:layout_height="0dp"
  18. app:layout_constraintBottom_toBottomOf="parent"
  19. app:layout_constraintEnd_toEndOf="parent"
  20. app:layout_constraintStart_toStartOf="parent"
  21. app:layout_constraintTop_toBottomOf="@+id/mainToolbar">
  22. <com.google.android.material.appbar.AppBarLayout
  23. android:id="@+id/tab_anim_appbar"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:layout_gravity="center|top"
  27. android:layout_marginLeft="20dp"
  28. android:layout_marginRight="20dp"
  29. android:background="#00FFFFFF">
  30. <com.google.android.material.appbar.MaterialToolbar
  31. android:id="@+id/searchToolbar"
  32. style="@style/Widget.MaterialComponents.Toolbar.Primary"
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content"
  35. android:layout_gravity="center|top"
  36. android:background="#A106A1"
  37. android:elevation="2dp"
  38. app:contentInsetLeft="0dp"
  39. app:contentInsetStart="0dp"
  40. app:layout_scrollFlags="scroll|enterAlways">
  41. <!-- 用于捕捉焦点的虚拟布局 -->
  42. <LinearLayout
  43. android:layout_width="0px"
  44. android:layout_height="0px"
  45. android:focusable="true"
  46. android:focusableInTouchMode="true" />
  47. <androidx.appcompat.widget.SearchView
  48. android:id="@+id/searchView"
  49. android:layout_width="match_parent"
  50. android:layout_height="wrap_content"
  51. android:animateLayoutChanges="true"
  52. android:layoutDirection="rtl"
  53. app:iconifiedByDefault="true"
  54. android:layout_marginLeft="30dp"
  55. android:layout_marginRight="30dp" />
  56. </com.google.android.material.appbar.MaterialToolbar>
  57. </com.google.android.material.appbar.AppBarLayout>
  58. <include layout="@layout/content_scrolling" />
  59. </androidx.coordinatorlayout.widget.CoordinatorLayout>
  60. </androidx.constraintlayout.widget.ConstraintLayout>
  • 代码中的工具栏:
  1. // 使工具栏的边缘变成圆角
  2. MaterialToolbar searchToolbar = findViewById(R.id.searchToolbar);
  3. MaterialShapeDrawable searchbarBackground = (MaterialShapeDrawable) searchToolbar.getBackground();
  4. searchbarBackground.setShapeAppearanceModel(
  5. searchbarBackground.getShapeAppearanceModel()
  6. .toBuilder()
  7. .setBottomRightCorner(CornerFamily.ROUNDED, 100)
  8. .setBottomLeftCorner(CornerFamily.ROUNDED, 100)
  9. .build()
  10. );
  11. // 添加搜索功能的代码
  12. SearchView searchView = findViewById(R.id.searchView);
  13. searchView.setImeOptions(EditorInfo.IME_ACTION_DONE); // 以便键盘上的按钮是一个检查标志
  14. searchView.setQueryHint("搜索...");
  15. searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
  16. @Override
  17. public boolean onQueryTextSubmit(String query) {
  18. return false;
  19. }
  20. @Override
  21. public boolean onQueryTextChange(String newText) {
  22. ItemAdapter.getFilter().filter(newText);
  23. return false;
  24. }
  25. });
英文:

这个奇怪的白色矩形在我的工具栏后面是什么?

I've been trying to get rid of it all day to no avail. Tried giving every view and layout around it an invisible background color. Also tried reducing the size of the layouts and views it's in. Those sharp white edges persist. What are they? Also hi this is text filler, website says there's too much code and too little text. "Hi text filler, you good?"

  • activity_main.xml:

    1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    2. &lt;androidx.constraintlayout.widget.ConstraintLayout
    3. xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    4. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    5. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    6. android:layout_width=&quot;match_parent&quot;
    7. android:layout_height=&quot;match_parent&quot;
    8. tools:context=&quot;.MainActivity&quot;&gt;
    9. &lt;include
    10. android:id=&quot;@+id/mainToolbar&quot;
    11. layout=&quot;@layout/toolbar&quot;
    12. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    13. app:layout_constraintStart_toStartOf=&quot;parent&quot;
    14. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
    15. &lt;androidx.coordinatorlayout.widget.CoordinatorLayout
    16. android:layout_width=&quot;0dp&quot;
    17. android:layout_height=&quot;0dp&quot;
    18. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
    19. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    20. app:layout_constraintStart_toStartOf=&quot;parent&quot;
    21. app:layout_constraintTop_toBottomOf=&quot;@+id/mainToolbar&quot;&gt;
    22. &lt;com.google.android.material.appbar.AppBarLayout
    23. android:id=&quot;@+id/tab_anim_appbar&quot;
    24. android:layout_width=&quot;match_parent&quot;
    25. android:layout_height=&quot;wrap_content&quot;
    26. android:layout_gravity=&quot;center|top&quot;
    27. android:layout_marginLeft=&quot;20dp&quot;
    28. android:layout_marginRight=&quot;20dp&quot;
    29. android:background=&quot;#00FFFFFF&quot;&gt;
    30. &lt;com.google.android.material.appbar.MaterialToolbar
    31. android:id=&quot;@+id/searchToolbar&quot;
    32. style=&quot;@style/Widget.MaterialComponents.Toolbar.Primary&quot;
    33. android:layout_width=&quot;match_parent&quot;
    34. android:layout_height=&quot;wrap_content&quot;
    35. android:layout_gravity=&quot;center|top&quot;
    36. android:background=&quot;#A106A1&quot;
    37. android:elevation=&quot;2dp&quot;
    38. app:contentInsetLeft=&quot;0dp&quot;
    39. app:contentInsetStart=&quot;0dp&quot;
    40. app:layout_scrollFlags=&quot;scroll|enterAlways&quot;&gt;
    41. &lt;!-- dummy to catch focus --&gt;
    42. &lt;LinearLayout
    43. android:layout_width=&quot;0px&quot;
    44. android:layout_height=&quot;0px&quot;
    45. android:focusable=&quot;true&quot;
    46. android:focusableInTouchMode=&quot;true&quot; /&gt;
    47. &lt;androidx.appcompat.widget.SearchView
    48. android:id=&quot;@+id/searchView&quot;
    49. android:layout_width=&quot;match_parent&quot;
    50. android:layout_height=&quot;wrap_content&quot;
    51. android:animateLayoutChanges=&quot;true&quot;
    52. android:layoutDirection=&quot;rtl&quot;
    53. app:iconifiedByDefault=&quot;true&quot;
    54. android:layout_marginLeft=&quot;30dp&quot;
    55. android:layout_marginRight=&quot;30dp&quot;/&gt;
    56. &lt;/com.google.android.material.appbar.MaterialToolbar&gt;
    57. &lt;/com.google.android.material.appbar.AppBarLayout&gt;
    58. &lt;include layout=&quot;@layout/content_scrolling&quot;/&gt;
    59. &lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;
    60. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  • The toolbar in code:

    1. //code to make the toolbar edges round
    2. MaterialToolbar searchToolbar = findViewById(R.id.searchToolbar);
    3. MaterialShapeDrawable searchbarBackground = (MaterialShapeDrawable) searchToolbar.getBackground();
    4. searchbarBackground.setShapeAppearanceModel(
    5. searchbarBackground.getShapeAppearanceModel()
    6. .toBuilder()
    7. .setBottomRightCorner(CornerFamily.ROUNDED,100)
    8. .setBottomLeftCorner(CornerFamily.ROUNDED,100)
    9. .build()
    10. );
    11. //code to add search function
    12. SearchView searchView = findViewById(R.id.searchView);
    13. searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);//so that button on keyboard is a check
    14. searchView.setQueryHint(&quot;Search...&quot;);
    15. searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    16. @Override
    17. public boolean onQueryTextSubmit(String query) {
    18. return false;
    19. }
    20. @Override
    21. public boolean onQueryTextChange(String newText) {
    22. ItemAdapter.getFilter().filter(newText);
    23. return false;
    24. }
    25. });

答案1

得分: 3

这是AppBarLayout的阴影。使用:

  1. <com.google.android.material.appbar.AppBarLayout
  2. app:elevation="0dp"
  3. android:background="@android:color/transparent"

这个奇怪的白色矩形在我的工具栏后面是什么?

英文:

It is the shadow of the AppBarLayout. Use:

  1. &lt;com.google.android.material.appbar.AppBarLayout
  2. app:elevation=&quot;0dp&quot;
  3. android:background=&quot;@android:color/transparent&quot;

这个奇怪的白色矩形在我的工具栏后面是什么?

huangapple
  • 本文由 发表于 2020年8月3日 22:32:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63231402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定