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

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

What's this strange white rectangle behind my toolbar?

问题

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

  • activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <include
        android:id="@+id/mainToolbar"
        layout="@layout/toolbar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/mainToolbar">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/tab_anim_appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center|top"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:background="#00FFFFFF">

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/searchToolbar"
                style="@style/Widget.MaterialComponents.Toolbar.Primary"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center|top"
                android:background="#A106A1"
                android:elevation="2dp"
                app:contentInsetLeft="0dp"
                app:contentInsetStart="0dp"
                app:layout_scrollFlags="scroll|enterAlways">

                <!-- 用于捕捉焦点的虚拟布局 -->
                <LinearLayout
                    android:layout_width="0px"
                    android:layout_height="0px"
                    android:focusable="true"
                    android:focusableInTouchMode="true" />

                <androidx.appcompat.widget.SearchView
                    android:id="@+id/searchView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:animateLayoutChanges="true"
                    android:layoutDirection="rtl"
                    app:iconifiedByDefault="true"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="30dp" />

            </com.google.android.material.appbar.MaterialToolbar>

        </com.google.android.material.appbar.AppBarLayout>

        <include layout="@layout/content_scrolling" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
  • 代码中的工具栏:
// 使工具栏的边缘变成圆角
MaterialToolbar searchToolbar = findViewById(R.id.searchToolbar);
MaterialShapeDrawable searchbarBackground = (MaterialShapeDrawable) searchToolbar.getBackground();
searchbarBackground.setShapeAppearanceModel(
        searchbarBackground.getShapeAppearanceModel()
                .toBuilder()
                .setBottomRightCorner(CornerFamily.ROUNDED, 100)
                .setBottomLeftCorner(CornerFamily.ROUNDED, 100)
                .build()
);

// 添加搜索功能的代码
SearchView searchView = findViewById(R.id.searchView);
searchView.setImeOptions(EditorInfo.IME_ACTION_DONE); // 以便键盘上的按钮是一个检查标志
searchView.setQueryHint("搜索...");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        ItemAdapter.getFilter().filter(newText);
        return false;
    }
});
英文:

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

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:

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    &lt;androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot;&gt;
    &lt;include
    android:id=&quot;@+id/mainToolbar&quot;
    layout=&quot;@layout/toolbar&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
    &lt;androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width=&quot;0dp&quot;
    android:layout_height=&quot;0dp&quot;
    app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toBottomOf=&quot;@+id/mainToolbar&quot;&gt;
    &lt;com.google.android.material.appbar.AppBarLayout
    android:id=&quot;@+id/tab_anim_appbar&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_gravity=&quot;center|top&quot;
    android:layout_marginLeft=&quot;20dp&quot;
    android:layout_marginRight=&quot;20dp&quot;
    android:background=&quot;#00FFFFFF&quot;&gt;
    &lt;com.google.android.material.appbar.MaterialToolbar
    android:id=&quot;@+id/searchToolbar&quot;
    style=&quot;@style/Widget.MaterialComponents.Toolbar.Primary&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_gravity=&quot;center|top&quot;
    android:background=&quot;#A106A1&quot;
    android:elevation=&quot;2dp&quot;
    app:contentInsetLeft=&quot;0dp&quot;
    app:contentInsetStart=&quot;0dp&quot;
    app:layout_scrollFlags=&quot;scroll|enterAlways&quot;&gt;
    &lt;!-- dummy to catch focus --&gt;
    &lt;LinearLayout
    android:layout_width=&quot;0px&quot;
    android:layout_height=&quot;0px&quot;
    android:focusable=&quot;true&quot;
    android:focusableInTouchMode=&quot;true&quot; /&gt;
    &lt;androidx.appcompat.widget.SearchView
    android:id=&quot;@+id/searchView&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:animateLayoutChanges=&quot;true&quot;
    android:layoutDirection=&quot;rtl&quot;
    app:iconifiedByDefault=&quot;true&quot;
    android:layout_marginLeft=&quot;30dp&quot;
    android:layout_marginRight=&quot;30dp&quot;/&gt;
    &lt;/com.google.android.material.appbar.MaterialToolbar&gt;
    &lt;/com.google.android.material.appbar.AppBarLayout&gt;
    &lt;include layout=&quot;@layout/content_scrolling&quot;/&gt;
    &lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;
    &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
    
  • The toolbar in code:

        //code to make the toolbar edges round
    MaterialToolbar searchToolbar = findViewById(R.id.searchToolbar);
    MaterialShapeDrawable searchbarBackground = (MaterialShapeDrawable) searchToolbar.getBackground();
    searchbarBackground.setShapeAppearanceModel(
    searchbarBackground.getShapeAppearanceModel()
    .toBuilder()
    .setBottomRightCorner(CornerFamily.ROUNDED,100)
    .setBottomLeftCorner(CornerFamily.ROUNDED,100)
    .build()
    );
    //code to add search function
    SearchView searchView = findViewById(R.id.searchView);
    searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);//so that button on keyboard is a check
    searchView.setQueryHint(&quot;Search...&quot;);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
    return false;
    }
    @Override
    public boolean onQueryTextChange(String newText) {
    ItemAdapter.getFilter().filter(newText);
    return false;
    }
    });
    

答案1

得分: 3

这是AppBarLayout的阴影。使用:

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

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

英文:

It is the shadow of the AppBarLayout. Use:

&lt;com.google.android.material.appbar.AppBarLayout
app:elevation=&quot;0dp&quot;
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:

确定