我正在制作添加了活动片段的应用程序。现在,我在片段部分添加了搜索栏,因此我想在单击图像时打开导航抽屉(处于活动状态)和操作菜单。
片段看起来像这样-image here
片段XML-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:id="@+id/swiper"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.cardview.widget.CardView
app:cardUseCompatPadding="true"
android:layout_margin="8dp"
app:cardCornerRadius="8dp"
android:layout_width="match_parent"
android:layout_height="60dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:src="@drawable/ic_menu_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/edit_search"
android:layout_width="297dp"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="@android:color/transparent"
android:cursorVisible="false"
android:elevation="5dp"
android:fontFamily="@font/robolightfont"
android:gravity="center"
android:hint="Search"
android:inputType="text"
android:minWidth="100dp"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textStyle="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="@+id/imgmore"
app:layout_constraintLeft_toRightOf="@+id/imageView2"
app:layout_constraintStart_toEndOf="@+id/imageView2"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imgmore"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:src="@drawable/ic_more_vert_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<!-- <EditText
android:padding="10dp"
android:background="@drawable/rounddesign"
android:layout_margin="12dp"
android:elevation="5dp"
android:fontFamily="@font/robotolight"
android:id="@+id/edit_search"
android:textColorHint="@color/white"
android:textColor="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:gravity="center"
android:textStyle="normal"
android:cursorVisible="false"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle"
android:hint="Search"
android:visibility="visible"/>-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:background="@color/mainback"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</LinearLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</RelativeLayout>
主要活动XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".ui.NewMainActivity"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<ImageView
android:id="@+id/logimage"
android:layout_width="55dp"
android:layout_height="55dp"
android:visibility="gone"
android:background="@drawable/ic_access_time_black_24dp"
android:layout_margin="20dp"
android:layout_gravity="center" />
<TextView
android:id="@+id/logtxt"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="10dp"
android:layout_gravity="center"
android:gravity="center"
android:fontFamily="@font/robolightfont"
android:layout_marginTop="30dp"
android:textSize="20dp"
android:text="It Seems, No Notification Has Been Logged Yet !\n\nCome Back Once You Start Receiving Notification"/>
<Button
android:id="@+id/button_check"
android:visibility="gone"
android:gravity="center"
android:textColor="@color/mainback"
android:layout_margin="14dp"
android:fontFamily="@font/robofont"
android:background="@drawable/roundbtndesign"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Retry"/>
<ImageView
android:id="@+id/acimage"
android:layout_width="80dp"
android:layout_gravity="center"
android:layout_margin="30dp"
android:layout_height="80dp"
android:visibility="gone"
android:background="@drawable/ic_tag_faces_black_24dp"/>
<TextView
android:id="@+id/actitle"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification Access"
android:layout_gravity="center"
android:fontFamily="@font/fontmossemi"
android:layout_margin="10dp"
android:visibility="gone"
android:gravity="center"/>
<TextView
android:id="@+id/acsubtext"
android:visibility="gone"
android:textSize="15sp"
android:textColor="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To Get Started, Enable Notification Access For Notification Log"
android:layout_gravity="center"
android:fontFamily="@font/robolightfont"
android:layout_margin="10dp"
android:gravity="center"/>
<Button
android:id="@+id/button_allow"
android:visibility="gone"
android:gravity="center"
android:background="@drawable/roundbtndesign"
android:textColor="@color/mainback"
android:layout_margin="24dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Allow Permission"/>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
现在如何从fragment.java代码中打开操作菜单和抽屉菜单。请帮忙
答案 0 :(得分:1)
在活动中创建一个公共方法,这将打开导航抽屉并从片段中调用该方法。
活动代码:
Copy
在您的片段图片中,点击代码:
public void openDrawer(){
drawer.openDrawer(Gravity.LEFT);
}
希望这对您有用!