Java 使用Android Studio创建自定义视图

Java 使用Android Studio创建自定义视图,java,android,xml,android-activity,fragment,Java,Android,Xml,Android Activity,Fragment,我试图在Android Studio中创建一个小视图,可以从右向左拖动它。此视图将有2个按钮 当您选择其中一个或按其外部时,小菜单将再次隐藏 我一直在搜索,但我没有找到任何类似的图书馆。我也不知道怎么做 我可以在单独的视图(布局xml)中绘制小视图,但我不知道如何添加它并通过拖动创建要打开或关闭的事件 我该怎么做 谢谢。首先,在您的渐变中添加android材质依赖项: implementation 'com.google.android.material:material:1.1.0' 然

我试图在Android Studio中创建一个小视图,可以从右向左拖动它。此视图将有2个按钮

当您选择其中一个或按其外部时,小菜单将再次隐藏

我一直在搜索,但我没有找到任何类似的图书馆。我也不知道怎么做

我可以在单独的视图(布局xml)中绘制小视图,但我不知道如何添加它并通过拖动创建要打开或关闭的事件

我该怎么做


谢谢。

首先,在您的
渐变中添加android
材质
依赖项

implementation  'com.google.android.material:material:1.1.0'
然后您可以像这样使用
NavigationView
组件:

<com.google.android.material.navigation.NavigationView
    android:id="@+id/navi_view"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:background="@color/colorPrimary"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <Button
        android:id="@+id/btn1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="btn1" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="end"
        android:text="btn2" />


</com.google.android.material.navigation.NavigationView>


这是一个没有约定的非常简单的示例,您应该对其进行改进。这是创建自定义可拖动抽屉的基本示例

这些是我看过的参考资料

为了检测拖动/投掷手势,我使用了GestureDetectorCompat,和 我提到:

要创建我提到的抽屉打开和关闭动画,请执行以下操作:

请注意,这是一个非常基本的示例。 你可以以此为基础来创建你的最终目标。 您必须过滤掉收到的不需要的拖放回调。 你将不得不忽略抽屉上的水龙头

下面是实现

MainActivity.java

导入androidx.appcompat.app.appcompat活动;
导入androidx.constraintlayout.widget.constraintlayout;
导入androidx.constraintlayout.widget.ConstraintSet;
导入androidx.core.view.GestureDetectorCompat;
导入android.os.Bundle;
导入android.transition.TransitionManager;
导入android.view.GestureDetector;
导入android.view.MotionEvent;
公共类MainActivity扩展了AppCompatActivity{
私有布尔值被套住;
私有约束约束mRootConstraintLayout;
私有最终约束集mDrawerClosedConstraintSet=新约束集();
私有最终约束集MDRAWEROPENDCONSTRAINTSET=新约束集();
私人手势检测器Compat mGestureDetector;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u main\u drawer\u closed);
//抽屉最初是关闭的
错拉=假;
mRootConstraintLayout=findviewbyd(R.id.rootConstraintLayout);
mDrawerClosedConstraintSet.clone(此,R.layout.activity\u main\u drawer\u closed);
mDrawerOpenedConstraintSet.clone(此,R.layout.activity\u main\u drawer\u已打开);
mGestureDetector=新的GestureDetectorCompat(
getApplicationContext(),
新的GestureDetector.SimpleOnGestureListener(){
@凌驾
公共布尔onFling(MotionEvent e1、MotionEvent e2、float-velocityX、float-velocityY){
//检测到拖放手势
//待办事项:识别不需要的拖拽/投掷手势并忽略它们。
TransitionManager.beginDelayedTransition(mRootConstraintLayout);
//抽屉关上了吗?
如果(!被套住){
//打开抽屉
mDrawerOpenedConstraintSet.applyTo(mrootcConstraintLayout);
错误的=正确的;
}
返回true;
}
@凌驾
公共布尔onSingleTapUp(运动事件e){
//检测到单个抽头
//TODO:如果用户点击了抽屉,请不要关闭它。
TransitionManager.beginDelayedTransition(mRootConstraintLayout);
//抽屉打开了吗?
如果(错拉){
//关上抽屉
mDrawerClosedConstraintSet.applyTo(mRootConstraintLayout);
错拉=假;
}
返回true;
}
@凌驾
公共布尔onDown(运动事件e){
返回true;
}
}
);
}
@凌驾
公共布尔onTouchEvent(运动事件){
mGestureDetector.onTouchEvent(事件);
返回super.onTouchEvent(事件);
}
}
res/layout/activity\u main\u drawer\u closed.xml


res/layout/activity\u main\u drawer\u opened.xml


res/drawable/drawer_notch.xml


app/build.gradle

android {
    defaultConfig {
        minSdkVersion 19
        .
        .
        .
    }
    .
    .
    .
}
结果:


是否尝试使用
导航视图
组件?不完全是这样,视图较小,并且包含水平方向的图标。虽然它的行为类似,因为它是从一侧拖动的,在按下外部时必须关闭,但我认为您可以更改
导航视图的大小,然后在其中定义一个
相对性视图,其中包含两个
按钮。
。您尝试了我的答案吗?这是一个好主意,我尝试了@Lakindu的想法。谢谢你。太好了,它对我来说是正确的。您只需指出必须将以下内容添加到Main_Activity.xml:
。谢谢你@lakindu我很高兴它对你有用。在本例中,用于
MainActivity
的布局资源是
activity\u main\u drawer\u closed.xml
。您可以在
onCreate
方法中看到它。这里没有
main_activity.xml
。是的,你可以有一个
main\u activity.xml
,如果你愿意,可以在里面包含
activity\u main\u drawer\u关闭的
布局。嗨@Lakindu,我对旧的Android版本有一个问题(例如,版本21)。抽屉