英文:
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:
<?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"> <!-- dummy to catch focus --> <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>
-
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("Search..."); 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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论