为什么Cardview优于Appbar?
Why Cardview is over Appbar?
在尝试了很多次之后,我没有弄清楚如何解决我的问题。在我的布局中,我有一个带有应用栏的导航抽屉,里面有一个带有相对布局的 Cardview。这是我在布局中编码 xml 时预览中的屏幕:
当我在模拟器或 phone 上启动应用程序时,我有这个:
My Cardview 出现在 Appbar 上。完整布局代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:theme="@style/AppTheme.NoActionBar"
tools:context=".activities.display.MainActivity"
tools:openDrawer="">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="70dp"
app:cardBackgroundColor="#80FFFFFF"
app:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:id="@+id/newresearch_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Rechercher"
android:textColor="#424242"
android:textSize="18sp"
android:textStyle="bold" />
<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/newresearch_title"
android:layout_margin="5dp"
android:background="@drawable/custom_edittext"
android:hint="Prix min"
android:inputType="number"
android:padding="5dp"
android:textColor="@android:color/black" />
<EditText
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/edit1"
android:layout_margin="5dp"
android:background="@drawable/custom_edittext"
android:hint="Prix max"
android:inputType="number"
android:padding="5dp"
android:textColor="@android:color/black" />
<Button
android:id="@+id/bouton_de_recherche"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/edit2"
android:layout_centerHorizontal="true"
android:layout_marginEnd="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="25dp"
android:background="@drawable/custom_button"
android:text="Rechercher"
android:textColor="#424242"
android:textSize="12sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<include
layout="@layout/navdraw_appbar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/navdraw_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
感谢您的帮助!
编辑:在下方添加了 navdraw_appbar 的代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context=".activities.display.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
试试这个
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:theme="@style/AppTheme.NoActionBar"
tools:context=".activities.display.MainActivity"
tools:openDrawer="">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<include
layout="@layout/navdraw_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:id="@+id/rltv_allcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="70dp"
app:cardBackgroundColor="#80FFFFFF"
app:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:id="@+id/newresearch_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Rechercher"
android:textColor="#424242"
android:textSize="18sp"
android:textStyle="bold" />
<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/newresearch_title"
android:layout_margin="5dp"
android:background="@drawable/custom_edittext"
android:hint="Prix min"
android:inputType="number"
android:padding="5dp"
android:textColor="@android:color/black" />
<EditText
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/edit1"
android:layout_margin="5dp"
android:background="@drawable/custom_edittext"
android:hint="Prix max"
android:inputType="number"
android:padding="5dp"
android:textColor="@android:color/black" />
<Button
android:id="@+id/bouton_de_recherche"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/edit2"
android:layout_centerHorizontal="true"
android:layout_marginEnd="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="25dp"
android:background="@drawable/custom_button"
android:text="Rechercher"
android:textColor="#424242"
android:textSize="12sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/navdraw_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
希望对您有所帮助。
我最近遇到了类似的问题。假设您的 activity 扩展了 AppCompatActivity,您是否在 activity 中设置了工具栏?
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
在尝试了很多次之后,我没有弄清楚如何解决我的问题。在我的布局中,我有一个带有应用栏的导航抽屉,里面有一个带有相对布局的 Cardview。这是我在布局中编码 xml 时预览中的屏幕:
当我在模拟器或 phone 上启动应用程序时,我有这个:
My Cardview 出现在 Appbar 上。完整布局代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:theme="@style/AppTheme.NoActionBar"
tools:context=".activities.display.MainActivity"
tools:openDrawer="">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="70dp"
app:cardBackgroundColor="#80FFFFFF"
app:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:id="@+id/newresearch_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Rechercher"
android:textColor="#424242"
android:textSize="18sp"
android:textStyle="bold" />
<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/newresearch_title"
android:layout_margin="5dp"
android:background="@drawable/custom_edittext"
android:hint="Prix min"
android:inputType="number"
android:padding="5dp"
android:textColor="@android:color/black" />
<EditText
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/edit1"
android:layout_margin="5dp"
android:background="@drawable/custom_edittext"
android:hint="Prix max"
android:inputType="number"
android:padding="5dp"
android:textColor="@android:color/black" />
<Button
android:id="@+id/bouton_de_recherche"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/edit2"
android:layout_centerHorizontal="true"
android:layout_marginEnd="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="25dp"
android:background="@drawable/custom_button"
android:text="Rechercher"
android:textColor="#424242"
android:textSize="12sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<include
layout="@layout/navdraw_appbar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/navdraw_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
感谢您的帮助!
编辑:在下方添加了 navdraw_appbar 的代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context=".activities.display.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
试试这个
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:theme="@style/AppTheme.NoActionBar"
tools:context=".activities.display.MainActivity"
tools:openDrawer="">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<include
layout="@layout/navdraw_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:id="@+id/rltv_allcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="70dp"
app:cardBackgroundColor="#80FFFFFF"
app:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:id="@+id/newresearch_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Rechercher"
android:textColor="#424242"
android:textSize="18sp"
android:textStyle="bold" />
<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/newresearch_title"
android:layout_margin="5dp"
android:background="@drawable/custom_edittext"
android:hint="Prix min"
android:inputType="number"
android:padding="5dp"
android:textColor="@android:color/black" />
<EditText
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/edit1"
android:layout_margin="5dp"
android:background="@drawable/custom_edittext"
android:hint="Prix max"
android:inputType="number"
android:padding="5dp"
android:textColor="@android:color/black" />
<Button
android:id="@+id/bouton_de_recherche"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/edit2"
android:layout_centerHorizontal="true"
android:layout_marginEnd="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="25dp"
android:background="@drawable/custom_button"
android:text="Rechercher"
android:textColor="#424242"
android:textSize="12sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/navdraw_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
希望对您有所帮助。
我最近遇到了类似的问题。假设您的 activity 扩展了 AppCompatActivity,您是否在 activity 中设置了工具栏?
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);