【问题标题】:NestedScrollView with Toolbar带有工具栏的 NestedScrollView
【发布时间】:2019-12-02 11:09:17
【问题描述】:

所以我在 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>

【问题讨论】:

    标签: android android-recyclerview scrollview toolbar


    【解决方案1】:

    太简单了。在要显示工具栏的 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 中以编程方式绑定工具栏。

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

    【讨论】:

      【解决方案2】:

      类似这样的Xml结构应该可以解决问题

      <LinearLayout>
      <Toolbar/>
      <NestedScrollView>
      //one parent view inside nestedscrollview
      <AnylayoutType>
      // other required views
      </AnylayoutType>
      </NestedScrollView>
      <LinearLayout/>
      

      使用这种方法,您的工具栏不会移动,菜单选项将始终可用,并且嵌套滚动视图将根据需要工作。

      如果您确定您的视图层次结构没有嵌套,最好在嵌套滚动视图中使用 LinearLayout

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-14
        相关资源
        最近更新 更多