gpt4 book ai didi

android - 带有工具栏的 NestedScrollView

转载 作者:行者123 更新时间:2023-11-29 18:25:45 25 4
gpt4 key购买 nike

所以我在 styles.xml 中为 AppTheme NoActionBar 设置,因为我只希望在几个 Activites 中使用工具栏,并且我还创建了包含两个项目的 post_details_menu。但是工具栏根本不显示。

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

然后在 Manifest.xml 中,我有主题:AppTheme

<application
android:theme="@style/AppTheme"
...>
</application>

然后我创建了 my_toolbar:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.appcompat.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"/>
</LinearLayout>

在我的 PostDetailsActivity 中,我想使用我的工具栏:

public class PostDetailActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_detail);

Toolbar toolbar = findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.post_details_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch(item.getItemId()){
case R.id.post_Edit:
showMessage("post_edit clicked");
return true;
case R.id.post_Delete:
showMessage("post_delete clicked");
return true;
default:
return super.onOptionsItemSelected(item);
}
}

最后这是我的 ActivityPostDetails 布局:

<androidx.core.widget.NestedScrollView
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=".PostDetailActivity"
android:background="#fff">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView ... />

<TextView ... />

<androidx.recyclerview.widget.RecyclerView .../>

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>

最佳答案

太简单了。在要显示工具栏的 XML 文件中设置应用栏布局。

检查下面的 XML 代码:-

<androidx.core.widget.NestedScrollView
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=".PostDetailActivity"
android:background="#fff">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/AppTheme.ActionBar"
app:navigationIcon="@drawable/ic_back"
app:subtitleTextAppearance="@style/CustomSubTitleTextAppearance"
app:titleTextColor="@color/black" />

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

<ImageView ... />

<TextView ... />

<androidx.recyclerview.widget.RecyclerView .../>

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>

在您的 java 代码中,您必须在 PostDetailsActivity.java 中以编程方式绑定(bind)工具栏。

toolbar = findViewById(R.id.toolbar)
toolbar!!.setNavigationIcon(R.drawable.ic_back)
if (toolbar != null) {
setSupportActionBar(toolbar)
}

关于android - 带有工具栏的 NestedScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59137688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com