Android 如何通过单击ImageView在MaterialCardView顶部显示/设计导航抽屉布局
我试图通过点击菜单图标ImageView来设计从右向左拖动导航抽屉布局的布局 功能正常工作,没有任何问题。但是,从用户界面的角度来看,导航抽屉布局在MaterialCardView下方可见,并根据卡片大小包装内容。如何将导航抽屉置于卡片顶部和全屏上 下面是我的布局文件: activity_main.xmlAndroid 如何通过单击ImageView在MaterialCardView顶部显示/设计导航抽屉布局,android,android-layout,android-navigationview,Android,Android Layout,Android Navigationview,我试图通过点击菜单图标ImageView来设计从右向左拖动导航抽屉布局的布局 功能正常工作,没有任何问题。但是,从用户界面的角度来看,导航抽屉布局在MaterialCardView下方可见,并根据卡片大小包装内容。如何将导航抽屉置于卡片顶部和全屏上 下面是我的布局文件: activity_main.xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.w
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<com.google.android.material.card.MaterialCardView
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardBackgroundColor="#1835d9"
style="@style/CustomCardCorners"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<ImageView
android:id="@+id/navigation_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/menu"
android:layout_gravity="top|end"
android:contentDescription="@string/app_name"/>
</com.google.android.material.card.MaterialCardView>
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="right"
android:layout_gravity="right"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true">
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
app:menu="@menu/my_navigation_items"/>
</androidx.drawerlayout.widget.DrawerLayout>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
如果您确定抽屉布局在
材料卡片视图下,那么这肯定是一个“提升”问题
基于Material.io,导航抽屉布局默认为16dp,而materialcardwiew
则为1dp-8dp
我猜您没有使用该材质的DrawerLayout
组件,或者他们称之为NavigationView
您可以看到如何实现它,在这里,视图的时间顺序引起了问题。如中所述,正确的年表为
<DrawerLayout>
<RootView> <!--root view of main content-->
<!--other child views-->
</RootView>
<NavigationView/> <!--view for navigation menu-->
</DrawerLayout>
因此,请尝试以下布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_gravity="right"
android:fitsSystemWindows="true"
tools:openDrawer="right">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.card.MaterialCardView
android:id="@+id/toolbar"
style="@style/CustomCardCorners"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
app:cardBackgroundColor="#1835d9">
<ImageView
android:id="@+id/navigation_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|end"
android:contentDescription="@string/app_name"
android:onClick="onClick"
android:src="@drawable/ic_launcher_background" />
</com.google.android.material.card.MaterialCardView>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
app:menu="@menu/my_navigation_items" />
</androidx.drawerlayout.widget.DrawerLayout>
尝试使用立面和过渡-z@Sekiro-我试过你的密码。它正在按预期工作。但是,操作栏也随之显示。虽然我以编程方式隐藏标题栏和动作栏,但它正在显示。有没有办法做到这一点,使全屏?谢谢你的回答。但是,如果我使用高程并将其设置为16dp,则MaterialCardView的上部不可见。因此,ImageView没有显示。另外,我确信抽屉布局在卡的下面。您可以看到我的java文件,我正在使用Toast在导航布局中显示项目的id,并且它正在按预期工作。您是否可以使用屏幕截图更新您的问题?我已经编辑了一个问题并添加了图像。请调查一下it@Eswar如上所示重新排列时间顺序解决了您的问题,在我的设备上按预期工作,快乐编码:)我已经尝试了您的代码。它正在按预期工作。但是,操作栏也随之显示。虽然我以编程方式隐藏标题栏和动作栏,但它正在显示。有没有办法做到这一点并使其全屏显示?只需将应用主题更改为NoActionBar
,然后删除this.supportActionBar!!。隐藏()
从您的activity@Sekiro-现在工作很好。谢谢你,伙计,成功了,接受我的回答,伙计
public class MainActivity extends AppCompatActivity implements View.OnClickListener,
NavigationView.OnNavigationItemSelectedListener {
ImageView imageView;
DrawerLayout drawerLayout;
NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//will hide the title
this.getSupportActionBar().hide();
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.navigation_icon);
imageView.setOnClickListener(MainActivity.this);
setUp();
}
private void setUp() {
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.navigation);
navigationView.setNavigationItemSelectedListener(MainActivity.this);
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.navigation_icon:
drawerLayout.openDrawer(Gravity.RIGHT);
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Toast.makeText(MainActivity.this, ""+ item.getItemId(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
return true;
}
}
<DrawerLayout>
<RootView> <!--root view of main content-->
<!--other child views-->
</RootView>
<NavigationView/> <!--view for navigation menu-->
</DrawerLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_gravity="right"
android:fitsSystemWindows="true"
tools:openDrawer="right">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.card.MaterialCardView
android:id="@+id/toolbar"
style="@style/CustomCardCorners"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
app:cardBackgroundColor="#1835d9">
<ImageView
android:id="@+id/navigation_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|end"
android:contentDescription="@string/app_name"
android:onClick="onClick"
android:src="@drawable/ic_launcher_background" />
</com.google.android.material.card.MaterialCardView>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
app:menu="@menu/my_navigation_items" />
</androidx.drawerlayout.widget.DrawerLayout>