java - 尽管设置了属性,但 AppCompat 工具栏高度缺失(使用 RecyclerView)
问题描述
出于某种原因,我Toolbar
不会为海拔投下阴影。我什至尝试过使用Java,但它仍然无法正常工作。
关于为什么这没有按预期执行的任何想法?我的应用不支持 pre-Lollipop(最低 API 为 24)。
活动 XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/masterToolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize">
<include layout="@layout/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"/>
</LinearLayout>
<RelativeLayout
android:id="@+id/master_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
工具栏 XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_toolbar"
android:elevation="4dp"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:minHeight="?android:attr/actionBarSize">
<LinearLayout
android:id="@+id/mytoolbar_textlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/mytoolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/TextAppearance.Material.Widget.ActionBar.Title"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
活动课
public class MyActivity extends AppCompatActivity {
private Boolean mCurrentValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.md);
}
@Override
protected void onStart() {
super.onStart();
setContentView(R.layout.md);
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
Boolean mNewValue = mSharedPreferences.getBoolean("my_preference", false);
if (mCurrentValue != mNewValue) {
recreate();
}
// ... do other stuff here
Resources r = getBaseContext().getResources();
int fourDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, r.getDisplayMetrics());
final String toolbarColour = "#EE7C0E";
Toolbar mMasterToolbar = findViewById(R.id.my_toolbar);
setSupportActionBar(mMasterToolbar);
mMasterToolbar.setBackgroundColor(Color.parseColor(toolbarColour));
mMasterToolbar.setElevation(fourDp);
mMasterToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
final TextView mTitle = this.findViewById(R.id.toolbar_1line_title);
mTitle.setText(getString(R.string.london_overground));
FragmentMain newFragment = new FragmentMain();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.master_container, newFragment);
transaction.commit();
}
}
解决方案
你android:background
在你的Toolbar
.
例子:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_toolbar"
android:elevation="4dp"
android:background="?attr/colorPrimary"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:minHeight="?android:attr/actionBarSize">
注意:为了将高程应用到任何视图,它需要有背景。
编辑:
由于您的应用具有最小 SDK 24,您可以使用该translationZ
属性。
推荐阅读
- css - 如何使 HTML 小部件的高度适应 R Shiny 中窗口的高度?
- java - 为什么在 Android Studio 中更新或删除 SQLite 数据库中的项目时使用“new String[] {}”?
- c++ - 从 const 字符串转换为 const u8_t 时出现问题 *
- android - 如何从 Adapter 类中动态获取 R.drawable.{var}?
- python - 未找到带有参数 '('',)' 的 'add_review' 的反向操作。尝试了 1 种模式:['addreview/(?P
[0-9]+)/$'] - amazon-s3 - 无法使用 localstack 触发 AWS Lambda S3 触发器?
- java - Spring Batch - 处理器步骤期间的事务性 REQUIRES_NEW
- jenkins - 詹金斯“在步骤中找不到这样的 DSL 方法‘步骤’”
- ios - Flutter:获取所有 Near by WiFi 网络的列表
- list - 是否有在ansible中汇总列表内容的快捷方式?